]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6d66f5cd | 2 | * fs/sysfs/bin.c - sysfs binary file implementation |
1da177e4 LT |
3 | * |
4 | * Copyright (c) 2003 Patrick Mochel | |
5 | * Copyright (c) 2003 Matthew Wilcox | |
6 | * Copyright (c) 2004 Silicon Graphics, Inc. | |
6d66f5cd TH |
7 | * Copyright (c) 2007 SUSE Linux Products GmbH |
8 | * Copyright (c) 2007 Tejun Heo <[email protected]> | |
9 | * | |
10 | * This file is released under the GPLv2. | |
11 | * | |
12 | * Please see Documentation/filesystems/sysfs.txt for more information. | |
1da177e4 LT |
13 | */ |
14 | ||
15 | #undef DEBUG | |
16 | ||
17 | #include <linux/errno.h> | |
18 | #include <linux/fs.h> | |
995982ca | 19 | #include <linux/kernel.h> |
1da177e4 LT |
20 | #include <linux/kobject.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/slab.h> | |
869512ab | 23 | #include <linux/mutex.h> |
1da177e4 LT |
24 | |
25 | #include <asm/uaccess.h> | |
26 | ||
27 | #include "sysfs.h" | |
28 | ||
eb361653 TH |
29 | struct bin_buffer { |
30 | struct mutex mutex; | |
31 | void *buffer; | |
0ab66088 | 32 | int mmapped; |
eb361653 TH |
33 | }; |
34 | ||
1da177e4 LT |
35 | static int |
36 | fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) | |
37 | { | |
3e519038 | 38 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 TH |
39 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
40 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
41 | int rc; |
42 | ||
43 | /* need attr_sd for attr, its parent for kobj */ | |
44 | if (!sysfs_get_active_two(attr_sd)) | |
45 | return -ENODEV; | |
1da177e4 | 46 | |
0ab66088 TH |
47 | rc = -EIO; |
48 | if (attr->read) | |
91a69029 | 49 | rc = attr->read(kobj, attr, buffer, off, count); |
1da177e4 | 50 | |
0ab66088 TH |
51 | sysfs_put_active_two(attr_sd); |
52 | ||
53 | return rc; | |
1da177e4 LT |
54 | } |
55 | ||
56 | static ssize_t | |
93e3cd82 | 57 | read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) |
1da177e4 | 58 | { |
eb361653 | 59 | struct bin_buffer *bb = file->private_data; |
f427f5d5 | 60 | struct dentry *dentry = file->f_path.dentry; |
1da177e4 LT |
61 | int size = dentry->d_inode->i_size; |
62 | loff_t offs = *off; | |
93e3cd82 | 63 | int count = min_t(size_t, bytes, PAGE_SIZE); |
b31ca3f5 | 64 | char *temp; |
1da177e4 | 65 | |
4503efd0 GKH |
66 | if (!bytes) |
67 | return 0; | |
68 | ||
1da177e4 LT |
69 | if (size) { |
70 | if (offs > size) | |
71 | return 0; | |
72 | if (offs + count > size) | |
73 | count = size - offs; | |
74 | } | |
75 | ||
b31ca3f5 NP |
76 | temp = kmalloc(count, GFP_KERNEL); |
77 | if (!temp) | |
78 | return -ENOMEM; | |
79 | ||
eb361653 TH |
80 | mutex_lock(&bb->mutex); |
81 | ||
82 | count = fill_read(dentry, bb->buffer, offs, count); | |
b31ca3f5 NP |
83 | if (count < 0) { |
84 | mutex_unlock(&bb->mutex); | |
85 | goto out_free; | |
86 | } | |
1da177e4 | 87 | |
b31ca3f5 NP |
88 | memcpy(temp, bb->buffer, count); |
89 | ||
90 | mutex_unlock(&bb->mutex); | |
91 | ||
92 | if (copy_to_user(userbuf, temp, count)) { | |
eb361653 | 93 | count = -EFAULT; |
b31ca3f5 | 94 | goto out_free; |
eb361653 | 95 | } |
1da177e4 | 96 | |
93e3cd82 | 97 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
1da177e4 LT |
98 | |
99 | *off = offs + count; | |
100 | ||
b31ca3f5 NP |
101 | out_free: |
102 | kfree(temp); | |
1da177e4 LT |
103 | return count; |
104 | } | |
105 | ||
106 | static int | |
107 | flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) | |
108 | { | |
3e519038 | 109 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 TH |
110 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
111 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
112 | int rc; |
113 | ||
114 | /* need attr_sd for attr, its parent for kobj */ | |
115 | if (!sysfs_get_active_two(attr_sd)) | |
116 | return -ENODEV; | |
1da177e4 | 117 | |
0ab66088 TH |
118 | rc = -EIO; |
119 | if (attr->write) | |
91a69029 | 120 | rc = attr->write(kobj, attr, buffer, offset, count); |
1da177e4 | 121 | |
0ab66088 TH |
122 | sysfs_put_active_two(attr_sd); |
123 | ||
124 | return rc; | |
1da177e4 LT |
125 | } |
126 | ||
93e3cd82 TH |
127 | static ssize_t write(struct file *file, const char __user *userbuf, |
128 | size_t bytes, loff_t *off) | |
1da177e4 | 129 | { |
eb361653 | 130 | struct bin_buffer *bb = file->private_data; |
f427f5d5 | 131 | struct dentry *dentry = file->f_path.dentry; |
1da177e4 LT |
132 | int size = dentry->d_inode->i_size; |
133 | loff_t offs = *off; | |
93e3cd82 | 134 | int count = min_t(size_t, bytes, PAGE_SIZE); |
b31ca3f5 | 135 | char *temp; |
1da177e4 | 136 | |
4503efd0 GKH |
137 | if (!bytes) |
138 | return 0; | |
139 | ||
1da177e4 LT |
140 | if (size) { |
141 | if (offs > size) | |
142 | return 0; | |
143 | if (offs + count > size) | |
144 | count = size - offs; | |
145 | } | |
146 | ||
b31ca3f5 NP |
147 | temp = kmalloc(count, GFP_KERNEL); |
148 | if (!temp) | |
149 | return -ENOMEM; | |
eb361653 | 150 | |
b31ca3f5 | 151 | if (copy_from_user(temp, userbuf, count)) { |
eb361653 | 152 | count = -EFAULT; |
b31ca3f5 | 153 | goto out_free; |
eb361653 | 154 | } |
1da177e4 | 155 | |
b31ca3f5 NP |
156 | mutex_lock(&bb->mutex); |
157 | ||
158 | memcpy(bb->buffer, temp, count); | |
159 | ||
eb361653 | 160 | count = flush_write(dentry, bb->buffer, offs, count); |
b31ca3f5 NP |
161 | mutex_unlock(&bb->mutex); |
162 | ||
1da177e4 LT |
163 | if (count > 0) |
164 | *off = offs + count; | |
eb361653 | 165 | |
b31ca3f5 NP |
166 | out_free: |
167 | kfree(temp); | |
1da177e4 LT |
168 | return count; |
169 | } | |
170 | ||
171 | static int mmap(struct file *file, struct vm_area_struct *vma) | |
172 | { | |
eb361653 | 173 | struct bin_buffer *bb = file->private_data; |
3e519038 | 174 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 TH |
175 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
176 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
eb361653 | 177 | int rc; |
1da177e4 | 178 | |
eb361653 | 179 | mutex_lock(&bb->mutex); |
0ab66088 TH |
180 | |
181 | /* need attr_sd for attr, its parent for kobj */ | |
182 | if (!sysfs_get_active_two(attr_sd)) | |
183 | return -ENODEV; | |
184 | ||
185 | rc = -EINVAL; | |
186 | if (attr->mmap) | |
187 | rc = attr->mmap(kobj, attr, vma); | |
188 | ||
189 | if (rc == 0 && !bb->mmapped) | |
190 | bb->mmapped = 1; | |
191 | else | |
192 | sysfs_put_active_two(attr_sd); | |
193 | ||
eb361653 TH |
194 | mutex_unlock(&bb->mutex); |
195 | ||
196 | return rc; | |
1da177e4 LT |
197 | } |
198 | ||
199 | static int open(struct inode * inode, struct file * file) | |
200 | { | |
3e519038 | 201 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 | 202 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
eb361653 | 203 | struct bin_buffer *bb = NULL; |
0ab66088 | 204 | int error; |
1da177e4 | 205 | |
078ce640 TH |
206 | /* binary file operations requires both @sd and its parent */ |
207 | if (!sysfs_get_active_two(attr_sd)) | |
0ab66088 | 208 | return -ENODEV; |
1da177e4 | 209 | |
1da177e4 LT |
210 | error = -EACCES; |
211 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) | |
7b595756 | 212 | goto err_out; |
1da177e4 | 213 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
7b595756 | 214 | goto err_out; |
1da177e4 LT |
215 | |
216 | error = -ENOMEM; | |
eb361653 TH |
217 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
218 | if (!bb) | |
7b595756 | 219 | goto err_out; |
1da177e4 | 220 | |
eb361653 TH |
221 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
222 | if (!bb->buffer) | |
7b595756 | 223 | goto err_out; |
eb361653 TH |
224 | |
225 | mutex_init(&bb->mutex); | |
226 | file->private_data = bb; | |
227 | ||
078ce640 TH |
228 | /* open succeeded, put active references */ |
229 | sysfs_put_active_two(attr_sd); | |
0ab66088 | 230 | return 0; |
1da177e4 | 231 | |
7b595756 | 232 | err_out: |
078ce640 | 233 | sysfs_put_active_two(attr_sd); |
0ab66088 | 234 | kfree(bb); |
1da177e4 LT |
235 | return error; |
236 | } | |
237 | ||
238 | static int release(struct inode * inode, struct file * file) | |
239 | { | |
3e519038 | 240 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
eb361653 | 241 | struct bin_buffer *bb = file->private_data; |
1da177e4 | 242 | |
0ab66088 TH |
243 | if (bb->mmapped) |
244 | sysfs_put_active_two(attr_sd); | |
eb361653 TH |
245 | kfree(bb->buffer); |
246 | kfree(bb); | |
1da177e4 LT |
247 | return 0; |
248 | } | |
249 | ||
4b6f5d20 | 250 | const struct file_operations bin_fops = { |
1da177e4 LT |
251 | .read = read, |
252 | .write = write, | |
253 | .mmap = mmap, | |
254 | .llseek = generic_file_llseek, | |
255 | .open = open, | |
256 | .release = release, | |
257 | }; | |
258 | ||
259 | /** | |
260 | * sysfs_create_bin_file - create binary file for object. | |
261 | * @kobj: object. | |
262 | * @attr: attribute descriptor. | |
1da177e4 LT |
263 | */ |
264 | ||
265 | int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) | |
266 | { | |
608e266a | 267 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 268 | |
608e266a | 269 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
1da177e4 LT |
270 | } |
271 | ||
272 | ||
273 | /** | |
274 | * sysfs_remove_bin_file - remove binary file for object. | |
275 | * @kobj: object. | |
276 | * @attr: attribute descriptor. | |
1da177e4 LT |
277 | */ |
278 | ||
995982ca | 279 | void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
1da177e4 | 280 | { |
5f1835da | 281 | sysfs_hash_and_remove(kobj->sd, attr->attr.name); |
1da177e4 LT |
282 | } |
283 | ||
284 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); | |
285 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |