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 bin_attribute * attr = to_bin_attr(dentry);
27 struct kobject * kobj = to_kobj(dentry->d_parent);
32 return attr->read(kobj, buffer, off, count);
36 read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
38 char *buffer = file->private_data;
39 struct dentry *dentry = file->f_path.dentry;
40 int size = dentry->d_inode->i_size;
42 int count = min_t(size_t, bytes, PAGE_SIZE);
47 if (offs + count > size)
51 count = fill_read(dentry, buffer, offs, count);
55 if (copy_to_user(userbuf, buffer, count))
58 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
66 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
68 struct bin_attribute *attr = to_bin_attr(dentry);
69 struct kobject *kobj = to_kobj(dentry->d_parent);
74 return attr->write(kobj, buffer, offset, count);
77 static ssize_t write(struct file *file, const char __user *userbuf,
78 size_t bytes, loff_t *off)
80 char *buffer = file->private_data;
81 struct dentry *dentry = file->f_path.dentry;
82 int size = dentry->d_inode->i_size;
84 int count = min_t(size_t, bytes, PAGE_SIZE);
89 if (offs + count > size)
93 if (copy_from_user(buffer, userbuf, count))
96 count = flush_write(dentry, buffer, offs, count);
102 static int mmap(struct file *file, struct vm_area_struct *vma)
104 struct dentry *dentry = file->f_path.dentry;
105 struct bin_attribute *attr = to_bin_attr(dentry);
106 struct kobject *kobj = to_kobj(dentry->d_parent);
111 return attr->mmap(kobj, attr, vma);
114 static int open(struct inode * inode, struct file * file)
116 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
117 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
123 /* Grab the module reference for this attribute if we have one */
125 if (!try_module_get(attr->attr.owner))
129 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
131 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
135 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
136 if (!file->private_data)
143 module_put(attr->attr.owner);
150 static int release(struct inode * inode, struct file * file)
152 struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
153 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
154 u8 * buffer = file->private_data;
157 module_put(attr->attr.owner);
162 const struct file_operations bin_fops = {
166 .llseek = generic_file_llseek,
172 * sysfs_create_bin_file - create binary file for object.
174 * @attr: attribute descriptor.
177 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
179 BUG_ON(!kobj || !kobj->dentry || !attr);
181 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
186 * sysfs_remove_bin_file - remove binary file for object.
188 * @attr: attribute descriptor.
191 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
193 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
194 printk(KERN_ERR "%s: "
195 "bad dentry or inode or no such file: \"%s\"\n",
196 __FUNCTION__, attr->attr.name);
201 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
202 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);