]>
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> |
e0edd3c6 | 24 | #include <linux/mm.h> |
1da177e4 LT |
25 | |
26 | #include <asm/uaccess.h> | |
27 | ||
28 | #include "sysfs.h" | |
29 | ||
e0edd3c6 EB |
30 | /* |
31 | * There's one bin_buffer for each open file. | |
32 | * | |
33 | * filp->private_data points to bin_buffer and | |
34 | * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s | |
35 | * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock | |
36 | */ | |
37 | static DEFINE_MUTEX(sysfs_bin_lock); | |
38 | ||
eb361653 | 39 | struct bin_buffer { |
e0edd3c6 EB |
40 | struct mutex mutex; |
41 | void *buffer; | |
42 | int mmapped; | |
f0f37e2f | 43 | const struct vm_operations_struct *vm_ops; |
e0edd3c6 EB |
44 | struct file *file; |
45 | struct hlist_node list; | |
eb361653 TH |
46 | }; |
47 | ||
1da177e4 | 48 | static int |
2c3c8bea | 49 | fill_read(struct file *file, char *buffer, loff_t off, size_t count) |
1da177e4 | 50 | { |
2c3c8bea | 51 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 TH |
52 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
53 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
54 | int rc; |
55 | ||
56 | /* need attr_sd for attr, its parent for kobj */ | |
e72ceb8c | 57 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 58 | return -ENODEV; |
1da177e4 | 59 | |
0ab66088 TH |
60 | rc = -EIO; |
61 | if (attr->read) | |
2c3c8bea | 62 | rc = attr->read(file, kobj, attr, buffer, off, count); |
1da177e4 | 63 | |
e72ceb8c | 64 | sysfs_put_active(attr_sd); |
0ab66088 TH |
65 | |
66 | return rc; | |
1da177e4 LT |
67 | } |
68 | ||
69 | static ssize_t | |
93e3cd82 | 70 | read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) |
1da177e4 | 71 | { |
eb361653 | 72 | struct bin_buffer *bb = file->private_data; |
496ad9aa | 73 | int size = file_inode(file)->i_size; |
1da177e4 | 74 | loff_t offs = *off; |
93e3cd82 | 75 | int count = min_t(size_t, bytes, PAGE_SIZE); |
b31ca3f5 | 76 | char *temp; |
1da177e4 | 77 | |
4503efd0 GKH |
78 | if (!bytes) |
79 | return 0; | |
80 | ||
1da177e4 LT |
81 | if (size) { |
82 | if (offs > size) | |
83 | return 0; | |
84 | if (offs + count > size) | |
85 | count = size - offs; | |
86 | } | |
87 | ||
b31ca3f5 NP |
88 | temp = kmalloc(count, GFP_KERNEL); |
89 | if (!temp) | |
90 | return -ENOMEM; | |
91 | ||
eb361653 TH |
92 | mutex_lock(&bb->mutex); |
93 | ||
2c3c8bea | 94 | count = fill_read(file, bb->buffer, offs, count); |
b31ca3f5 NP |
95 | if (count < 0) { |
96 | mutex_unlock(&bb->mutex); | |
97 | goto out_free; | |
98 | } | |
1da177e4 | 99 | |
b31ca3f5 NP |
100 | memcpy(temp, bb->buffer, count); |
101 | ||
102 | mutex_unlock(&bb->mutex); | |
103 | ||
104 | if (copy_to_user(userbuf, temp, count)) { | |
eb361653 | 105 | count = -EFAULT; |
b31ca3f5 | 106 | goto out_free; |
eb361653 | 107 | } |
1da177e4 | 108 | |
93e3cd82 | 109 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
1da177e4 LT |
110 | |
111 | *off = offs + count; | |
112 | ||
b31ca3f5 NP |
113 | out_free: |
114 | kfree(temp); | |
1da177e4 LT |
115 | return count; |
116 | } | |
117 | ||
118 | static int | |
2c3c8bea | 119 | flush_write(struct file *file, char *buffer, loff_t offset, size_t count) |
1da177e4 | 120 | { |
2c3c8bea | 121 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 TH |
122 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
123 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
124 | int rc; |
125 | ||
126 | /* need attr_sd for attr, its parent for kobj */ | |
e72ceb8c | 127 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 128 | return -ENODEV; |
1da177e4 | 129 | |
0ab66088 TH |
130 | rc = -EIO; |
131 | if (attr->write) | |
2c3c8bea | 132 | rc = attr->write(file, kobj, attr, buffer, offset, count); |
1da177e4 | 133 | |
e72ceb8c | 134 | sysfs_put_active(attr_sd); |
0ab66088 TH |
135 | |
136 | return rc; | |
1da177e4 LT |
137 | } |
138 | ||
93e3cd82 TH |
139 | static ssize_t write(struct file *file, const char __user *userbuf, |
140 | size_t bytes, loff_t *off) | |
1da177e4 | 141 | { |
eb361653 | 142 | struct bin_buffer *bb = file->private_data; |
496ad9aa | 143 | int size = file_inode(file)->i_size; |
1da177e4 | 144 | loff_t offs = *off; |
93e3cd82 | 145 | int count = min_t(size_t, bytes, PAGE_SIZE); |
b31ca3f5 | 146 | char *temp; |
1da177e4 | 147 | |
4503efd0 GKH |
148 | if (!bytes) |
149 | return 0; | |
150 | ||
1da177e4 LT |
151 | if (size) { |
152 | if (offs > size) | |
153 | return 0; | |
154 | if (offs + count > size) | |
155 | count = size - offs; | |
156 | } | |
157 | ||
1c8542c7 LZ |
158 | temp = memdup_user(userbuf, count); |
159 | if (IS_ERR(temp)) | |
160 | return PTR_ERR(temp); | |
1da177e4 | 161 | |
b31ca3f5 NP |
162 | mutex_lock(&bb->mutex); |
163 | ||
164 | memcpy(bb->buffer, temp, count); | |
165 | ||
2c3c8bea | 166 | count = flush_write(file, bb->buffer, offs, count); |
b31ca3f5 NP |
167 | mutex_unlock(&bb->mutex); |
168 | ||
1da177e4 LT |
169 | if (count > 0) |
170 | *off = offs + count; | |
eb361653 | 171 | |
d5ce5b40 | 172 | kfree(temp); |
1da177e4 LT |
173 | return count; |
174 | } | |
175 | ||
e0edd3c6 EB |
176 | static void bin_vma_open(struct vm_area_struct *vma) |
177 | { | |
178 | struct file *file = vma->vm_file; | |
179 | struct bin_buffer *bb = file->private_data; | |
180 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
181 | ||
38f49a51 | 182 | if (!bb->vm_ops) |
e0edd3c6 EB |
183 | return; |
184 | ||
e72ceb8c | 185 | if (!sysfs_get_active(attr_sd)) |
e0edd3c6 EB |
186 | return; |
187 | ||
38f49a51 EB |
188 | if (bb->vm_ops->open) |
189 | bb->vm_ops->open(vma); | |
e0edd3c6 | 190 | |
e72ceb8c | 191 | sysfs_put_active(attr_sd); |
e0edd3c6 EB |
192 | } |
193 | ||
e0edd3c6 EB |
194 | static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
195 | { | |
196 | struct file *file = vma->vm_file; | |
197 | struct bin_buffer *bb = file->private_data; | |
198 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
199 | int ret; | |
200 | ||
38f49a51 | 201 | if (!bb->vm_ops) |
e0edd3c6 EB |
202 | return VM_FAULT_SIGBUS; |
203 | ||
e72ceb8c | 204 | if (!sysfs_get_active(attr_sd)) |
e0edd3c6 EB |
205 | return VM_FAULT_SIGBUS; |
206 | ||
38f49a51 EB |
207 | ret = VM_FAULT_SIGBUS; |
208 | if (bb->vm_ops->fault) | |
209 | ret = bb->vm_ops->fault(vma, vmf); | |
e0edd3c6 | 210 | |
e72ceb8c | 211 | sysfs_put_active(attr_sd); |
e0edd3c6 EB |
212 | return ret; |
213 | } | |
214 | ||
851a039c | 215 | static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
e0edd3c6 EB |
216 | { |
217 | struct file *file = vma->vm_file; | |
218 | struct bin_buffer *bb = file->private_data; | |
219 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
220 | int ret; | |
221 | ||
095160ae | 222 | if (!bb->vm_ops) |
851a039c | 223 | return VM_FAULT_SIGBUS; |
e0edd3c6 | 224 | |
e72ceb8c | 225 | if (!sysfs_get_active(attr_sd)) |
851a039c | 226 | return VM_FAULT_SIGBUS; |
e0edd3c6 | 227 | |
38f49a51 EB |
228 | ret = 0; |
229 | if (bb->vm_ops->page_mkwrite) | |
230 | ret = bb->vm_ops->page_mkwrite(vma, vmf); | |
14ae417c JK |
231 | else |
232 | file_update_time(file); | |
e0edd3c6 | 233 | |
e72ceb8c | 234 | sysfs_put_active(attr_sd); |
e0edd3c6 EB |
235 | return ret; |
236 | } | |
237 | ||
238 | static int bin_access(struct vm_area_struct *vma, unsigned long addr, | |
239 | void *buf, int len, int write) | |
240 | { | |
241 | struct file *file = vma->vm_file; | |
242 | struct bin_buffer *bb = file->private_data; | |
243 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
244 | int ret; | |
245 | ||
38f49a51 | 246 | if (!bb->vm_ops) |
e0edd3c6 EB |
247 | return -EINVAL; |
248 | ||
e72ceb8c | 249 | if (!sysfs_get_active(attr_sd)) |
e0edd3c6 EB |
250 | return -EINVAL; |
251 | ||
38f49a51 EB |
252 | ret = -EINVAL; |
253 | if (bb->vm_ops->access) | |
254 | ret = bb->vm_ops->access(vma, addr, buf, len, write); | |
e0edd3c6 | 255 | |
e72ceb8c | 256 | sysfs_put_active(attr_sd); |
e0edd3c6 EB |
257 | return ret; |
258 | } | |
259 | ||
095160ae HD |
260 | #ifdef CONFIG_NUMA |
261 | static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new) | |
262 | { | |
263 | struct file *file = vma->vm_file; | |
264 | struct bin_buffer *bb = file->private_data; | |
265 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
266 | int ret; | |
267 | ||
38f49a51 | 268 | if (!bb->vm_ops) |
095160ae HD |
269 | return 0; |
270 | ||
e72ceb8c | 271 | if (!sysfs_get_active(attr_sd)) |
095160ae HD |
272 | return -EINVAL; |
273 | ||
38f49a51 EB |
274 | ret = 0; |
275 | if (bb->vm_ops->set_policy) | |
276 | ret = bb->vm_ops->set_policy(vma, new); | |
095160ae | 277 | |
e72ceb8c | 278 | sysfs_put_active(attr_sd); |
095160ae HD |
279 | return ret; |
280 | } | |
281 | ||
282 | static struct mempolicy *bin_get_policy(struct vm_area_struct *vma, | |
283 | unsigned long addr) | |
284 | { | |
285 | struct file *file = vma->vm_file; | |
286 | struct bin_buffer *bb = file->private_data; | |
287 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
288 | struct mempolicy *pol; | |
289 | ||
38f49a51 | 290 | if (!bb->vm_ops) |
095160ae HD |
291 | return vma->vm_policy; |
292 | ||
e72ceb8c | 293 | if (!sysfs_get_active(attr_sd)) |
095160ae HD |
294 | return vma->vm_policy; |
295 | ||
38f49a51 EB |
296 | pol = vma->vm_policy; |
297 | if (bb->vm_ops->get_policy) | |
298 | pol = bb->vm_ops->get_policy(vma, addr); | |
095160ae | 299 | |
e72ceb8c | 300 | sysfs_put_active(attr_sd); |
095160ae HD |
301 | return pol; |
302 | } | |
303 | ||
304 | static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, | |
305 | const nodemask_t *to, unsigned long flags) | |
306 | { | |
307 | struct file *file = vma->vm_file; | |
308 | struct bin_buffer *bb = file->private_data; | |
309 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | |
310 | int ret; | |
311 | ||
38f49a51 | 312 | if (!bb->vm_ops) |
095160ae HD |
313 | return 0; |
314 | ||
e72ceb8c | 315 | if (!sysfs_get_active(attr_sd)) |
095160ae HD |
316 | return 0; |
317 | ||
38f49a51 EB |
318 | ret = 0; |
319 | if (bb->vm_ops->migrate) | |
320 | ret = bb->vm_ops->migrate(vma, from, to, flags); | |
095160ae | 321 | |
e72ceb8c | 322 | sysfs_put_active(attr_sd); |
095160ae HD |
323 | return ret; |
324 | } | |
325 | #endif | |
326 | ||
f0f37e2f | 327 | static const struct vm_operations_struct bin_vm_ops = { |
e0edd3c6 | 328 | .open = bin_vma_open, |
e0edd3c6 EB |
329 | .fault = bin_fault, |
330 | .page_mkwrite = bin_page_mkwrite, | |
331 | .access = bin_access, | |
095160ae HD |
332 | #ifdef CONFIG_NUMA |
333 | .set_policy = bin_set_policy, | |
334 | .get_policy = bin_get_policy, | |
335 | .migrate = bin_migrate, | |
336 | #endif | |
e0edd3c6 EB |
337 | }; |
338 | ||
1da177e4 LT |
339 | static int mmap(struct file *file, struct vm_area_struct *vma) |
340 | { | |
eb361653 | 341 | struct bin_buffer *bb = file->private_data; |
3e519038 | 342 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 TH |
343 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
344 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
eb361653 | 345 | int rc; |
1da177e4 | 346 | |
eb361653 | 347 | mutex_lock(&bb->mutex); |
0ab66088 TH |
348 | |
349 | /* need attr_sd for attr, its parent for kobj */ | |
e0edd3c6 | 350 | rc = -ENODEV; |
e72ceb8c | 351 | if (!sysfs_get_active(attr_sd)) |
e0edd3c6 | 352 | goto out_unlock; |
0ab66088 TH |
353 | |
354 | rc = -EINVAL; | |
e0edd3c6 EB |
355 | if (!attr->mmap) |
356 | goto out_put; | |
0ab66088 | 357 | |
2c3c8bea | 358 | rc = attr->mmap(file, kobj, attr, vma); |
e0edd3c6 EB |
359 | if (rc) |
360 | goto out_put; | |
0ab66088 | 361 | |
095160ae HD |
362 | /* |
363 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() | |
364 | * to satisfy versions of X which crash if the mmap fails: that | |
365 | * substitutes a new vm_file, and we don't then want bin_vm_ops. | |
366 | */ | |
367 | if (vma->vm_file != file) | |
e0edd3c6 EB |
368 | goto out_put; |
369 | ||
e0edd3c6 | 370 | rc = -EINVAL; |
095160ae | 371 | if (bb->mmapped && bb->vm_ops != vma->vm_ops) |
e0edd3c6 | 372 | goto out_put; |
e0edd3c6 | 373 | |
a6849fa1 EB |
374 | /* |
375 | * It is not possible to successfully wrap close. | |
376 | * So error if someone is trying to use close. | |
377 | */ | |
378 | rc = -EINVAL; | |
379 | if (vma->vm_ops && vma->vm_ops->close) | |
380 | goto out_put; | |
381 | ||
e0edd3c6 EB |
382 | rc = 0; |
383 | bb->mmapped = 1; | |
095160ae HD |
384 | bb->vm_ops = vma->vm_ops; |
385 | vma->vm_ops = &bin_vm_ops; | |
e0edd3c6 | 386 | out_put: |
e72ceb8c | 387 | sysfs_put_active(attr_sd); |
e0edd3c6 | 388 | out_unlock: |
eb361653 TH |
389 | mutex_unlock(&bb->mutex); |
390 | ||
391 | return rc; | |
1da177e4 LT |
392 | } |
393 | ||
394 | static int open(struct inode * inode, struct file * file) | |
395 | { | |
3e519038 | 396 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 | 397 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
eb361653 | 398 | struct bin_buffer *bb = NULL; |
0ab66088 | 399 | int error; |
1da177e4 | 400 | |
078ce640 | 401 | /* binary file operations requires both @sd and its parent */ |
e72ceb8c | 402 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 403 | return -ENODEV; |
1da177e4 | 404 | |
1da177e4 LT |
405 | error = -EACCES; |
406 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) | |
7b595756 | 407 | goto err_out; |
1da177e4 | 408 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
7b595756 | 409 | goto err_out; |
1da177e4 LT |
410 | |
411 | error = -ENOMEM; | |
eb361653 TH |
412 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
413 | if (!bb) | |
7b595756 | 414 | goto err_out; |
1da177e4 | 415 | |
eb361653 TH |
416 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
417 | if (!bb->buffer) | |
7b595756 | 418 | goto err_out; |
eb361653 TH |
419 | |
420 | mutex_init(&bb->mutex); | |
e0edd3c6 | 421 | bb->file = file; |
eb361653 TH |
422 | file->private_data = bb; |
423 | ||
e0edd3c6 EB |
424 | mutex_lock(&sysfs_bin_lock); |
425 | hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers); | |
426 | mutex_unlock(&sysfs_bin_lock); | |
427 | ||
078ce640 | 428 | /* open succeeded, put active references */ |
e72ceb8c | 429 | sysfs_put_active(attr_sd); |
0ab66088 | 430 | return 0; |
1da177e4 | 431 | |
7b595756 | 432 | err_out: |
e72ceb8c | 433 | sysfs_put_active(attr_sd); |
0ab66088 | 434 | kfree(bb); |
1da177e4 LT |
435 | return error; |
436 | } | |
437 | ||
438 | static int release(struct inode * inode, struct file * file) | |
439 | { | |
eb361653 | 440 | struct bin_buffer *bb = file->private_data; |
1da177e4 | 441 | |
e0edd3c6 EB |
442 | mutex_lock(&sysfs_bin_lock); |
443 | hlist_del(&bb->list); | |
444 | mutex_unlock(&sysfs_bin_lock); | |
445 | ||
eb361653 TH |
446 | kfree(bb->buffer); |
447 | kfree(bb); | |
1da177e4 LT |
448 | return 0; |
449 | } | |
450 | ||
4b6f5d20 | 451 | const struct file_operations bin_fops = { |
1da177e4 LT |
452 | .read = read, |
453 | .write = write, | |
454 | .mmap = mmap, | |
455 | .llseek = generic_file_llseek, | |
456 | .open = open, | |
457 | .release = release, | |
458 | }; | |
459 | ||
e0edd3c6 EB |
460 | |
461 | void unmap_bin_file(struct sysfs_dirent *attr_sd) | |
462 | { | |
463 | struct bin_buffer *bb; | |
e0edd3c6 EB |
464 | |
465 | if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR) | |
466 | return; | |
467 | ||
468 | mutex_lock(&sysfs_bin_lock); | |
469 | ||
b67bfe0d | 470 | hlist_for_each_entry(bb, &attr_sd->s_bin_attr.buffers, list) { |
496ad9aa | 471 | struct inode *inode = file_inode(bb->file); |
e0edd3c6 EB |
472 | |
473 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); | |
474 | } | |
475 | ||
476 | mutex_unlock(&sysfs_bin_lock); | |
477 | } | |
478 | ||
1da177e4 LT |
479 | /** |
480 | * sysfs_create_bin_file - create binary file for object. | |
481 | * @kobj: object. | |
482 | * @attr: attribute descriptor. | |
1da177e4 LT |
483 | */ |
484 | ||
66ecb92b PC |
485 | int sysfs_create_bin_file(struct kobject *kobj, |
486 | const struct bin_attribute *attr) | |
1da177e4 | 487 | { |
608e266a | 488 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 489 | |
608e266a | 490 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
1da177e4 LT |
491 | } |
492 | ||
493 | ||
494 | /** | |
495 | * sysfs_remove_bin_file - remove binary file for object. | |
496 | * @kobj: object. | |
497 | * @attr: attribute descriptor. | |
1da177e4 LT |
498 | */ |
499 | ||
66ecb92b PC |
500 | void sysfs_remove_bin_file(struct kobject *kobj, |
501 | const struct bin_attribute *attr) | |
1da177e4 | 502 | { |
3ff195b0 | 503 | sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name); |
1da177e4 LT |
504 | } |
505 | ||
506 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); | |
507 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |