MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / sysfs / file.c
blob159d99f06a3fa50f6cce1ef11242441f57367fbb
1 /*
2 * file.c - operations for regular (text) files.
3 */
5 #include <linux/module.h>
6 #include <linux/dnotify.h>
7 #include <linux/kobject.h>
8 #include <asm/uaccess.h>
10 #include "sysfs.h"
12 static struct file_operations sysfs_file_operations;
14 static int init_file(struct inode * inode)
16 inode->i_size = PAGE_SIZE;
17 inode->i_fop = &sysfs_file_operations;
18 return 0;
21 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
22 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
24 /**
25 * Subsystem file operations.
26 * These operations allow subsystems to have files that can be
27 * read/written.
29 static ssize_t
30 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
32 struct subsystem * s = to_subsys(kobj);
33 struct subsys_attribute * sattr = to_sattr(attr);
34 ssize_t ret = 0;
36 if (sattr->show)
37 ret = sattr->show(s,page);
38 return ret;
41 static ssize_t
42 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
43 const char * page, size_t count)
45 struct subsystem * s = to_subsys(kobj);
46 struct subsys_attribute * sattr = to_sattr(attr);
47 ssize_t ret = 0;
49 if (sattr->store)
50 ret = sattr->store(s,page,count);
51 return ret;
54 static struct sysfs_ops subsys_sysfs_ops = {
55 .show = subsys_attr_show,
56 .store = subsys_attr_store,
60 struct sysfs_buffer {
61 size_t count;
62 loff_t pos;
63 char * page;
64 struct sysfs_ops * ops;
68 /**
69 * fill_read_buffer - allocate and fill buffer from object.
70 * @file: file pointer.
71 * @buffer: data buffer for file.
73 * Allocate @buffer->page, if it hasn't been already, then call the
74 * kobject's show() method to fill the buffer with this attribute's
75 * data.
76 * This is called only once, on the file's first read.
78 static int fill_read_buffer(struct file * file, struct sysfs_buffer * buffer)
80 struct attribute * attr = file->f_dentry->d_fsdata;
81 struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
82 struct sysfs_ops * ops = buffer->ops;
83 int ret = 0;
84 ssize_t count;
86 if (!buffer->page)
87 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
88 if (!buffer->page)
89 return -ENOMEM;
91 count = ops->show(kobj,attr,buffer->page);
92 BUG_ON(count > (ssize_t)PAGE_SIZE);
93 if (count >= 0)
94 buffer->count = count;
95 else
96 ret = count;
97 return ret;
102 * flush_read_buffer - push buffer to userspace.
103 * @buffer: data buffer for file.
104 * @userbuf: user-passed buffer.
105 * @count: number of bytes requested.
106 * @ppos: file position.
108 * Copy the buffer we filled in fill_read_buffer() to userspace.
109 * This is done at the reader's leisure, copying and advancing
110 * the amount they specify each time.
111 * This may be called continuously until the buffer is empty.
113 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
114 size_t count, loff_t * ppos)
116 int error;
118 if (count > (buffer->count - *ppos))
119 count = buffer->count - *ppos;
121 error = copy_to_user(buf,buffer->page + *ppos,count);
122 if (!error)
123 *ppos += count;
124 return error ? -EFAULT : count;
128 * sysfs_read_file - read an attribute.
129 * @file: file pointer.
130 * @buf: buffer to fill.
131 * @count: number of bytes to read.
132 * @ppos: starting offset in file.
134 * Userspace wants to read an attribute file. The attribute descriptor
135 * is in the file's ->d_fsdata. The target object is in the directory's
136 * ->d_fsdata.
138 * We call fill_read_buffer() to allocate and fill the buffer from the
139 * object's show() method exactly once (if the read is happening from
140 * the beginning of the file). That should fill the entire buffer with
141 * all the data the object has to offer for that attribute.
142 * We then call flush_read_buffer() to copy the buffer to userspace
143 * in the increments specified.
146 static ssize_t
147 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
149 struct sysfs_buffer * buffer = file->private_data;
150 ssize_t retval = 0;
152 if (!*ppos) {
153 if ((retval = fill_read_buffer(file,buffer)))
154 return retval;
156 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
157 __FUNCTION__,count,*ppos,buffer->page);
158 return flush_read_buffer(buffer,buf,count,ppos);
163 * fill_write_buffer - copy buffer from userspace.
164 * @buffer: data buffer for file.
165 * @userbuf: data from user.
166 * @count: number of bytes in @userbuf.
168 * Allocate @buffer->page if it hasn't been already, then
169 * copy the user-supplied buffer into it.
172 static int
173 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
175 int error;
177 if (!buffer->page)
178 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
179 if (!buffer->page)
180 return -ENOMEM;
182 if (count >= PAGE_SIZE)
183 count = PAGE_SIZE - 1;
184 error = copy_from_user(buffer->page,buf,count);
185 return error ? -EFAULT : count;
190 * flush_write_buffer - push buffer to kobject.
191 * @file: file pointer.
192 * @buffer: data buffer for file.
194 * Get the correct pointers for the kobject and the attribute we're
195 * dealing with, then call the store() method for the attribute,
196 * passing the buffer that we acquired in fill_write_buffer().
199 static int
200 flush_write_buffer(struct file * file, struct sysfs_buffer * buffer, size_t count)
202 struct attribute * attr = file->f_dentry->d_fsdata;
203 struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
204 struct sysfs_ops * ops = buffer->ops;
206 return ops->store(kobj,attr,buffer->page,count);
211 * sysfs_write_file - write an attribute.
212 * @file: file pointer
213 * @buf: data to write
214 * @count: number of bytes
215 * @ppos: starting offset
217 * Similar to sysfs_read_file(), though working in the opposite direction.
218 * We allocate and fill the data from the user in fill_write_buffer(),
219 * then push it to the kobject in flush_write_buffer().
220 * There is no easy way for us to know if userspace is only doing a partial
221 * write, so we don't support them. We expect the entire buffer to come
222 * on the first write.
223 * Hint: if you're writing a value, first read the file, modify only the
224 * the value you're changing, then write entire buffer back.
227 static ssize_t
228 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
230 struct sysfs_buffer * buffer = file->private_data;
232 count = fill_write_buffer(buffer,buf,count);
233 if (count > 0)
234 count = flush_write_buffer(file,buffer,count);
235 if (count > 0)
236 *ppos += count;
237 return count;
240 static int check_perm(struct inode * inode, struct file * file)
242 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
243 struct attribute * attr = file->f_dentry->d_fsdata;
244 struct sysfs_buffer * buffer;
245 struct sysfs_ops * ops = NULL;
246 int error = 0;
248 if (!kobj || !attr)
249 goto Einval;
251 /* Grab the module reference for this attribute if we have one */
252 if (!try_module_get(attr->owner)) {
253 error = -ENODEV;
254 goto Done;
257 /* if the kobject has no ktype, then we assume that it is a subsystem
258 * itself, and use ops for it.
260 if (kobj->kset && kobj->kset->ktype)
261 ops = kobj->kset->ktype->sysfs_ops;
262 else if (kobj->ktype)
263 ops = kobj->ktype->sysfs_ops;
264 else
265 ops = &subsys_sysfs_ops;
267 /* No sysfs operations, either from having no subsystem,
268 * or the subsystem have no operations.
270 if (!ops)
271 goto Eaccess;
273 /* File needs write support.
274 * The inode's perms must say it's ok,
275 * and we must have a store method.
277 if (file->f_mode & FMODE_WRITE) {
279 if (!(inode->i_mode & S_IWUGO) || !ops->store)
280 goto Eaccess;
284 /* File needs read support.
285 * The inode's perms must say it's ok, and we there
286 * must be a show method for it.
288 if (file->f_mode & FMODE_READ) {
289 if (!(inode->i_mode & S_IRUGO) || !ops->show)
290 goto Eaccess;
293 /* No error? Great, allocate a buffer for the file, and store it
294 * it in file->private_data for easy access.
296 buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
297 if (buffer) {
298 memset(buffer,0,sizeof(struct sysfs_buffer));
299 buffer->ops = ops;
300 file->private_data = buffer;
301 } else
302 error = -ENOMEM;
303 goto Done;
305 Einval:
306 error = -EINVAL;
307 goto Done;
308 Eaccess:
309 error = -EACCES;
310 module_put(attr->owner);
311 Done:
312 if (error && kobj)
313 kobject_put(kobj);
314 return error;
317 static int sysfs_open_file(struct inode * inode, struct file * filp)
319 return check_perm(inode,filp);
322 static int sysfs_release(struct inode * inode, struct file * filp)
324 struct kobject * kobj = filp->f_dentry->d_parent->d_fsdata;
325 struct attribute * attr = filp->f_dentry->d_fsdata;
326 struct sysfs_buffer * buffer = filp->private_data;
328 if (kobj)
329 kobject_put(kobj);
330 module_put(attr->owner);
332 if (buffer) {
333 if (buffer->page)
334 free_page((unsigned long)buffer->page);
335 kfree(buffer);
337 return 0;
340 static struct file_operations sysfs_file_operations = {
341 .read = sysfs_read_file,
342 .write = sysfs_write_file,
343 .llseek = generic_file_llseek,
344 .open = sysfs_open_file,
345 .release = sysfs_release,
349 int sysfs_add_file(struct dentry * dir, const struct attribute * attr)
351 struct dentry * dentry;
352 int error;
354 down(&dir->d_inode->i_sem);
355 dentry = sysfs_get_dentry(dir,attr->name);
356 if (!IS_ERR(dentry)) {
357 error = sysfs_create(dentry,
358 (attr->mode & S_IALLUGO) | S_IFREG,
359 init_file);
360 if (!error)
361 dentry->d_fsdata = (void *)attr;
362 dput(dentry);
363 } else
364 error = PTR_ERR(dentry);
365 up(&dir->d_inode->i_sem);
366 return error;
371 * sysfs_create_file - create an attribute file for an object.
372 * @kobj: object we're creating for.
373 * @attr: atrribute descriptor.
376 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
378 if (kobj && attr)
379 return sysfs_add_file(kobj->dentry,attr);
380 return -EINVAL;
385 * sysfs_update_file - update the modified timestamp on an object attribute.
386 * @kobj: object we're acting for.
387 * @attr: attribute descriptor.
389 * Also call dnotify for the dentry, which lots of userspace programs
390 * use.
392 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
394 struct dentry * dir = kobj->dentry;
395 struct dentry * victim;
396 int res = -ENOENT;
398 down(&dir->d_inode->i_sem);
399 victim = sysfs_get_dentry(dir, attr->name);
400 if (!IS_ERR(victim)) {
401 /* make sure dentry is really there */
402 if (victim->d_inode &&
403 (victim->d_parent->d_inode == dir->d_inode)) {
404 victim->d_inode->i_mtime = CURRENT_TIME;
405 dnotify_parent(victim, DN_MODIFY);
408 * Drop reference from initial sysfs_get_dentry().
410 dput(victim);
411 res = 0;
415 * Drop the reference acquired from sysfs_get_dentry() above.
417 dput(victim);
419 up(&dir->d_inode->i_sem);
421 return res;
426 * sysfs_remove_file - remove an object attribute.
427 * @kobj: object we're acting for.
428 * @attr: attribute descriptor.
430 * Hash the attribute name and kill the victim.
433 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
435 sysfs_hash_and_remove(kobj->dentry,attr->name);
439 EXPORT_SYMBOL(sysfs_create_file);
440 EXPORT_SYMBOL(sysfs_remove_file);
441 EXPORT_SYMBOL(sysfs_update_file);