patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / sysfs / bin.c
1 /*
2  * bin.c - binary file operations for sysfs.
3  */
4
5 #undef DEBUG
6
7 #include <linux/errno.h>
8 #include <linux/fs.h>
9 #include <linux/kobject.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12
13 #include <asm/uaccess.h>
14
15 #include "sysfs.h"
16
17 static int
18 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
19 {
20         struct bin_attribute * attr = dentry->d_fsdata;
21         struct kobject * kobj = dentry->d_parent->d_fsdata;
22
23         return attr->read(kobj, buffer, off, count);
24 }
25
26 static ssize_t
27 read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
28 {
29         char *buffer = file->private_data;
30         struct dentry *dentry = file->f_dentry;
31         int size = dentry->d_inode->i_size;
32         loff_t offs = *off;
33         int ret;
34
35         if (count > PAGE_SIZE)
36                 count = PAGE_SIZE;
37
38         if (size) {
39                 if (offs > size)
40                         return 0;
41                 if (offs + count > size)
42                         count = size - offs;
43         }
44
45         ret = fill_read(dentry, buffer, offs, count);
46         if (ret < 0) 
47                 return ret;
48         count = ret;
49
50         if (copy_to_user(userbuf, buffer, count))
51                 return -EFAULT;
52
53         pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
54
55         *off = offs + count;
56
57         return count;
58 }
59
60 static int
61 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
62 {
63         struct bin_attribute *attr = dentry->d_fsdata;
64         struct kobject *kobj = dentry->d_parent->d_fsdata;
65
66         return attr->write(kobj, buffer, offset, count);
67 }
68
69 static ssize_t write(struct file * file, const char __user * userbuf,
70                      size_t count, loff_t * off)
71 {
72         char *buffer = file->private_data;
73         struct dentry *dentry = file->f_dentry;
74         int size = dentry->d_inode->i_size;
75         loff_t offs = *off;
76
77         if (count > PAGE_SIZE)
78                 count = PAGE_SIZE;
79         if (size) {
80                 if (offs > size)
81                         return 0;
82                 if (offs + count > size)
83                         count = size - offs;
84         }
85
86         if (copy_from_user(buffer, userbuf, count))
87                 return -EFAULT;
88
89         count = flush_write(dentry, buffer, offs, count);
90         if (count > 0)
91                 *off = offs + count;
92         return count;
93 }
94
95 static int open(struct inode * inode, struct file * file)
96 {
97         struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
98         struct bin_attribute * attr = file->f_dentry->d_fsdata;
99         int error = -EINVAL;
100
101         if (!kobj || !attr)
102                 goto Done;
103
104         /* Grab the module reference for this attribute if we have one */
105         error = -ENODEV;
106         if (!try_module_get(attr->attr.owner)) 
107                 goto Done;
108
109         error = -EACCES;
110         if ((file->f_mode & FMODE_WRITE) && !attr->write)
111                 goto Error;
112         if ((file->f_mode & FMODE_READ) && !attr->read)
113                 goto Error;
114
115         error = -ENOMEM;
116         file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
117         if (!file->private_data)
118                 goto Error;
119
120         error = 0;
121     goto Done;
122
123  Error:
124         module_put(attr->attr.owner);
125  Done:
126         if (error && kobj)
127                 kobject_put(kobj);
128         return error;
129 }
130
131 static int release(struct inode * inode, struct file * file)
132 {
133         struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
134         struct bin_attribute * attr = file->f_dentry->d_fsdata;
135         u8 * buffer = file->private_data;
136
137         if (kobj) 
138                 kobject_put(kobj);
139         module_put(attr->attr.owner);
140         kfree(buffer);
141         return 0;
142 }
143
144 static struct file_operations bin_fops = {
145         .read           = read,
146         .write          = write,
147         .llseek         = generic_file_llseek,
148         .open           = open,
149         .release        = release,
150 };
151
152 /**
153  *      sysfs_create_bin_file - create binary file for object.
154  *      @kobj:  object.
155  *      @attr:  attribute descriptor.
156  *
157  */
158
159 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
160 {
161         struct dentry * dentry;
162         struct dentry * parent;
163         int error = 0;
164
165         if (!kobj || !attr)
166                 return -EINVAL;
167
168         parent = kobj->dentry;
169
170         down(&parent->d_inode->i_sem);
171         dentry = sysfs_get_dentry(parent,attr->attr.name);
172         if (!IS_ERR(dentry)) {
173                 dentry->d_fsdata = (void *)attr;
174                 error = sysfs_create(dentry,
175                                      (attr->attr.mode & S_IALLUGO) | S_IFREG,
176                                      NULL);
177                 if (!error) {
178                         dentry->d_inode->i_size = attr->size;
179                         dentry->d_inode->i_fop = &bin_fops;
180                 }
181                 dput(dentry);
182         } else
183                 error = PTR_ERR(dentry);
184         up(&parent->d_inode->i_sem);
185         return error;
186 }
187
188
189 /**
190  *      sysfs_remove_bin_file - remove binary file for object.
191  *      @kobj:  object.
192  *      @attr:  attribute descriptor.
193  *
194  */
195
196 int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
197 {
198         sysfs_hash_and_remove(kobj->dentry,attr->attr.name);
199         return 0;
200 }
201
202 EXPORT_SYMBOL(sysfs_create_bin_file);
203 EXPORT_SYMBOL(sysfs_remove_bin_file);