sysfs: fix some bin_vm_ops errors
[linux-2.6/mini2440.git] / fs / sysfs / bin.c
blob07703d3ff4a1ebdb8729bfcbfd7d759a7afbc06a
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 = kmalloc(count, GFP_KERNEL);
161 if (!temp)
162 return -ENOMEM;
164 if (copy_from_user(temp, userbuf, count)) {
165 count = -EFAULT;
166 goto out_free;
169 mutex_lock(&bb->mutex);
171 memcpy(bb->buffer, temp, count);
173 count = flush_write(dentry, bb->buffer, offs, count);
174 mutex_unlock(&bb->mutex);
176 if (count > 0)
177 *off = offs + count;
179 out_free:
180 kfree(temp);
181 return count;
184 static void bin_vma_open(struct vm_area_struct *vma)
186 struct file *file = vma->vm_file;
187 struct bin_buffer *bb = file->private_data;
188 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
190 if (!bb->vm_ops || !bb->vm_ops->open)
191 return;
193 if (!sysfs_get_active_two(attr_sd))
194 return;
196 bb->vm_ops->open(vma);
198 sysfs_put_active_two(attr_sd);
201 static void bin_vma_close(struct vm_area_struct *vma)
203 struct file *file = vma->vm_file;
204 struct bin_buffer *bb = file->private_data;
205 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
207 if (!bb->vm_ops || !bb->vm_ops->close)
208 return;
210 if (!sysfs_get_active_two(attr_sd))
211 return;
213 bb->vm_ops->close(vma);
215 sysfs_put_active_two(attr_sd);
218 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
220 struct file *file = vma->vm_file;
221 struct bin_buffer *bb = file->private_data;
222 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
223 int ret;
225 if (!bb->vm_ops || !bb->vm_ops->fault)
226 return VM_FAULT_SIGBUS;
228 if (!sysfs_get_active_two(attr_sd))
229 return VM_FAULT_SIGBUS;
231 ret = bb->vm_ops->fault(vma, vmf);
233 sysfs_put_active_two(attr_sd);
234 return ret;
237 static int bin_page_mkwrite(struct vm_area_struct *vma, struct page *page)
239 struct file *file = vma->vm_file;
240 struct bin_buffer *bb = file->private_data;
241 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
242 int ret;
244 if (!bb->vm_ops)
245 return -EINVAL;
247 if (!bb->vm_ops->page_mkwrite)
248 return 0;
250 if (!sysfs_get_active_two(attr_sd))
251 return -EINVAL;
253 ret = bb->vm_ops->page_mkwrite(vma, page);
255 sysfs_put_active_two(attr_sd);
256 return ret;
259 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
260 void *buf, int len, int write)
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;
267 if (!bb->vm_ops || !bb->vm_ops->access)
268 return -EINVAL;
270 if (!sysfs_get_active_two(attr_sd))
271 return -EINVAL;
273 ret = bb->vm_ops->access(vma, addr, buf, len, write);
275 sysfs_put_active_two(attr_sd);
276 return ret;
279 #ifdef CONFIG_NUMA
280 static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
282 struct file *file = vma->vm_file;
283 struct bin_buffer *bb = file->private_data;
284 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
285 int ret;
287 if (!bb->vm_ops || !bb->vm_ops->set_policy)
288 return 0;
290 if (!sysfs_get_active_two(attr_sd))
291 return -EINVAL;
293 ret = bb->vm_ops->set_policy(vma, new);
295 sysfs_put_active_two(attr_sd);
296 return ret;
299 static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
300 unsigned long addr)
302 struct file *file = vma->vm_file;
303 struct bin_buffer *bb = file->private_data;
304 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
305 struct mempolicy *pol;
307 if (!bb->vm_ops || !bb->vm_ops->get_policy)
308 return vma->vm_policy;
310 if (!sysfs_get_active_two(attr_sd))
311 return vma->vm_policy;
313 pol = bb->vm_ops->get_policy(vma, addr);
315 sysfs_put_active_two(attr_sd);
316 return pol;
319 static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
320 const nodemask_t *to, unsigned long flags)
322 struct file *file = vma->vm_file;
323 struct bin_buffer *bb = file->private_data;
324 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
325 int ret;
327 if (!bb->vm_ops || !bb->vm_ops->migrate)
328 return 0;
330 if (!sysfs_get_active_two(attr_sd))
331 return 0;
333 ret = bb->vm_ops->migrate(vma, from, to, flags);
335 sysfs_put_active_two(attr_sd);
336 return ret;
338 #endif
340 static struct vm_operations_struct bin_vm_ops = {
341 .open = bin_vma_open,
342 .close = bin_vma_close,
343 .fault = bin_fault,
344 .page_mkwrite = bin_page_mkwrite,
345 .access = bin_access,
346 #ifdef CONFIG_NUMA
347 .set_policy = bin_set_policy,
348 .get_policy = bin_get_policy,
349 .migrate = bin_migrate,
350 #endif
353 static int mmap(struct file *file, struct vm_area_struct *vma)
355 struct bin_buffer *bb = file->private_data;
356 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
357 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
358 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
359 int rc;
361 mutex_lock(&bb->mutex);
363 /* need attr_sd for attr, its parent for kobj */
364 rc = -ENODEV;
365 if (!sysfs_get_active_two(attr_sd))
366 goto out_unlock;
368 rc = -EINVAL;
369 if (!attr->mmap)
370 goto out_put;
372 rc = attr->mmap(kobj, attr, vma);
373 if (rc)
374 goto out_put;
377 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
378 * to satisfy versions of X which crash if the mmap fails: that
379 * substitutes a new vm_file, and we don't then want bin_vm_ops.
381 if (vma->vm_file != file)
382 goto out_put;
384 rc = -EINVAL;
385 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
386 goto out_put;
388 rc = 0;
389 bb->mmapped = 1;
390 bb->vm_ops = vma->vm_ops;
391 vma->vm_ops = &bin_vm_ops;
392 out_put:
393 sysfs_put_active_two(attr_sd);
394 out_unlock:
395 mutex_unlock(&bb->mutex);
397 return rc;
400 static int open(struct inode * inode, struct file * file)
402 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
403 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
404 struct bin_buffer *bb = NULL;
405 int error;
407 /* binary file operations requires both @sd and its parent */
408 if (!sysfs_get_active_two(attr_sd))
409 return -ENODEV;
411 error = -EACCES;
412 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
413 goto err_out;
414 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
415 goto err_out;
417 error = -ENOMEM;
418 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
419 if (!bb)
420 goto err_out;
422 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
423 if (!bb->buffer)
424 goto err_out;
426 mutex_init(&bb->mutex);
427 bb->file = file;
428 file->private_data = bb;
430 mutex_lock(&sysfs_bin_lock);
431 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
432 mutex_unlock(&sysfs_bin_lock);
434 /* open succeeded, put active references */
435 sysfs_put_active_two(attr_sd);
436 return 0;
438 err_out:
439 sysfs_put_active_two(attr_sd);
440 kfree(bb);
441 return error;
444 static int release(struct inode * inode, struct file * file)
446 struct bin_buffer *bb = file->private_data;
448 mutex_lock(&sysfs_bin_lock);
449 hlist_del(&bb->list);
450 mutex_unlock(&sysfs_bin_lock);
452 kfree(bb->buffer);
453 kfree(bb);
454 return 0;
457 const struct file_operations bin_fops = {
458 .read = read,
459 .write = write,
460 .mmap = mmap,
461 .llseek = generic_file_llseek,
462 .open = open,
463 .release = release,
467 void unmap_bin_file(struct sysfs_dirent *attr_sd)
469 struct bin_buffer *bb;
470 struct hlist_node *tmp;
472 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
473 return;
475 mutex_lock(&sysfs_bin_lock);
477 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
478 struct inode *inode = bb->file->f_path.dentry->d_inode;
480 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
483 mutex_unlock(&sysfs_bin_lock);
487 * sysfs_create_bin_file - create binary file for object.
488 * @kobj: object.
489 * @attr: attribute descriptor.
492 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
494 BUG_ON(!kobj || !kobj->sd || !attr);
496 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
501 * sysfs_remove_bin_file - remove binary file for object.
502 * @kobj: object.
503 * @attr: attribute descriptor.
506 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
508 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
511 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
512 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);