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