GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / sysfs / bin.c
blob4e321f7353fa3e329134087ad8728a19a097a541
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 void bin_vma_close(struct vm_area_struct *vma)
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;
199 if (!bb->vm_ops || !bb->vm_ops->close)
200 return;
202 if (!sysfs_get_active(attr_sd))
203 return;
205 bb->vm_ops->close(vma);
207 sysfs_put_active(attr_sd);
210 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
212 struct file *file = vma->vm_file;
213 struct bin_buffer *bb = file->private_data;
214 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
215 int ret;
217 if (!bb->vm_ops || !bb->vm_ops->fault)
218 return VM_FAULT_SIGBUS;
220 if (!sysfs_get_active(attr_sd))
221 return VM_FAULT_SIGBUS;
223 ret = bb->vm_ops->fault(vma, vmf);
225 sysfs_put_active(attr_sd);
226 return ret;
229 static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
231 struct file *file = vma->vm_file;
232 struct bin_buffer *bb = file->private_data;
233 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
234 int ret;
236 if (!bb->vm_ops)
237 return VM_FAULT_SIGBUS;
239 if (!bb->vm_ops->page_mkwrite)
240 return 0;
242 if (!sysfs_get_active(attr_sd))
243 return VM_FAULT_SIGBUS;
245 ret = bb->vm_ops->page_mkwrite(vma, vmf);
247 sysfs_put_active(attr_sd);
248 return ret;
251 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
252 void *buf, int len, int write)
254 struct file *file = vma->vm_file;
255 struct bin_buffer *bb = file->private_data;
256 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
257 int ret;
259 if (!bb->vm_ops || !bb->vm_ops->access)
260 return -EINVAL;
262 if (!sysfs_get_active(attr_sd))
263 return -EINVAL;
265 ret = bb->vm_ops->access(vma, addr, buf, len, write);
267 sysfs_put_active(attr_sd);
268 return ret;
271 #ifdef CONFIG_NUMA
272 static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
274 struct file *file = vma->vm_file;
275 struct bin_buffer *bb = file->private_data;
276 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
277 int ret;
279 if (!bb->vm_ops || !bb->vm_ops->set_policy)
280 return 0;
282 if (!sysfs_get_active(attr_sd))
283 return -EINVAL;
285 ret = bb->vm_ops->set_policy(vma, new);
287 sysfs_put_active(attr_sd);
288 return ret;
291 static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
292 unsigned long addr)
294 struct file *file = vma->vm_file;
295 struct bin_buffer *bb = file->private_data;
296 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
297 struct mempolicy *pol;
299 if (!bb->vm_ops || !bb->vm_ops->get_policy)
300 return vma->vm_policy;
302 if (!sysfs_get_active(attr_sd))
303 return vma->vm_policy;
305 pol = bb->vm_ops->get_policy(vma, addr);
307 sysfs_put_active(attr_sd);
308 return pol;
311 static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
312 const nodemask_t *to, unsigned long flags)
314 struct file *file = vma->vm_file;
315 struct bin_buffer *bb = file->private_data;
316 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
317 int ret;
319 if (!bb->vm_ops || !bb->vm_ops->migrate)
320 return 0;
322 if (!sysfs_get_active(attr_sd))
323 return 0;
325 ret = bb->vm_ops->migrate(vma, from, to, flags);
327 sysfs_put_active(attr_sd);
328 return ret;
330 #endif
332 static const struct vm_operations_struct bin_vm_ops = {
333 .open = bin_vma_open,
334 .close = bin_vma_close,
335 .fault = bin_fault,
336 .page_mkwrite = bin_page_mkwrite,
337 .access = bin_access,
338 #ifdef CONFIG_NUMA
339 .set_policy = bin_set_policy,
340 .get_policy = bin_get_policy,
341 .migrate = bin_migrate,
342 #endif
345 static int mmap(struct file *file, struct vm_area_struct *vma)
347 struct bin_buffer *bb = file->private_data;
348 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
349 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
350 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
351 int rc;
353 mutex_lock(&bb->mutex);
355 /* need attr_sd for attr, its parent for kobj */
356 rc = -ENODEV;
357 if (!sysfs_get_active(attr_sd))
358 goto out_unlock;
360 rc = -EINVAL;
361 if (!attr->mmap)
362 goto out_put;
364 rc = attr->mmap(file, kobj, attr, vma);
365 if (rc)
366 goto out_put;
369 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
370 * to satisfy versions of X which crash if the mmap fails: that
371 * substitutes a new vm_file, and we don't then want bin_vm_ops.
373 if (vma->vm_file != file)
374 goto out_put;
376 rc = -EINVAL;
377 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
378 goto out_put;
380 rc = 0;
381 bb->mmapped = 1;
382 bb->vm_ops = vma->vm_ops;
383 vma->vm_ops = &bin_vm_ops;
384 out_put:
385 sysfs_put_active(attr_sd);
386 out_unlock:
387 mutex_unlock(&bb->mutex);
389 return rc;
392 static int open(struct inode * inode, struct file * file)
394 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
395 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
396 struct bin_buffer *bb = NULL;
397 int error;
399 /* binary file operations requires both @sd and its parent */
400 if (!sysfs_get_active(attr_sd))
401 return -ENODEV;
403 error = -EACCES;
404 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
405 goto err_out;
406 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
407 goto err_out;
409 error = -ENOMEM;
410 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
411 if (!bb)
412 goto err_out;
414 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
415 if (!bb->buffer)
416 goto err_out;
418 mutex_init(&bb->mutex);
419 bb->file = file;
420 file->private_data = bb;
422 mutex_lock(&sysfs_bin_lock);
423 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
424 mutex_unlock(&sysfs_bin_lock);
426 /* open succeeded, put active references */
427 sysfs_put_active(attr_sd);
428 return 0;
430 err_out:
431 sysfs_put_active(attr_sd);
432 kfree(bb);
433 return error;
436 static int release(struct inode * inode, struct file * file)
438 struct bin_buffer *bb = file->private_data;
440 mutex_lock(&sysfs_bin_lock);
441 hlist_del(&bb->list);
442 mutex_unlock(&sysfs_bin_lock);
444 kfree(bb->buffer);
445 kfree(bb);
446 return 0;
449 const struct file_operations bin_fops = {
450 .read = read,
451 .write = write,
452 .mmap = mmap,
453 .llseek = generic_file_llseek,
454 .open = open,
455 .release = release,
459 void unmap_bin_file(struct sysfs_dirent *attr_sd)
461 struct bin_buffer *bb;
462 struct hlist_node *tmp;
464 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
465 return;
467 mutex_lock(&sysfs_bin_lock);
469 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
470 struct inode *inode = bb->file->f_path.dentry->d_inode;
472 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
475 mutex_unlock(&sysfs_bin_lock);
479 * sysfs_create_bin_file - create binary file for object.
480 * @kobj: object.
481 * @attr: attribute descriptor.
484 int sysfs_create_bin_file(struct kobject *kobj,
485 const 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,
500 const struct bin_attribute *attr)
502 sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
505 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
506 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);