[PATCH] hrtimers: hrtimer_clock_base description typo
[wrt350n-kernel.git] / fs / sysfs / bin.c
blobd3b9f5f07db149a38ecb03d0a44370d365ffb5d5
1 /*
2 * bin.c - binary file operations for sysfs.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 */
9 #undef DEBUG
11 #include <linux/errno.h>
12 #include <linux/fs.h>
13 #include <linux/kernel.h>
14 #include <linux/kobject.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <asm/uaccess.h>
19 #include <asm/semaphore.h>
21 #include "sysfs.h"
23 static int
24 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
26 struct bin_attribute * attr = to_bin_attr(dentry);
27 struct kobject * kobj = to_kobj(dentry->d_parent);
29 if (!attr->read)
30 return -EIO;
32 return attr->read(kobj, buffer, off, count);
35 static ssize_t
36 read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
38 char *buffer = file->private_data;
39 struct dentry *dentry = file->f_path.dentry;
40 int size = dentry->d_inode->i_size;
41 loff_t offs = *off;
42 int ret;
44 if (count > PAGE_SIZE)
45 count = PAGE_SIZE;
47 if (size) {
48 if (offs > size)
49 return 0;
50 if (offs + count > size)
51 count = size - offs;
54 ret = fill_read(dentry, buffer, offs, count);
55 if (ret < 0)
56 return ret;
57 count = ret;
59 if (copy_to_user(userbuf, buffer, count))
60 return -EFAULT;
62 pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
64 *off = offs + count;
66 return count;
69 static int
70 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
72 struct bin_attribute *attr = to_bin_attr(dentry);
73 struct kobject *kobj = to_kobj(dentry->d_parent);
75 if (!attr->write)
76 return -EIO;
78 return attr->write(kobj, buffer, offset, count);
81 static ssize_t write(struct file * file, const char __user * userbuf,
82 size_t count, loff_t * off)
84 char *buffer = file->private_data;
85 struct dentry *dentry = file->f_path.dentry;
86 int size = dentry->d_inode->i_size;
87 loff_t offs = *off;
89 if (count > PAGE_SIZE)
90 count = PAGE_SIZE;
91 if (size) {
92 if (offs > size)
93 return 0;
94 if (offs + count > size)
95 count = size - offs;
98 if (copy_from_user(buffer, userbuf, count))
99 return -EFAULT;
101 count = flush_write(dentry, buffer, offs, count);
102 if (count > 0)
103 *off = offs + count;
104 return count;
107 static int mmap(struct file *file, struct vm_area_struct *vma)
109 struct dentry *dentry = file->f_path.dentry;
110 struct bin_attribute *attr = to_bin_attr(dentry);
111 struct kobject *kobj = to_kobj(dentry->d_parent);
113 if (!attr->mmap)
114 return -EINVAL;
116 return attr->mmap(kobj, attr, vma);
119 static int open(struct inode * inode, struct file * file)
121 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
122 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
123 int error = -EINVAL;
125 if (!kobj || !attr)
126 goto Done;
128 /* Grab the module reference for this attribute if we have one */
129 error = -ENODEV;
130 if (!try_module_get(attr->attr.owner))
131 goto Done;
133 error = -EACCES;
134 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
135 goto Error;
136 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
137 goto Error;
139 error = -ENOMEM;
140 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
141 if (!file->private_data)
142 goto Error;
144 error = 0;
145 goto Done;
147 Error:
148 module_put(attr->attr.owner);
149 Done:
150 if (error)
151 kobject_put(kobj);
152 return error;
155 static int release(struct inode * inode, struct file * file)
157 struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
158 struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
159 u8 * buffer = file->private_data;
161 kobject_put(kobj);
162 module_put(attr->attr.owner);
163 kfree(buffer);
164 return 0;
167 const struct file_operations bin_fops = {
168 .read = read,
169 .write = write,
170 .mmap = mmap,
171 .llseek = generic_file_llseek,
172 .open = open,
173 .release = release,
177 * sysfs_create_bin_file - create binary file for object.
178 * @kobj: object.
179 * @attr: attribute descriptor.
182 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
184 BUG_ON(!kobj || !kobj->dentry || !attr);
186 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
191 * sysfs_remove_bin_file - remove binary file for object.
192 * @kobj: object.
193 * @attr: attribute descriptor.
196 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
198 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
199 printk(KERN_ERR "%s: "
200 "bad dentry or inode or no such file: \"%s\"\n",
201 __FUNCTION__, attr->attr.name);
202 dump_stack();
206 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
207 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);