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/kallsyms.h>
16 #include <linux/slab.h>
17 #include <linux/fsnotify.h>
18 #include <linux/namei.h>
19 #include <linux/poll.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/limits.h>
23 #include <linux/uaccess.h>
24 #include <linux/seq_file.h>
29 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
30 * for each sysfs_dirent with one or more open files.
32 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
33 * protected by sysfs_open_dirent_lock.
35 * filp->private_data points to seq_file whose ->private points to
36 * sysfs_open_file. sysfs_open_files are chained at
37 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
39 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
40 static DEFINE_MUTEX(sysfs_open_file_mutex);
42 struct sysfs_open_dirent {
45 wait_queue_head_t poll;
46 struct list_head files; /* goes through sysfs_open_file.list */
49 struct sysfs_open_file {
50 struct sysfs_dirent *sd;
54 struct list_head list;
57 static bool sysfs_is_bin(struct sysfs_dirent *sd)
59 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
62 static struct sysfs_open_file *sysfs_of(struct file *file)
64 return ((struct seq_file *)file->private_data)->private;
68 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
69 * must be called while holding an active reference.
71 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
73 struct kobject *kobj = sd->s_parent->s_dir.kobj;
75 lockdep_assert_held(sd);
76 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
80 * Reads on sysfs are handled through seq_file, which takes care of hairy
81 * details like buffering and seeking. The following function pipes
82 * sysfs_ops->show() result through seq_file.
84 static int sysfs_seq_show(struct seq_file *sf, void *v)
86 struct sysfs_open_file *of = sf->private;
87 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
88 const struct sysfs_ops *ops;
92 /* acquire buffer and ensure that it's >= PAGE_SIZE */
93 count = seq_get_buf(sf, &buf);
94 if (count < PAGE_SIZE) {
100 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
101 * nests outside active ref and is just to ensure that the ops
102 * aren't called concurrently for the same open file.
104 mutex_lock(&of->mutex);
105 if (!sysfs_get_active(of->sd)) {
106 mutex_unlock(&of->mutex);
110 of->event = atomic_read(&of->sd->s_attr.open->event);
113 * Lookup @ops and invoke show(). Control may reach here via seq
114 * file lseek even if @ops->show() isn't implemented.
116 ops = sysfs_file_ops(of->sd);
118 count = ops->show(kobj, of->sd->s_attr.attr, buf);
122 sysfs_put_active(of->sd);
123 mutex_unlock(&of->mutex);
129 * The code works fine with PAGE_SIZE return but it's likely to
130 * indicate truncated result or overflow in normal use cases.
132 if (count >= (ssize_t)PAGE_SIZE) {
133 print_symbol("fill_read_buffer: %s returned bad count\n",
134 (unsigned long)ops->show);
135 /* Try to struggle along */
136 count = PAGE_SIZE - 1;
138 seq_commit(sf, count);
143 * Read method for bin files. As reading a bin file can have side-effects,
144 * the exact offset and bytes specified in read(2) call should be passed to
145 * the read callback making it difficult to use seq_file. Implement
146 * simplistic custom buffering for bin files.
148 static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
149 size_t bytes, loff_t *off)
151 struct sysfs_open_file *of = sysfs_of(file);
152 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
153 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
154 int size = file_inode(file)->i_size;
155 int count = min_t(size_t, bytes, PAGE_SIZE);
165 if (offs + count > size)
169 buf = kmalloc(count, GFP_KERNEL);
173 /* need of->sd for battr, its parent for kobj */
174 mutex_lock(&of->mutex);
175 if (!sysfs_get_active(of->sd)) {
177 mutex_unlock(&of->mutex);
182 count = battr->read(file, kobj, battr, buf, offs, count);
186 sysfs_put_active(of->sd);
187 mutex_unlock(&of->mutex);
192 if (copy_to_user(userbuf, buf, count)) {
197 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
207 * flush_write_buffer - push buffer to kobject
209 * @buf: data buffer for file
210 * @off: file offset to write to
211 * @count: number of bytes
213 * Get the correct pointers for the kobject and the attribute we're dealing
214 * with, then call the store() method for it with @buf.
216 static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
219 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
223 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
224 * nests outside active ref and is just to ensure that the ops
225 * aren't called concurrently for the same open file.
227 mutex_lock(&of->mutex);
228 if (!sysfs_get_active(of->sd)) {
229 mutex_unlock(&of->mutex);
233 if (sysfs_is_bin(of->sd)) {
234 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
238 rc = battr->write(of->file, kobj, battr, buf, off,
241 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
243 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
246 sysfs_put_active(of->sd);
247 mutex_unlock(&of->mutex);
253 * sysfs_write_file - write an attribute
254 * @file: file pointer
255 * @user_buf: data to write
256 * @count: number of bytes
257 * @ppos: starting offset
259 * Copy data in from userland and pass it to the matching
260 * sysfs_ops->store() by invoking flush_write_buffer().
262 * There is no easy way for us to know if userspace is only doing a partial
263 * write, so we don't support them. We expect the entire buffer to come on
264 * the first write. Hint: if you're writing a value, first read the file,
265 * modify only the the value you're changing, then write entire buffer
268 static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
269 size_t count, loff_t *ppos)
271 struct sysfs_open_file *of = sysfs_of(file);
272 ssize_t len = min_t(size_t, count, PAGE_SIZE);
275 if (sysfs_is_bin(of->sd)) {
276 loff_t size = file_inode(file)->i_size;
280 len = min_t(ssize_t, len, size - *ppos);
286 buf = kmalloc(len + 1, GFP_KERNEL);
290 if (copy_from_user(buf, user_buf, len)) {
294 buf[len] = '\0'; /* guarantee string termination */
296 len = flush_write_buffer(of, buf, *ppos, len);
305 * sysfs_get_open_dirent - get or create sysfs_open_dirent
306 * @sd: target sysfs_dirent
307 * @of: sysfs_open_file for this instance of open
309 * If @sd->s_attr.open exists, increment its reference count;
310 * otherwise, create one. @of is chained to the files list.
313 * Kernel thread context (may sleep).
316 * 0 on success, -errno on failure.
318 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
319 struct sysfs_open_file *of)
321 struct sysfs_open_dirent *od, *new_od = NULL;
324 mutex_lock(&sysfs_open_file_mutex);
325 spin_lock_irq(&sysfs_open_dirent_lock);
327 if (!sd->s_attr.open && new_od) {
328 sd->s_attr.open = new_od;
332 od = sd->s_attr.open;
334 atomic_inc(&od->refcnt);
335 list_add_tail(&of->list, &od->files);
338 spin_unlock_irq(&sysfs_open_dirent_lock);
339 mutex_unlock(&sysfs_open_file_mutex);
346 /* not there, initialize a new one and retry */
347 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
351 atomic_set(&new_od->refcnt, 0);
352 atomic_set(&new_od->event, 1);
353 init_waitqueue_head(&new_od->poll);
354 INIT_LIST_HEAD(&new_od->files);
359 * sysfs_put_open_dirent - put sysfs_open_dirent
360 * @sd: target sysfs_dirent
361 * @of: associated sysfs_open_file
363 * Put @sd->s_attr.open and unlink @of from the files list. If
364 * reference count reaches zero, disassociate and free it.
369 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
370 struct sysfs_open_file *of)
372 struct sysfs_open_dirent *od = sd->s_attr.open;
375 mutex_lock(&sysfs_open_file_mutex);
376 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
379 if (atomic_dec_and_test(&od->refcnt))
380 sd->s_attr.open = NULL;
384 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
385 mutex_unlock(&sysfs_open_file_mutex);
390 static int sysfs_open_file(struct inode *inode, struct file *file)
392 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
393 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
394 struct sysfs_open_file *of;
395 const struct sysfs_ops *ops;
398 /* need attr_sd for attr and ops, its parent for kobj */
399 if (!sysfs_get_active(attr_sd))
402 /* every kobject with an attribute needs a ktype assigned */
403 ops = sysfs_file_ops(attr_sd);
404 if (WARN(!ops, KERN_ERR
405 "missing sysfs attribute operations for kobject: %s\n",
409 /* File needs write support.
410 * The inode's perms must say it's ok,
411 * and we must have a store method.
413 if (file->f_mode & FMODE_WRITE) {
414 if (!(inode->i_mode & S_IWUGO) || !ops->store)
418 /* File needs read support.
419 * The inode's perms must say it's ok, and we there
420 * must be a show method for it.
422 if (file->f_mode & FMODE_READ) {
423 if (!(inode->i_mode & S_IRUGO) || !ops->show)
427 /* allocate a sysfs_open_file for the file */
429 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
433 mutex_init(&of->mutex);
438 * Always instantiate seq_file even if read access is not
439 * implemented or requested. This unifies private data access and
440 * most files are readable anyway.
442 error = single_open(file, sysfs_seq_show, of);
446 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
447 if (file->f_mode & FMODE_WRITE)
448 file->f_mode |= FMODE_PWRITE;
450 /* make sure we have open dirent struct */
451 error = sysfs_get_open_dirent(attr_sd, of);
455 /* open succeeded, put active references */
456 sysfs_put_active(attr_sd);
460 single_release(inode, file);
464 sysfs_put_active(attr_sd);
468 static int sysfs_release(struct inode *inode, struct file *filp)
470 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
471 struct sysfs_open_file *of = sysfs_of(filp);
473 sysfs_put_open_dirent(sd, of);
474 single_release(inode, filp);
480 /* Sysfs attribute files are pollable. The idea is that you read
481 * the content and then you use 'poll' or 'select' to wait for
482 * the content to change. When the content changes (assuming the
483 * manager for the kobject supports notification), poll will
484 * return POLLERR|POLLPRI, and select will return the fd whether
485 * it is waiting for read, write, or exceptions.
486 * Once poll/select indicates that the value has changed, you
487 * need to close and re-open the file, or seek to 0 and read again.
488 * Reminder: this only works for attributes which actively support
489 * it, and it is not possible to test an attribute from userspace
490 * to see if it supports poll (Neither 'poll' nor 'select' return
491 * an appropriate error code). When in doubt, set a suitable timeout value.
493 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
495 struct sysfs_open_file *of = sysfs_of(filp);
496 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
497 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
499 /* need parent for the kobj, grab both */
500 if (!sysfs_get_active(attr_sd))
503 poll_wait(filp, &od->poll, wait);
505 sysfs_put_active(attr_sd);
507 if (of->event != atomic_read(&od->event))
510 return DEFAULT_POLLMASK;
513 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
516 void sysfs_notify_dirent(struct sysfs_dirent *sd)
518 struct sysfs_open_dirent *od;
521 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
523 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
524 od = sd->s_attr.open;
526 atomic_inc(&od->event);
527 wake_up_interruptible(&od->poll);
531 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
533 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
535 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
537 struct sysfs_dirent *sd = k->sd;
539 mutex_lock(&sysfs_mutex);
542 sd = sysfs_find_dirent(sd, dir, NULL);
544 sd = sysfs_find_dirent(sd, attr, NULL);
546 sysfs_notify_dirent(sd);
548 mutex_unlock(&sysfs_mutex);
550 EXPORT_SYMBOL_GPL(sysfs_notify);
552 const struct file_operations sysfs_file_operations = {
554 .write = sysfs_write_file,
556 .open = sysfs_open_file,
557 .release = sysfs_release,
561 const struct file_operations sysfs_bin_operations = {
562 .read = sysfs_bin_read,
563 .write = sysfs_write_file,
564 .llseek = generic_file_llseek,
567 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
568 const struct attribute *attr, int type,
569 umode_t amode, const void *ns)
571 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
572 struct sysfs_addrm_cxt acxt;
573 struct sysfs_dirent *sd;
576 sd = sysfs_new_dirent(attr->name, mode, type);
581 sd->s_attr.attr = (void *)attr;
582 sysfs_dirent_init_lockdep(sd);
584 sysfs_addrm_start(&acxt);
585 rc = sysfs_add_one(&acxt, sd, dir_sd);
586 sysfs_addrm_finish(&acxt);
595 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
598 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
602 * sysfs_create_file_ns - create an attribute file for an object with custom ns
603 * @kobj: object we're creating for
604 * @attr: attribute descriptor
605 * @ns: namespace the new file should belong to
607 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
610 BUG_ON(!kobj || !kobj->sd || !attr);
612 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
616 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
618 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
623 for (i = 0; ptr[i] && !err; i++)
624 err = sysfs_create_file(kobj, ptr[i]);
627 sysfs_remove_file(kobj, ptr[i]);
630 EXPORT_SYMBOL_GPL(sysfs_create_files);
633 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
634 * @kobj: object we're acting for.
635 * @attr: attribute descriptor.
636 * @group: group name.
638 int sysfs_add_file_to_group(struct kobject *kobj,
639 const struct attribute *attr, const char *group)
641 struct sysfs_dirent *dir_sd;
645 dir_sd = sysfs_get_dirent(kobj->sd, group);
647 dir_sd = sysfs_get(kobj->sd);
652 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
657 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
660 * sysfs_chmod_file - update the modified mode value on an object attribute.
661 * @kobj: object we're acting for.
662 * @attr: attribute descriptor.
663 * @mode: file permissions.
666 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
669 struct sysfs_dirent *sd;
670 struct iattr newattrs;
673 mutex_lock(&sysfs_mutex);
676 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
680 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
681 newattrs.ia_valid = ATTR_MODE;
682 rc = sysfs_sd_setattr(sd, &newattrs);
685 mutex_unlock(&sysfs_mutex);
688 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
691 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
692 * @kobj: object we're acting for
693 * @attr: attribute descriptor
694 * @ns: namespace tag of the file to remove
696 * Hash the attribute name and namespace tag and kill the victim.
698 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
701 struct sysfs_dirent *dir_sd = kobj->sd;
703 sysfs_hash_and_remove(dir_sd, attr->name, ns);
705 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
707 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
710 for (i = 0; ptr[i]; i++)
711 sysfs_remove_file(kobj, ptr[i]);
713 EXPORT_SYMBOL_GPL(sysfs_remove_files);
716 * sysfs_remove_file_from_group - remove an attribute file from a group.
717 * @kobj: object we're acting for.
718 * @attr: attribute descriptor.
719 * @group: group name.
721 void sysfs_remove_file_from_group(struct kobject *kobj,
722 const struct attribute *attr, const char *group)
724 struct sysfs_dirent *dir_sd;
727 dir_sd = sysfs_get_dirent(kobj->sd, group);
729 dir_sd = sysfs_get(kobj->sd);
731 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
735 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
737 struct sysfs_schedule_callback_struct {
738 struct list_head workq_list;
739 struct kobject *kobj;
740 void (*func)(void *);
742 struct module *owner;
743 struct work_struct work;
746 static struct workqueue_struct *sysfs_workqueue;
747 static DEFINE_MUTEX(sysfs_workq_mutex);
748 static LIST_HEAD(sysfs_workq);
749 static void sysfs_schedule_callback_work(struct work_struct *work)
751 struct sysfs_schedule_callback_struct *ss = container_of(work,
752 struct sysfs_schedule_callback_struct, work);
754 (ss->func)(ss->data);
755 kobject_put(ss->kobj);
756 module_put(ss->owner);
757 mutex_lock(&sysfs_workq_mutex);
758 list_del(&ss->workq_list);
759 mutex_unlock(&sysfs_workq_mutex);
764 * sysfs_schedule_callback - helper to schedule a callback for a kobject
765 * @kobj: object we're acting for.
766 * @func: callback function to invoke later.
767 * @data: argument to pass to @func.
768 * @owner: module owning the callback code
770 * sysfs attribute methods must not unregister themselves or their parent
771 * kobject (which would amount to the same thing). Attempts to do so will
772 * deadlock, since unregistration is mutually exclusive with driver
775 * Instead methods can call this routine, which will attempt to allocate
776 * and schedule a workqueue request to call back @func with @data as its
777 * argument in the workqueue's process context. @kobj will be pinned
778 * until @func returns.
780 * Returns 0 if the request was submitted, -ENOMEM if storage could not
781 * be allocated, -ENODEV if a reference to @owner isn't available,
782 * -EAGAIN if a callback has already been scheduled for @kobj.
784 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
785 void *data, struct module *owner)
787 struct sysfs_schedule_callback_struct *ss, *tmp;
789 if (!try_module_get(owner))
792 mutex_lock(&sysfs_workq_mutex);
793 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
794 if (ss->kobj == kobj) {
796 mutex_unlock(&sysfs_workq_mutex);
799 mutex_unlock(&sysfs_workq_mutex);
801 if (sysfs_workqueue == NULL) {
802 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
803 if (sysfs_workqueue == NULL) {
809 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
819 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
820 INIT_LIST_HEAD(&ss->workq_list);
821 mutex_lock(&sysfs_workq_mutex);
822 list_add_tail(&ss->workq_list, &sysfs_workq);
823 mutex_unlock(&sysfs_workq_mutex);
824 queue_work(sysfs_workqueue, &ss->work);
827 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);