2 * fs/sysfs/file.c - sysfs regular (text) file implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
13 #include <linux/module.h>
14 #include <linux/kobject.h>
15 #include <linux/namei.h>
16 #include <linux/poll.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <asm/uaccess.h>
23 #define to_sattr(a) container_of(a,struct subsys_attribute, attr)
26 * Subsystem file operations.
27 * These operations allow subsystems to have files that can be
31 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
33 struct kset *kset = to_kset(kobj);
34 struct subsys_attribute * sattr = to_sattr(attr);
38 ret = sattr->show(kset, page);
43 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
44 const char * page, size_t count)
46 struct kset *kset = to_kset(kobj);
47 struct subsys_attribute * sattr = to_sattr(attr);
51 ret = sattr->store(kset, page, count);
55 static struct sysfs_ops subsys_sysfs_ops = {
56 .show = subsys_attr_show,
57 .store = subsys_attr_store,
61 * There's one sysfs_buffer for each open file and one
62 * sysfs_open_dirent for each sysfs_dirent with one or more open
65 * filp->private_data points to sysfs_buffer and
66 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
67 * is protected by sysfs_open_dirent_lock.
69 static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED;
71 struct sysfs_open_dirent {
74 wait_queue_head_t poll;
75 struct list_head buffers; /* goes through sysfs_buffer.list */
82 struct sysfs_ops * ops;
86 struct list_head list;
90 * fill_read_buffer - allocate and fill buffer from object.
91 * @dentry: dentry pointer.
92 * @buffer: data buffer for file.
94 * Allocate @buffer->page, if it hasn't been already, then call the
95 * kobject's show() method to fill the buffer with this attribute's
97 * This is called only once, on the file's first read unless an error
100 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
102 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
103 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
104 struct sysfs_ops * ops = buffer->ops;
109 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
113 /* need attr_sd for attr and ops, its parent for kobj */
114 if (!sysfs_get_active_two(attr_sd))
117 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
118 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
120 sysfs_put_active_two(attr_sd);
123 * The code works fine with PAGE_SIZE return but it's likely to
124 * indicate truncated result or overflow in normal use cases.
126 BUG_ON(count >= (ssize_t)PAGE_SIZE);
128 buffer->needs_read_fill = 0;
129 buffer->count = count;
137 * sysfs_read_file - read an attribute.
138 * @file: file pointer.
139 * @buf: buffer to fill.
140 * @count: number of bytes to read.
141 * @ppos: starting offset in file.
143 * Userspace wants to read an attribute file. The attribute descriptor
144 * is in the file's ->d_fsdata. The target object is in the directory's
147 * We call fill_read_buffer() to allocate and fill the buffer from the
148 * object's show() method exactly once (if the read is happening from
149 * the beginning of the file). That should fill the entire buffer with
150 * all the data the object has to offer for that attribute.
151 * We then call flush_read_buffer() to copy the buffer to userspace
152 * in the increments specified.
156 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
158 struct sysfs_buffer * buffer = file->private_data;
161 mutex_lock(&buffer->mutex);
162 if (buffer->needs_read_fill) {
163 retval = fill_read_buffer(file->f_path.dentry,buffer);
167 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
168 __FUNCTION__, count, *ppos, buffer->page);
169 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
172 mutex_unlock(&buffer->mutex);
177 * fill_write_buffer - copy buffer from userspace.
178 * @buffer: data buffer for file.
179 * @buf: data from user.
180 * @count: number of bytes in @userbuf.
182 * Allocate @buffer->page if it hasn't been already, then
183 * copy the user-supplied buffer into it.
187 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
192 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
196 if (count >= PAGE_SIZE)
197 count = PAGE_SIZE - 1;
198 error = copy_from_user(buffer->page,buf,count);
199 buffer->needs_read_fill = 1;
200 /* if buf is assumed to contain a string, terminate it by \0,
201 so e.g. sscanf() can scan the string easily */
202 buffer->page[count] = 0;
203 return error ? -EFAULT : count;
208 * flush_write_buffer - push buffer to kobject.
209 * @dentry: dentry to the attribute
210 * @buffer: data buffer for file.
211 * @count: number of bytes
213 * Get the correct pointers for the kobject and the attribute we're
214 * dealing with, then call the store() method for the attribute,
215 * passing the buffer that we acquired in fill_write_buffer().
219 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
221 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
222 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
223 struct sysfs_ops * ops = buffer->ops;
226 /* need attr_sd for attr and ops, its parent for kobj */
227 if (!sysfs_get_active_two(attr_sd))
230 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
232 sysfs_put_active_two(attr_sd);
239 * sysfs_write_file - write an attribute.
240 * @file: file pointer
241 * @buf: data to write
242 * @count: number of bytes
243 * @ppos: starting offset
245 * Similar to sysfs_read_file(), though working in the opposite direction.
246 * We allocate and fill the data from the user in fill_write_buffer(),
247 * then push it to the kobject in flush_write_buffer().
248 * There is no easy way for us to know if userspace is only doing a partial
249 * write, so we don't support them. We expect the entire buffer to come
250 * on the first write.
251 * Hint: if you're writing a value, first read the file, modify only the
252 * the value you're changing, then write entire buffer back.
256 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
258 struct sysfs_buffer * buffer = file->private_data;
261 mutex_lock(&buffer->mutex);
262 len = fill_write_buffer(buffer, buf, count);
264 len = flush_write_buffer(file->f_path.dentry, buffer, len);
267 mutex_unlock(&buffer->mutex);
272 * sysfs_get_open_dirent - get or create sysfs_open_dirent
273 * @sd: target sysfs_dirent
274 * @buffer: sysfs_buffer for this instance of open
276 * If @sd->s_attr.open exists, increment its reference count;
277 * otherwise, create one. @buffer is chained to the buffers
281 * Kernel thread context (may sleep).
284 * 0 on success, -errno on failure.
286 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
287 struct sysfs_buffer *buffer)
289 struct sysfs_open_dirent *od, *new_od = NULL;
292 spin_lock(&sysfs_open_dirent_lock);
294 if (!sd->s_attr.open && new_od) {
295 sd->s_attr.open = new_od;
299 od = sd->s_attr.open;
301 atomic_inc(&od->refcnt);
302 list_add_tail(&buffer->list, &od->buffers);
305 spin_unlock(&sysfs_open_dirent_lock);
312 /* not there, initialize a new one and retry */
313 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
317 atomic_set(&new_od->refcnt, 0);
318 atomic_set(&new_od->event, 1);
319 init_waitqueue_head(&new_od->poll);
320 INIT_LIST_HEAD(&new_od->buffers);
325 * sysfs_put_open_dirent - put sysfs_open_dirent
326 * @sd: target sysfs_dirent
327 * @buffer: associated sysfs_buffer
329 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
330 * If reference count reaches zero, disassociate and free it.
335 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
336 struct sysfs_buffer *buffer)
338 struct sysfs_open_dirent *od = sd->s_attr.open;
340 spin_lock(&sysfs_open_dirent_lock);
342 list_del(&buffer->list);
343 if (atomic_dec_and_test(&od->refcnt))
344 sd->s_attr.open = NULL;
348 spin_unlock(&sysfs_open_dirent_lock);
353 static int sysfs_open_file(struct inode *inode, struct file *file)
355 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
356 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
357 struct sysfs_buffer * buffer;
358 struct sysfs_ops * ops = NULL;
361 /* need attr_sd for attr and ops, its parent for kobj */
362 if (!sysfs_get_active_two(attr_sd))
365 /* if the kobject has no ktype, then we assume that it is a subsystem
366 * itself, and use ops for it.
368 if (kobj->kset && kobj->kset->ktype)
369 ops = kobj->kset->ktype->sysfs_ops;
370 else if (kobj->ktype)
371 ops = kobj->ktype->sysfs_ops;
373 ops = &subsys_sysfs_ops;
377 /* No sysfs operations, either from having no subsystem,
378 * or the subsystem have no operations.
383 /* File needs write support.
384 * The inode's perms must say it's ok,
385 * and we must have a store method.
387 if (file->f_mode & FMODE_WRITE) {
388 if (!(inode->i_mode & S_IWUGO) || !ops->store)
392 /* File needs read support.
393 * The inode's perms must say it's ok, and we there
394 * must be a show method for it.
396 if (file->f_mode & FMODE_READ) {
397 if (!(inode->i_mode & S_IRUGO) || !ops->show)
401 /* No error? Great, allocate a buffer for the file, and store it
402 * it in file->private_data for easy access.
405 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
409 mutex_init(&buffer->mutex);
410 buffer->needs_read_fill = 1;
412 file->private_data = buffer;
414 /* make sure we have open dirent struct */
415 error = sysfs_get_open_dirent(attr_sd, buffer);
419 /* open succeeded, put active references */
420 sysfs_put_active_two(attr_sd);
426 sysfs_put_active_two(attr_sd);
430 static int sysfs_release(struct inode *inode, struct file *filp)
432 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
433 struct sysfs_buffer *buffer = filp->private_data;
435 sysfs_put_open_dirent(sd, buffer);
438 free_page((unsigned long)buffer->page);
444 /* Sysfs attribute files are pollable. The idea is that you read
445 * the content and then you use 'poll' or 'select' to wait for
446 * the content to change. When the content changes (assuming the
447 * manager for the kobject supports notification), poll will
448 * return POLLERR|POLLPRI, and select will return the fd whether
449 * it is waiting for read, write, or exceptions.
450 * Once poll/select indicates that the value has changed, you
451 * need to close and re-open the file, as simply seeking and reading
452 * again will not get new data, or reset the state of 'poll'.
453 * Reminder: this only works for attributes which actively support
454 * it, and it is not possible to test an attribute from userspace
455 * to see if it supports poll (Neither 'poll' nor 'select' return
456 * an appropriate error code). When in doubt, set a suitable timeout value.
458 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
460 struct sysfs_buffer * buffer = filp->private_data;
461 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
462 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
464 /* need parent for the kobj, grab both */
465 if (!sysfs_get_active_two(attr_sd))
468 poll_wait(filp, &od->poll, wait);
470 sysfs_put_active_two(attr_sd);
472 if (buffer->event != atomic_read(&od->event))
478 buffer->needs_read_fill = 1;
479 return POLLERR|POLLPRI;
482 void sysfs_notify(struct kobject *k, char *dir, char *attr)
484 struct sysfs_dirent *sd = k->sd;
486 mutex_lock(&sysfs_mutex);
489 sd = sysfs_find_dirent(sd, dir);
491 sd = sysfs_find_dirent(sd, attr);
493 struct sysfs_open_dirent *od;
495 spin_lock(&sysfs_open_dirent_lock);
497 od = sd->s_attr.open;
499 atomic_inc(&od->event);
500 wake_up_interruptible(&od->poll);
503 spin_unlock(&sysfs_open_dirent_lock);
506 mutex_unlock(&sysfs_mutex);
508 EXPORT_SYMBOL_GPL(sysfs_notify);
510 const struct file_operations sysfs_file_operations = {
511 .read = sysfs_read_file,
512 .write = sysfs_write_file,
513 .llseek = generic_file_llseek,
514 .open = sysfs_open_file,
515 .release = sysfs_release,
520 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
523 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
524 struct sysfs_addrm_cxt acxt;
525 struct sysfs_dirent *sd;
528 sd = sysfs_new_dirent(attr->name, mode, type);
531 sd->s_attr.attr = (void *)attr;
533 sysfs_addrm_start(&acxt, dir_sd);
534 rc = sysfs_add_one(&acxt, sd);
535 sysfs_addrm_finish(&acxt);
545 * sysfs_create_file - create an attribute file for an object.
546 * @kobj: object we're creating for.
547 * @attr: attribute descriptor.
550 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
552 BUG_ON(!kobj || !kobj->sd || !attr);
554 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
560 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
561 * @kobj: object we're acting for.
562 * @attr: attribute descriptor.
563 * @group: group name.
565 int sysfs_add_file_to_group(struct kobject *kobj,
566 const struct attribute *attr, const char *group)
568 struct sysfs_dirent *dir_sd;
571 dir_sd = sysfs_get_dirent(kobj->sd, group);
575 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
580 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
583 * sysfs_chmod_file - update the modified mode value on an object attribute.
584 * @kobj: object we're acting for.
585 * @attr: attribute descriptor.
586 * @mode: file permissions.
589 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
591 struct sysfs_dirent *victim_sd = NULL;
592 struct dentry *victim = NULL;
593 struct inode * inode;
594 struct iattr newattrs;
598 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
602 mutex_lock(&sysfs_rename_mutex);
603 victim = sysfs_get_dentry(victim_sd);
604 mutex_unlock(&sysfs_rename_mutex);
605 if (IS_ERR(victim)) {
606 rc = PTR_ERR(victim);
611 inode = victim->d_inode;
613 mutex_lock(&inode->i_mutex);
615 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
616 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
617 rc = notify_change(victim, &newattrs);
620 mutex_lock(&sysfs_mutex);
621 victim_sd->s_mode = newattrs.ia_mode;
622 mutex_unlock(&sysfs_mutex);
625 mutex_unlock(&inode->i_mutex);
628 sysfs_put(victim_sd);
631 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
635 * sysfs_remove_file - remove an object attribute.
636 * @kobj: object we're acting for.
637 * @attr: attribute descriptor.
639 * Hash the attribute name and kill the victim.
642 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
644 sysfs_hash_and_remove(kobj->sd, attr->name);
649 * sysfs_remove_file_from_group - remove an attribute file from a group.
650 * @kobj: object we're acting for.
651 * @attr: attribute descriptor.
652 * @group: group name.
654 void sysfs_remove_file_from_group(struct kobject *kobj,
655 const struct attribute *attr, const char *group)
657 struct sysfs_dirent *dir_sd;
659 dir_sd = sysfs_get_dirent(kobj->sd, group);
661 sysfs_hash_and_remove(dir_sd, attr->name);
665 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
667 struct sysfs_schedule_callback_struct {
668 struct kobject *kobj;
669 void (*func)(void *);
671 struct module *owner;
672 struct work_struct work;
675 static void sysfs_schedule_callback_work(struct work_struct *work)
677 struct sysfs_schedule_callback_struct *ss = container_of(work,
678 struct sysfs_schedule_callback_struct, work);
680 (ss->func)(ss->data);
681 kobject_put(ss->kobj);
682 module_put(ss->owner);
687 * sysfs_schedule_callback - helper to schedule a callback for a kobject
688 * @kobj: object we're acting for.
689 * @func: callback function to invoke later.
690 * @data: argument to pass to @func.
691 * @owner: module owning the callback code
693 * sysfs attribute methods must not unregister themselves or their parent
694 * kobject (which would amount to the same thing). Attempts to do so will
695 * deadlock, since unregistration is mutually exclusive with driver
698 * Instead methods can call this routine, which will attempt to allocate
699 * and schedule a workqueue request to call back @func with @data as its
700 * argument in the workqueue's process context. @kobj will be pinned
701 * until @func returns.
703 * Returns 0 if the request was submitted, -ENOMEM if storage could not
704 * be allocated, -ENODEV if a reference to @owner isn't available.
706 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
707 void *data, struct module *owner)
709 struct sysfs_schedule_callback_struct *ss;
711 if (!try_module_get(owner))
713 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
723 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
724 schedule_work(&ss->work);
727 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
730 EXPORT_SYMBOL_GPL(sysfs_create_file);
731 EXPORT_SYMBOL_GPL(sysfs_remove_file);