ACPI: Introduce acpi_get_pci_dev()
[linux-2.6/linux-acpi-2.6.git] / fs / sysfs / bin.c
blob9345806c8853e369ba34695542081bb2e3f347db
1 /*
2 * fs/sysfs/bin.c - sysfs binary file implementation
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
10 * This file is released under the GPLv2.
12 * Please see Documentation/filesystems/sysfs.txt for more information.
15 #undef DEBUG
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/kobject.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/mm.h>
26 #include <asm/uaccess.h>
28 #include "sysfs.h"
31 * There's one bin_buffer for each open file.
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
37 static DEFINE_MUTEX(sysfs_bin_lock);
39 struct bin_buffer {
40 struct mutex mutex;
41 void *buffer;
42 int mmapped;
43 struct vm_operations_struct *vm_ops;
44 struct file *file;
45 struct hlist_node list;
48 static int
49 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
51 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
52 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
53 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
54 int rc;
56 /* need attr_sd for attr, its parent for kobj */
57 if (!sysfs_get_active_two(attr_sd))
58 return -ENODEV;
60 rc = -EIO;
61 if (attr->read)
62 rc = attr->read(kobj, attr, buffer, off, count);
64 sysfs_put_active_two(attr_sd);
66 return rc;
69 static ssize_t
70 read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
72 struct bin_buffer *bb = file->private_data;
73 struct dentry *dentry = file->f_path.dentry;
74 int size = dentry->d_inode->i_size;
75 loff_t offs = *off;
76 int count = min_t(size_t, bytes, PAGE_SIZE);
77 char *temp;
79 if (!bytes)
80 return 0;
82 if (size) {
83 if (offs > size)
84 return 0;
85 if (offs + count > size)
86 count = size - offs;
89 temp = kmalloc(count, GFP_KERNEL);
90 if (!temp)
91 return -ENOMEM;
93 mutex_lock(&bb->mutex);
95 count = fill_read(dentry, bb->buffer, offs, count);
96 if (count < 0) {
97 mutex_unlock(&bb->mutex);
98 goto out_free;
101 memcpy(temp, bb->buffer, count);
103 mutex_unlock(&bb->mutex);
105 if (copy_to_user(userbuf, temp, count)) {
106 count = -EFAULT;
107 goto out_free;
110 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
112 *off = offs + count;
114 out_free:
115 kfree(temp);
116 return count;
119 static int
120 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
122 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
123 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
124 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
125 int rc;
127 /* need attr_sd for attr, its parent for kobj */
128 if (!sysfs_get_active_two(attr_sd))
129 return -ENODEV;
131 rc = -EIO;
132 if (attr->write)
133 rc = attr->write(kobj, attr, buffer, offset, count);
135 sysfs_put_active_two(attr_sd);
137 return rc;
140 static ssize_t write(struct file *file, const char __user *userbuf,
141 size_t bytes, loff_t *off)
143 struct bin_buffer *bb = file->private_data;
144 struct dentry *dentry = file->f_path.dentry;
145 int size = dentry->d_inode->i_size;
146 loff_t offs = *off;
147 int count = min_t(size_t, bytes, PAGE_SIZE);
148 char *temp;
150 if (!bytes)
151 return 0;
153 if (size) {
154 if (offs > size)
155 return 0;
156 if (offs + count > size)
157 count = size - offs;
160 temp = memdup_user(userbuf, count);
161 if (IS_ERR(temp))
162 return PTR_ERR(temp);
164 mutex_lock(&bb->mutex);
166 memcpy(bb->buffer, temp, count);
168 count = flush_write(dentry, bb->buffer, offs, count);
169 mutex_unlock(&bb->mutex);
171 if (count > 0)
172 *off = offs + count;
174 return count;
177 static void bin_vma_open(struct vm_area_struct *vma)
179 struct file *file = vma->vm_file;
180 struct bin_buffer *bb = file->private_data;
181 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
183 if (!bb->vm_ops || !bb->vm_ops->open)
184 return;
186 if (!sysfs_get_active_two(attr_sd))
187 return;
189 bb->vm_ops->open(vma);
191 sysfs_put_active_two(attr_sd);
194 static void bin_vma_close(struct vm_area_struct *vma)
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;
200 if (!bb->vm_ops || !bb->vm_ops->close)
201 return;
203 if (!sysfs_get_active_two(attr_sd))
204 return;
206 bb->vm_ops->close(vma);
208 sysfs_put_active_two(attr_sd);
211 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
213 struct file *file = vma->vm_file;
214 struct bin_buffer *bb = file->private_data;
215 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
216 int ret;
218 if (!bb->vm_ops || !bb->vm_ops->fault)
219 return VM_FAULT_SIGBUS;
221 if (!sysfs_get_active_two(attr_sd))
222 return VM_FAULT_SIGBUS;
224 ret = bb->vm_ops->fault(vma, vmf);
226 sysfs_put_active_two(attr_sd);
227 return ret;
230 static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
232 struct file *file = vma->vm_file;
233 struct bin_buffer *bb = file->private_data;
234 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
235 int ret;
237 if (!bb->vm_ops)
238 return VM_FAULT_SIGBUS;
240 if (!bb->vm_ops->page_mkwrite)
241 return 0;
243 if (!sysfs_get_active_two(attr_sd))
244 return VM_FAULT_SIGBUS;
246 ret = bb->vm_ops->page_mkwrite(vma, vmf);
248 sysfs_put_active_two(attr_sd);
249 return ret;
252 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
253 void *buf, int len, int write)
255 struct file *file = vma->vm_file;
256 struct bin_buffer *bb = file->private_data;
257 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
258 int ret;
260 if (!bb->vm_ops || !bb->vm_ops->access)
261 return -EINVAL;
263 if (!sysfs_get_active_two(attr_sd))
264 return -EINVAL;
266 ret = bb->vm_ops->access(vma, addr, buf, len, write);
268 sysfs_put_active_two(attr_sd);
269 return ret;
272 #ifdef CONFIG_NUMA
273 static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
275 struct file *file = vma->vm_file;
276 struct bin_buffer *bb = file->private_data;
277 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
278 int ret;
280 if (!bb->vm_ops || !bb->vm_ops->set_policy)
281 return 0;
283 if (!sysfs_get_active_two(attr_sd))
284 return -EINVAL;
286 ret = bb->vm_ops->set_policy(vma, new);
288 sysfs_put_active_two(attr_sd);
289 return ret;
292 static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
293 unsigned long addr)
295 struct file *file = vma->vm_file;
296 struct bin_buffer *bb = file->private_data;
297 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
298 struct mempolicy *pol;
300 if (!bb->vm_ops || !bb->vm_ops->get_policy)
301 return vma->vm_policy;
303 if (!sysfs_get_active_two(attr_sd))
304 return vma->vm_policy;
306 pol = bb->vm_ops->get_policy(vma, addr);
308 sysfs_put_active_two(attr_sd);
309 return pol;
312 static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
313 const nodemask_t *to, unsigned long flags)
315 struct file *file = vma->vm_file;
316 struct bin_buffer *bb = file->private_data;
317 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
318 int ret;
320 if (!bb->vm_ops || !bb->vm_ops->migrate)
321 return 0;
323 if (!sysfs_get_active_two(attr_sd))
324 return 0;
326 ret = bb->vm_ops->migrate(vma, from, to, flags);
328 sysfs_put_active_two(attr_sd);
329 return ret;
331 #endif
333 static struct vm_operations_struct bin_vm_ops = {
334 .open = bin_vma_open,
335 .close = bin_vma_close,
336 .fault = bin_fault,
337 .page_mkwrite = bin_page_mkwrite,
338 .access = bin_access,
339 #ifdef CONFIG_NUMA
340 .set_policy = bin_set_policy,
341 .get_policy = bin_get_policy,
342 .migrate = bin_migrate,
343 #endif
346 static int mmap(struct file *file, struct vm_area_struct *vma)
348 struct bin_buffer *bb = file->private_data;
349 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
350 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
351 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
352 int rc;
354 mutex_lock(&bb->mutex);
356 /* need attr_sd for attr, its parent for kobj */
357 rc = -ENODEV;
358 if (!sysfs_get_active_two(attr_sd))
359 goto out_unlock;
361 rc = -EINVAL;
362 if (!attr->mmap)
363 goto out_put;
365 rc = attr->mmap(kobj, attr, vma);
366 if (rc)
367 goto out_put;
370 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
371 * to satisfy versions of X which crash if the mmap fails: that
372 * substitutes a new vm_file, and we don't then want bin_vm_ops.
374 if (vma->vm_file != file)
375 goto out_put;
377 rc = -EINVAL;
378 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
379 goto out_put;
381 rc = 0;
382 bb->mmapped = 1;
383 bb->vm_ops = vma->vm_ops;
384 vma->vm_ops = &bin_vm_ops;
385 out_put:
386 sysfs_put_active_two(attr_sd);
387 out_unlock:
388 mutex_unlock(&bb->mutex);
390 return rc;
393 static int open(struct inode * inode, struct file * file)
395 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
396 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
397 struct bin_buffer *bb = NULL;
398 int error;
400 /* binary file operations requires both @sd and its parent */
401 if (!sysfs_get_active_two(attr_sd))
402 return -ENODEV;
404 error = -EACCES;
405 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
406 goto err_out;
407 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
408 goto err_out;
410 error = -ENOMEM;
411 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
412 if (!bb)
413 goto err_out;
415 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
416 if (!bb->buffer)
417 goto err_out;
419 mutex_init(&bb->mutex);
420 bb->file = file;
421 file->private_data = bb;
423 mutex_lock(&sysfs_bin_lock);
424 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
425 mutex_unlock(&sysfs_bin_lock);
427 /* open succeeded, put active references */
428 sysfs_put_active_two(attr_sd);
429 return 0;
431 err_out:
432 sysfs_put_active_two(attr_sd);
433 kfree(bb);
434 return error;
437 static int release(struct inode * inode, struct file * file)
439 struct bin_buffer *bb = file->private_data;
441 mutex_lock(&sysfs_bin_lock);
442 hlist_del(&bb->list);
443 mutex_unlock(&sysfs_bin_lock);
445 kfree(bb->buffer);
446 kfree(bb);
447 return 0;
450 const struct file_operations bin_fops = {
451 .read = read,
452 .write = write,
453 .mmap = mmap,
454 .llseek = generic_file_llseek,
455 .open = open,
456 .release = release,
460 void unmap_bin_file(struct sysfs_dirent *attr_sd)
462 struct bin_buffer *bb;
463 struct hlist_node *tmp;
465 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
466 return;
468 mutex_lock(&sysfs_bin_lock);
470 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
471 struct inode *inode = bb->file->f_path.dentry->d_inode;
473 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
476 mutex_unlock(&sysfs_bin_lock);
480 * sysfs_create_bin_file - create binary file for object.
481 * @kobj: object.
482 * @attr: attribute descriptor.
485 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
487 BUG_ON(!kobj || !kobj->sd || !attr);
489 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
494 * sysfs_remove_bin_file - remove binary file for object.
495 * @kobj: object.
496 * @attr: attribute descriptor.
499 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
501 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
504 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
505 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);