blob: 9ec1c8539a5dc9d09c2b192aa99787b6195b5888 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * bin.c - binary file operations for sysfs.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 */
8
9#undef DEBUG
10
11#include <linux/errno.h>
12#include <linux/fs.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070013#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17
18#include <asm/uaccess.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010019#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
22
23static int
24fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
25{
26 struct bin_attribute * attr = to_bin_attr(dentry);
27 struct kobject * kobj = to_kobj(dentry->d_parent);
28
29 if (!attr->read)
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050030 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 return attr->read(kobj, buffer, off, count);
33}
34
35static ssize_t
36read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
37{
38 char *buffer = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -080039 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 int size = dentry->d_inode->i_size;
41 loff_t offs = *off;
42 int ret;
43
44 if (count > PAGE_SIZE)
45 count = PAGE_SIZE;
46
47 if (size) {
48 if (offs > size)
49 return 0;
50 if (offs + count > size)
51 count = size - offs;
52 }
53
54 ret = fill_read(dentry, buffer, offs, count);
55 if (ret < 0)
56 return ret;
57 count = ret;
58
59 if (copy_to_user(userbuf, buffer, count))
60 return -EFAULT;
61
62 pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
63
64 *off = offs + count;
65
66 return count;
67}
68
69static int
70flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
71{
72 struct bin_attribute *attr = to_bin_attr(dentry);
73 struct kobject *kobj = to_kobj(dentry->d_parent);
74
75 if (!attr->write)
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050076 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return attr->write(kobj, buffer, offset, count);
79}
80
81static ssize_t write(struct file * file, const char __user * userbuf,
82 size_t count, loff_t * off)
83{
84 char *buffer = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -080085 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int size = dentry->d_inode->i_size;
87 loff_t offs = *off;
88
89 if (count > PAGE_SIZE)
90 count = PAGE_SIZE;
91 if (size) {
92 if (offs > size)
93 return 0;
94 if (offs + count > size)
95 count = size - offs;
96 }
97
98 if (copy_from_user(buffer, userbuf, count))
99 return -EFAULT;
100
101 count = flush_write(dentry, buffer, offs, count);
102 if (count > 0)
103 *off = offs + count;
104 return count;
105}
106
107static int mmap(struct file *file, struct vm_area_struct *vma)
108{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800109 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct bin_attribute *attr = to_bin_attr(dentry);
111 struct kobject *kobj = to_kobj(dentry->d_parent);
112
113 if (!attr->mmap)
114 return -EINVAL;
115
116 return attr->mmap(kobj, attr, vma);
117}
118
119static int open(struct inode * inode, struct file * file)
120{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800121 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
122 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int error = -EINVAL;
124
125 if (!kobj || !attr)
126 goto Done;
127
128 /* Grab the module reference for this attribute if we have one */
129 error = -ENODEV;
130 if (!try_module_get(attr->attr.owner))
131 goto Done;
132
133 error = -EACCES;
134 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
135 goto Error;
136 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
137 goto Error;
138
139 error = -ENOMEM;
140 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
141 if (!file->private_data)
142 goto Error;
143
144 error = 0;
145 goto Done;
146
147 Error:
148 module_put(attr->attr.owner);
149 Done:
150 if (error && kobj)
151 kobject_put(kobj);
152 return error;
153}
154
155static int release(struct inode * inode, struct file * file)
156{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800157 struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
158 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 u8 * buffer = file->private_data;
160
161 if (kobj)
162 kobject_put(kobj);
163 module_put(attr->attr.owner);
164 kfree(buffer);
165 return 0;
166}
167
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800168const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .read = read,
170 .write = write,
171 .mmap = mmap,
172 .llseek = generic_file_llseek,
173 .open = open,
174 .release = release,
175};
176
177/**
178 * sysfs_create_bin_file - create binary file for object.
179 * @kobj: object.
180 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
182
183int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
184{
185 BUG_ON(!kobj || !kobj->dentry || !attr);
186
187 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
188}
189
190
191/**
192 * sysfs_remove_bin_file - remove binary file for object.
193 * @kobj: object.
194 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196
Randy.Dunlap995982c2006-07-10 23:05:25 -0700197void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Randy.Dunlap995982c2006-07-10 23:05:25 -0700199 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
200 printk(KERN_ERR "%s: "
201 "bad dentry or inode or no such file: \"%s\"\n",
202 __FUNCTION__, attr->attr.name);
203 dump_stack();
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
208EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);