sysfs: Fail bin file mmap if vma close is implemented.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / sysfs / bin.c
blobd31d7b7d54261ada9e3ed3844654dcc8b817d3a7
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 const struct vm_operations_struct *vm_ops;
44 struct file *file;
45 struct hlist_node list;
48 static int
49 fill_read(struct file *file, char *buffer, loff_t off, size_t count)
51 struct sysfs_dirent *attr_sd = file->f_path.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(attr_sd))
58 return -ENODEV;
60 rc = -EIO;
61 if (attr->read)
62 rc = attr->read(file, kobj, attr, buffer, off, count);
64 sysfs_put_active(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 int size = file->f_path.dentry->d_inode->i_size;
74 loff_t offs = *off;
75 int count = min_t(size_t, bytes, PAGE_SIZE);
76 char *temp;
78 if (!bytes)
79 return 0;
81 if (size) {
82 if (offs > size)
83 return 0;
84 if (offs + count > size)
85 count = size - offs;
88 temp = kmalloc(count, GFP_KERNEL);
89 if (!temp)
90 return -ENOMEM;
92 mutex_lock(&bb->mutex);
94 count = fill_read(file, bb->buffer, offs, count);
95 if (count < 0) {
96 mutex_unlock(&bb->mutex);
97 goto out_free;
100 memcpy(temp, bb->buffer, count);
102 mutex_unlock(&bb->mutex);
104 if (copy_to_user(userbuf, temp, count)) {
105 count = -EFAULT;
106 goto out_free;
109 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
111 *off = offs + count;
113 out_free:
114 kfree(temp);
115 return count;
118 static int
119 flush_write(struct file *file, char *buffer, loff_t offset, size_t count)
121 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
122 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
123 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
124 int rc;
126 /* need attr_sd for attr, its parent for kobj */
127 if (!sysfs_get_active(attr_sd))
128 return -ENODEV;
130 rc = -EIO;
131 if (attr->write)
132 rc = attr->write(file, kobj, attr, buffer, offset, count);
134 sysfs_put_active(attr_sd);
136 return rc;
139 static ssize_t write(struct file *file, const char __user *userbuf,
140 size_t bytes, loff_t *off)
142 struct bin_buffer *bb = file->private_data;
143 int size = file->f_path.dentry->d_inode->i_size;
144 loff_t offs = *off;
145 int count = min_t(size_t, bytes, PAGE_SIZE);
146 char *temp;
148 if (!bytes)
149 return 0;
151 if (size) {
152 if (offs > size)
153 return 0;
154 if (offs + count > size)
155 count = size - offs;
158 temp = memdup_user(userbuf, count);
159 if (IS_ERR(temp))
160 return PTR_ERR(temp);
162 mutex_lock(&bb->mutex);
164 memcpy(bb->buffer, temp, count);
166 count = flush_write(file, bb->buffer, offs, count);
167 mutex_unlock(&bb->mutex);
169 if (count > 0)
170 *off = offs + count;
172 kfree(temp);
173 return count;
176 static void bin_vma_open(struct vm_area_struct *vma)
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;
182 if (!bb->vm_ops || !bb->vm_ops->open)
183 return;
185 if (!sysfs_get_active(attr_sd))
186 return;
188 bb->vm_ops->open(vma);
190 sysfs_put_active(attr_sd);
193 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
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;
200 if (!bb->vm_ops || !bb->vm_ops->fault)
201 return VM_FAULT_SIGBUS;
203 if (!sysfs_get_active(attr_sd))
204 return VM_FAULT_SIGBUS;
206 ret = bb->vm_ops->fault(vma, vmf);
208 sysfs_put_active(attr_sd);
209 return ret;
212 static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
214 struct file *file = vma->vm_file;
215 struct bin_buffer *bb = file->private_data;
216 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
217 int ret;
219 if (!bb->vm_ops)
220 return VM_FAULT_SIGBUS;
222 if (!bb->vm_ops->page_mkwrite)
223 return 0;
225 if (!sysfs_get_active(attr_sd))
226 return VM_FAULT_SIGBUS;
228 ret = bb->vm_ops->page_mkwrite(vma, vmf);
230 sysfs_put_active(attr_sd);
231 return ret;
234 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
235 void *buf, int len, int write)
237 struct file *file = vma->vm_file;
238 struct bin_buffer *bb = file->private_data;
239 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
240 int ret;
242 if (!bb->vm_ops || !bb->vm_ops->access)
243 return -EINVAL;
245 if (!sysfs_get_active(attr_sd))
246 return -EINVAL;
248 ret = bb->vm_ops->access(vma, addr, buf, len, write);
250 sysfs_put_active(attr_sd);
251 return ret;
254 #ifdef CONFIG_NUMA
255 static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
257 struct file *file = vma->vm_file;
258 struct bin_buffer *bb = file->private_data;
259 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
260 int ret;
262 if (!bb->vm_ops || !bb->vm_ops->set_policy)
263 return 0;
265 if (!sysfs_get_active(attr_sd))
266 return -EINVAL;
268 ret = bb->vm_ops->set_policy(vma, new);
270 sysfs_put_active(attr_sd);
271 return ret;
274 static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
275 unsigned long addr)
277 struct file *file = vma->vm_file;
278 struct bin_buffer *bb = file->private_data;
279 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
280 struct mempolicy *pol;
282 if (!bb->vm_ops || !bb->vm_ops->get_policy)
283 return vma->vm_policy;
285 if (!sysfs_get_active(attr_sd))
286 return vma->vm_policy;
288 pol = bb->vm_ops->get_policy(vma, addr);
290 sysfs_put_active(attr_sd);
291 return pol;
294 static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
295 const nodemask_t *to, unsigned long flags)
297 struct file *file = vma->vm_file;
298 struct bin_buffer *bb = file->private_data;
299 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
300 int ret;
302 if (!bb->vm_ops || !bb->vm_ops->migrate)
303 return 0;
305 if (!sysfs_get_active(attr_sd))
306 return 0;
308 ret = bb->vm_ops->migrate(vma, from, to, flags);
310 sysfs_put_active(attr_sd);
311 return ret;
313 #endif
315 static const struct vm_operations_struct bin_vm_ops = {
316 .open = bin_vma_open,
317 .fault = bin_fault,
318 .page_mkwrite = bin_page_mkwrite,
319 .access = bin_access,
320 #ifdef CONFIG_NUMA
321 .set_policy = bin_set_policy,
322 .get_policy = bin_get_policy,
323 .migrate = bin_migrate,
324 #endif
327 static int mmap(struct file *file, struct vm_area_struct *vma)
329 struct bin_buffer *bb = file->private_data;
330 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
331 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
332 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
333 int rc;
335 mutex_lock(&bb->mutex);
337 /* need attr_sd for attr, its parent for kobj */
338 rc = -ENODEV;
339 if (!sysfs_get_active(attr_sd))
340 goto out_unlock;
342 rc = -EINVAL;
343 if (!attr->mmap)
344 goto out_put;
346 rc = attr->mmap(file, kobj, attr, vma);
347 if (rc)
348 goto out_put;
351 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
352 * to satisfy versions of X which crash if the mmap fails: that
353 * substitutes a new vm_file, and we don't then want bin_vm_ops.
355 if (vma->vm_file != file)
356 goto out_put;
358 rc = -EINVAL;
359 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
360 goto out_put;
363 * It is not possible to successfully wrap close.
364 * So error if someone is trying to use close.
366 rc = -EINVAL;
367 if (vma->vm_ops && vma->vm_ops->close)
368 goto out_put;
370 rc = 0;
371 bb->mmapped = 1;
372 bb->vm_ops = vma->vm_ops;
373 vma->vm_ops = &bin_vm_ops;
374 out_put:
375 sysfs_put_active(attr_sd);
376 out_unlock:
377 mutex_unlock(&bb->mutex);
379 return rc;
382 static int open(struct inode * inode, struct file * file)
384 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
385 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
386 struct bin_buffer *bb = NULL;
387 int error;
389 /* binary file operations requires both @sd and its parent */
390 if (!sysfs_get_active(attr_sd))
391 return -ENODEV;
393 error = -EACCES;
394 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
395 goto err_out;
396 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
397 goto err_out;
399 error = -ENOMEM;
400 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
401 if (!bb)
402 goto err_out;
404 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
405 if (!bb->buffer)
406 goto err_out;
408 mutex_init(&bb->mutex);
409 bb->file = file;
410 file->private_data = bb;
412 mutex_lock(&sysfs_bin_lock);
413 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
414 mutex_unlock(&sysfs_bin_lock);
416 /* open succeeded, put active references */
417 sysfs_put_active(attr_sd);
418 return 0;
420 err_out:
421 sysfs_put_active(attr_sd);
422 kfree(bb);
423 return error;
426 static int release(struct inode * inode, struct file * file)
428 struct bin_buffer *bb = file->private_data;
430 mutex_lock(&sysfs_bin_lock);
431 hlist_del(&bb->list);
432 mutex_unlock(&sysfs_bin_lock);
434 kfree(bb->buffer);
435 kfree(bb);
436 return 0;
439 const struct file_operations bin_fops = {
440 .read = read,
441 .write = write,
442 .mmap = mmap,
443 .llseek = generic_file_llseek,
444 .open = open,
445 .release = release,
449 void unmap_bin_file(struct sysfs_dirent *attr_sd)
451 struct bin_buffer *bb;
452 struct hlist_node *tmp;
454 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
455 return;
457 mutex_lock(&sysfs_bin_lock);
459 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
460 struct inode *inode = bb->file->f_path.dentry->d_inode;
462 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
465 mutex_unlock(&sysfs_bin_lock);
469 * sysfs_create_bin_file - create binary file for object.
470 * @kobj: object.
471 * @attr: attribute descriptor.
474 int sysfs_create_bin_file(struct kobject *kobj,
475 const struct bin_attribute *attr)
477 BUG_ON(!kobj || !kobj->sd || !attr);
479 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
484 * sysfs_remove_bin_file - remove binary file for object.
485 * @kobj: object.
486 * @attr: attribute descriptor.
489 void sysfs_remove_bin_file(struct kobject *kobj,
490 const struct bin_attribute *attr)
492 sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
495 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
496 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);