[POWERPC] PS3: System-bus rework
[linux-2.6/mini2440.git] / fs / sysfs / file.c
blobb502c7197ec063777d90af4c49f038e95acd0c9e
1 /*
2 * file.c - operations for regular (text) files.
3 */
5 #include <linux/module.h>
6 #include <linux/fsnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/list.h>
11 #include <asm/uaccess.h>
12 #include <asm/semaphore.h>
14 #include "sysfs.h"
16 #define to_sattr(a) container_of(a,struct subsys_attribute, attr)
19 * Subsystem file operations.
20 * These operations allow subsystems to have files that can be
21 * read/written.
23 static ssize_t
24 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
26 struct kset *kset = to_kset(kobj);
27 struct subsys_attribute * sattr = to_sattr(attr);
28 ssize_t ret = -EIO;
30 if (sattr->show)
31 ret = sattr->show(kset, page);
32 return ret;
35 static ssize_t
36 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
37 const char * page, size_t count)
39 struct kset *kset = to_kset(kobj);
40 struct subsys_attribute * sattr = to_sattr(attr);
41 ssize_t ret = -EIO;
43 if (sattr->store)
44 ret = sattr->store(kset, page, count);
45 return ret;
48 static struct sysfs_ops subsys_sysfs_ops = {
49 .show = subsys_attr_show,
50 .store = subsys_attr_store,
53 /**
54 * add_to_collection - add buffer to a collection
55 * @buffer: buffer to be added
56 * @node: inode of set to add to
59 static inline void
60 add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
62 struct sysfs_buffer_collection *set = node->i_private;
64 mutex_lock(&node->i_mutex);
65 list_add(&buffer->associates, &set->associates);
66 mutex_unlock(&node->i_mutex);
69 static inline void
70 remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
72 mutex_lock(&node->i_mutex);
73 list_del(&buffer->associates);
74 mutex_unlock(&node->i_mutex);
77 /**
78 * fill_read_buffer - allocate and fill buffer from object.
79 * @dentry: dentry pointer.
80 * @buffer: data buffer for file.
82 * Allocate @buffer->page, if it hasn't been already, then call the
83 * kobject's show() method to fill the buffer with this attribute's
84 * data.
85 * This is called only once, on the file's first read unless an error
86 * is returned.
88 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
90 struct sysfs_dirent * sd = dentry->d_fsdata;
91 struct attribute * attr = to_attr(dentry);
92 struct kobject * kobj = to_kobj(dentry->d_parent);
93 struct sysfs_ops * ops = buffer->ops;
94 int ret = 0;
95 ssize_t count;
97 if (!buffer->page)
98 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
99 if (!buffer->page)
100 return -ENOMEM;
102 buffer->event = atomic_read(&sd->s_event);
103 count = ops->show(kobj,attr,buffer->page);
104 BUG_ON(count > (ssize_t)PAGE_SIZE);
105 if (count >= 0) {
106 buffer->needs_read_fill = 0;
107 buffer->count = count;
108 } else {
109 ret = count;
111 return ret;
115 * sysfs_read_file - read an attribute.
116 * @file: file pointer.
117 * @buf: buffer to fill.
118 * @count: number of bytes to read.
119 * @ppos: starting offset in file.
121 * Userspace wants to read an attribute file. The attribute descriptor
122 * is in the file's ->d_fsdata. The target object is in the directory's
123 * ->d_fsdata.
125 * We call fill_read_buffer() to allocate and fill the buffer from the
126 * object's show() method exactly once (if the read is happening from
127 * the beginning of the file). That should fill the entire buffer with
128 * all the data the object has to offer for that attribute.
129 * We then call flush_read_buffer() to copy the buffer to userspace
130 * in the increments specified.
133 static ssize_t
134 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
136 struct sysfs_buffer * buffer = file->private_data;
137 ssize_t retval = 0;
139 down(&buffer->sem);
140 if (buffer->needs_read_fill) {
141 if (buffer->orphaned)
142 retval = -ENODEV;
143 else
144 retval = fill_read_buffer(file->f_path.dentry,buffer);
145 if (retval)
146 goto out;
148 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
149 __FUNCTION__, count, *ppos, buffer->page);
150 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
151 buffer->count);
152 out:
153 up(&buffer->sem);
154 return retval;
158 * fill_write_buffer - copy buffer from userspace.
159 * @buffer: data buffer for file.
160 * @buf: data from user.
161 * @count: number of bytes in @userbuf.
163 * Allocate @buffer->page if it hasn't been already, then
164 * copy the user-supplied buffer into it.
167 static int
168 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
170 int error;
172 if (!buffer->page)
173 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
174 if (!buffer->page)
175 return -ENOMEM;
177 if (count >= PAGE_SIZE)
178 count = PAGE_SIZE - 1;
179 error = copy_from_user(buffer->page,buf,count);
180 buffer->needs_read_fill = 1;
181 /* if buf is assumed to contain a string, terminate it by \0,
182 so e.g. sscanf() can scan the string easily */
183 buffer->page[count] = 0;
184 return error ? -EFAULT : count;
189 * flush_write_buffer - push buffer to kobject.
190 * @dentry: dentry to the attribute
191 * @buffer: data buffer for file.
192 * @count: number of bytes
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 dentry * dentry, struct sysfs_buffer * buffer, size_t count)
202 struct attribute * attr = to_attr(dentry);
203 struct kobject * kobj = to_kobj(dentry->d_parent);
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;
231 ssize_t len;
233 down(&buffer->sem);
234 if (buffer->orphaned) {
235 len = -ENODEV;
236 goto out;
238 len = fill_write_buffer(buffer, buf, count);
239 if (len > 0)
240 len = flush_write_buffer(file->f_path.dentry, buffer, len);
241 if (len > 0)
242 *ppos += len;
243 out:
244 up(&buffer->sem);
245 return len;
248 static int sysfs_open_file(struct inode *inode, struct file *file)
250 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
251 struct attribute * attr = to_attr(file->f_path.dentry);
252 struct sysfs_buffer_collection *set;
253 struct sysfs_buffer * buffer;
254 struct sysfs_ops * ops = NULL;
255 int error = 0;
257 if (!kobj || !attr)
258 goto Einval;
260 /* Grab the module reference for this attribute if we have one */
261 if (!try_module_get(attr->owner)) {
262 error = -ENODEV;
263 goto Done;
266 /* if the kobject has no ktype, then we assume that it is a subsystem
267 * itself, and use ops for it.
269 if (kobj->kset && kobj->kset->ktype)
270 ops = kobj->kset->ktype->sysfs_ops;
271 else if (kobj->ktype)
272 ops = kobj->ktype->sysfs_ops;
273 else
274 ops = &subsys_sysfs_ops;
276 /* No sysfs operations, either from having no subsystem,
277 * or the subsystem have no operations.
279 if (!ops)
280 goto Eaccess;
282 /* make sure we have a collection to add our buffers to */
283 mutex_lock(&inode->i_mutex);
284 if (!(set = inode->i_private)) {
285 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
286 error = -ENOMEM;
287 goto Done;
288 } else {
289 INIT_LIST_HEAD(&set->associates);
292 mutex_unlock(&inode->i_mutex);
294 /* File needs write support.
295 * The inode's perms must say it's ok,
296 * and we must have a store method.
298 if (file->f_mode & FMODE_WRITE) {
300 if (!(inode->i_mode & S_IWUGO) || !ops->store)
301 goto Eaccess;
305 /* File needs read support.
306 * The inode's perms must say it's ok, and we there
307 * must be a show method for it.
309 if (file->f_mode & FMODE_READ) {
310 if (!(inode->i_mode & S_IRUGO) || !ops->show)
311 goto Eaccess;
314 /* No error? Great, allocate a buffer for the file, and store it
315 * it in file->private_data for easy access.
317 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
318 if (buffer) {
319 INIT_LIST_HEAD(&buffer->associates);
320 init_MUTEX(&buffer->sem);
321 buffer->needs_read_fill = 1;
322 buffer->ops = ops;
323 add_to_collection(buffer, inode);
324 file->private_data = buffer;
325 } else
326 error = -ENOMEM;
327 goto Done;
329 Einval:
330 error = -EINVAL;
331 goto Done;
332 Eaccess:
333 error = -EACCES;
334 module_put(attr->owner);
335 Done:
336 if (error)
337 kobject_put(kobj);
338 return error;
341 static int sysfs_release(struct inode * inode, struct file * filp)
343 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
344 struct attribute * attr = to_attr(filp->f_path.dentry);
345 struct module * owner = attr->owner;
346 struct sysfs_buffer * buffer = filp->private_data;
348 if (buffer)
349 remove_from_collection(buffer, inode);
350 kobject_put(kobj);
351 /* After this point, attr should not be accessed. */
352 module_put(owner);
354 if (buffer) {
355 if (buffer->page)
356 free_page((unsigned long)buffer->page);
357 kfree(buffer);
359 return 0;
362 /* Sysfs attribute files are pollable. The idea is that you read
363 * the content and then you use 'poll' or 'select' to wait for
364 * the content to change. When the content changes (assuming the
365 * manager for the kobject supports notification), poll will
366 * return POLLERR|POLLPRI, and select will return the fd whether
367 * it is waiting for read, write, or exceptions.
368 * Once poll/select indicates that the value has changed, you
369 * need to close and re-open the file, as simply seeking and reading
370 * again will not get new data, or reset the state of 'poll'.
371 * Reminder: this only works for attributes which actively support
372 * it, and it is not possible to test an attribute from userspace
373 * to see if it supports poll (Nether 'poll' or 'select' return
374 * an appropriate error code). When in doubt, set a suitable timeout value.
376 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
378 struct sysfs_buffer * buffer = filp->private_data;
379 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
380 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
381 int res = 0;
383 poll_wait(filp, &kobj->poll, wait);
385 if (buffer->event != atomic_read(&sd->s_event)) {
386 res = POLLERR|POLLPRI;
387 buffer->needs_read_fill = 1;
390 return res;
394 static struct dentry *step_down(struct dentry *dir, const char * name)
396 struct dentry * de;
398 if (dir == NULL || dir->d_inode == NULL)
399 return NULL;
401 mutex_lock(&dir->d_inode->i_mutex);
402 de = lookup_one_len(name, dir, strlen(name));
403 mutex_unlock(&dir->d_inode->i_mutex);
404 dput(dir);
405 if (IS_ERR(de))
406 return NULL;
407 if (de->d_inode == NULL) {
408 dput(de);
409 return NULL;
411 return de;
414 void sysfs_notify(struct kobject * k, char *dir, char *attr)
416 struct dentry *de = k->dentry;
417 if (de)
418 dget(de);
419 if (de && dir)
420 de = step_down(de, dir);
421 if (de && attr)
422 de = step_down(de, attr);
423 if (de) {
424 struct sysfs_dirent * sd = de->d_fsdata;
425 if (sd)
426 atomic_inc(&sd->s_event);
427 wake_up_interruptible(&k->poll);
428 dput(de);
431 EXPORT_SYMBOL_GPL(sysfs_notify);
433 const struct file_operations sysfs_file_operations = {
434 .read = sysfs_read_file,
435 .write = sysfs_write_file,
436 .llseek = generic_file_llseek,
437 .open = sysfs_open_file,
438 .release = sysfs_release,
439 .poll = sysfs_poll,
443 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
445 struct sysfs_dirent * parent_sd = dir->d_fsdata;
446 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
447 int error = -EEXIST;
449 mutex_lock(&dir->d_inode->i_mutex);
450 if (!sysfs_dirent_exist(parent_sd, attr->name))
451 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
452 mode, type);
453 mutex_unlock(&dir->d_inode->i_mutex);
455 return error;
460 * sysfs_create_file - create an attribute file for an object.
461 * @kobj: object we're creating for.
462 * @attr: atrribute descriptor.
465 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
467 BUG_ON(!kobj || !kobj->dentry || !attr);
469 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
475 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
476 * @kobj: object we're acting for.
477 * @attr: attribute descriptor.
478 * @group: group name.
480 int sysfs_add_file_to_group(struct kobject *kobj,
481 const struct attribute *attr, const char *group)
483 struct dentry *dir;
484 int error;
486 dir = lookup_one_len(group, kobj->dentry, strlen(group));
487 if (IS_ERR(dir))
488 error = PTR_ERR(dir);
489 else {
490 error = sysfs_add_file(dir, attr, SYSFS_KOBJ_ATTR);
491 dput(dir);
493 return error;
495 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
499 * sysfs_update_file - update the modified timestamp on an object attribute.
500 * @kobj: object we're acting for.
501 * @attr: attribute descriptor.
503 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
505 struct dentry * dir = kobj->dentry;
506 struct dentry * victim;
507 int res = -ENOENT;
509 mutex_lock(&dir->d_inode->i_mutex);
510 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
511 if (!IS_ERR(victim)) {
512 /* make sure dentry is really there */
513 if (victim->d_inode &&
514 (victim->d_parent->d_inode == dir->d_inode)) {
515 victim->d_inode->i_mtime = CURRENT_TIME;
516 fsnotify_modify(victim);
517 res = 0;
518 } else
519 d_drop(victim);
522 * Drop the reference acquired from lookup_one_len() above.
524 dput(victim);
526 mutex_unlock(&dir->d_inode->i_mutex);
528 return res;
533 * sysfs_chmod_file - update the modified mode value on an object attribute.
534 * @kobj: object we're acting for.
535 * @attr: attribute descriptor.
536 * @mode: file permissions.
539 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
541 struct dentry *dir = kobj->dentry;
542 struct dentry *victim;
543 struct inode * inode;
544 struct iattr newattrs;
545 int res = -ENOENT;
547 mutex_lock(&dir->d_inode->i_mutex);
548 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
549 if (!IS_ERR(victim)) {
550 if (victim->d_inode &&
551 (victim->d_parent->d_inode == dir->d_inode)) {
552 inode = victim->d_inode;
553 mutex_lock(&inode->i_mutex);
554 newattrs.ia_mode = (mode & S_IALLUGO) |
555 (inode->i_mode & ~S_IALLUGO);
556 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
557 res = notify_change(victim, &newattrs);
558 mutex_unlock(&inode->i_mutex);
560 dput(victim);
562 mutex_unlock(&dir->d_inode->i_mutex);
564 return res;
566 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
570 * sysfs_remove_file - remove an object attribute.
571 * @kobj: object we're acting for.
572 * @attr: attribute descriptor.
574 * Hash the attribute name and kill the victim.
577 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
579 sysfs_hash_and_remove(kobj->dentry, attr->name);
584 * sysfs_remove_file_from_group - remove an attribute file from a group.
585 * @kobj: object we're acting for.
586 * @attr: attribute descriptor.
587 * @group: group name.
589 void sysfs_remove_file_from_group(struct kobject *kobj,
590 const struct attribute *attr, const char *group)
592 struct dentry *dir;
594 dir = lookup_one_len(group, kobj->dentry, strlen(group));
595 if (!IS_ERR(dir)) {
596 sysfs_hash_and_remove(dir, attr->name);
597 dput(dir);
600 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
602 struct sysfs_schedule_callback_struct {
603 struct kobject *kobj;
604 void (*func)(void *);
605 void *data;
606 struct module *owner;
607 struct work_struct work;
610 static void sysfs_schedule_callback_work(struct work_struct *work)
612 struct sysfs_schedule_callback_struct *ss = container_of(work,
613 struct sysfs_schedule_callback_struct, work);
615 (ss->func)(ss->data);
616 kobject_put(ss->kobj);
617 module_put(ss->owner);
618 kfree(ss);
622 * sysfs_schedule_callback - helper to schedule a callback for a kobject
623 * @kobj: object we're acting for.
624 * @func: callback function to invoke later.
625 * @data: argument to pass to @func.
626 * @owner: module owning the callback code
628 * sysfs attribute methods must not unregister themselves or their parent
629 * kobject (which would amount to the same thing). Attempts to do so will
630 * deadlock, since unregistration is mutually exclusive with driver
631 * callbacks.
633 * Instead methods can call this routine, which will attempt to allocate
634 * and schedule a workqueue request to call back @func with @data as its
635 * argument in the workqueue's process context. @kobj will be pinned
636 * until @func returns.
638 * Returns 0 if the request was submitted, -ENOMEM if storage could not
639 * be allocated, -ENODEV if a reference to @owner isn't available.
641 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
642 void *data, struct module *owner)
644 struct sysfs_schedule_callback_struct *ss;
646 if (!try_module_get(owner))
647 return -ENODEV;
648 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
649 if (!ss) {
650 module_put(owner);
651 return -ENOMEM;
653 kobject_get(kobj);
654 ss->kobj = kobj;
655 ss->func = func;
656 ss->data = data;
657 ss->owner = owner;
658 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
659 schedule_work(&ss->work);
660 return 0;
662 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
665 EXPORT_SYMBOL_GPL(sysfs_create_file);
666 EXPORT_SYMBOL_GPL(sysfs_remove_file);
667 EXPORT_SYMBOL_GPL(sysfs_update_file);