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>
28 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
29 * for each sysfs_dirent with one or more open files.
31 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
32 * protected by sysfs_open_dirent_lock.
34 * filp->private_data points to sysfs_open_file which is chained at
35 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
37 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
38 static DEFINE_MUTEX(sysfs_open_file_mutex);
40 struct sysfs_open_dirent {
43 wait_queue_head_t poll;
44 struct list_head files; /* goes through sysfs_open_file.list */
47 struct sysfs_open_file {
52 struct list_head list;
56 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
57 * must be called while holding an active reference.
59 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
61 struct kobject *kobj = sd->s_parent->s_dir.kobj;
63 lockdep_assert_held(sd);
64 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
68 * fill_read_buffer - allocate and fill buffer from object.
69 * @dentry: dentry pointer.
70 * @buffer: data buffer for file.
72 * Allocate @buffer->page, if it hasn't been already, then call the
73 * kobject's show() method to fill the buffer with this attribute's
75 * This is called only once, on the file's first read unless an error
78 static int fill_read_buffer(struct dentry *dentry, struct sysfs_open_file *of)
80 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
81 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
82 const struct sysfs_ops *ops;
87 of->page = (char *) get_zeroed_page(GFP_KERNEL);
91 /* need attr_sd for attr and ops, its parent for kobj */
92 if (!sysfs_get_active(attr_sd))
95 of->event = atomic_read(&attr_sd->s_attr.open->event);
97 ops = sysfs_file_ops(attr_sd);
98 count = ops->show(kobj, attr_sd->s_attr.attr, of->page);
100 sysfs_put_active(attr_sd);
103 * The code works fine with PAGE_SIZE return but it's likely to
104 * indicate truncated result or overflow in normal use cases.
106 if (count >= (ssize_t)PAGE_SIZE) {
107 print_symbol("fill_read_buffer: %s returned bad count\n",
108 (unsigned long)ops->show);
109 /* Try to struggle along */
110 count = PAGE_SIZE - 1;
120 * sysfs_read_file - read an attribute.
121 * @file: file pointer.
122 * @buf: buffer to fill.
123 * @count: number of bytes to read.
124 * @ppos: starting offset in file.
126 * Userspace wants to read an attribute file. The attribute descriptor
127 * is in the file's ->d_fsdata. The target object is in the directory's
130 * We call fill_read_buffer() to allocate and fill the buffer from the
131 * object's show() method exactly once (if the read is happening from
132 * the beginning of the file). That should fill the entire buffer with
133 * all the data the object has to offer for that attribute.
134 * We then call flush_read_buffer() to copy the buffer to userspace
135 * in the increments specified.
139 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
141 struct sysfs_open_file *of = file->private_data;
144 mutex_lock(&of->mutex);
146 * Fill on zero offset and the first read so that silly things like
147 * "dd bs=1 skip=N" can work on sysfs files.
149 if (*ppos == 0 || !of->page) {
150 retval = fill_read_buffer(file->f_path.dentry, of);
154 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
155 __func__, count, *ppos, of->page);
156 retval = simple_read_from_buffer(buf, count, ppos, of->page, of->count);
158 mutex_unlock(&of->mutex);
163 * fill_write_buffer - copy buffer from userspace.
164 * @of: open file struct.
165 * @buf: data from user.
166 * @count: number of bytes in @userbuf.
168 * Allocate @of->page if it hasn't been already, then copy the
169 * user-supplied buffer into it.
171 static int fill_write_buffer(struct sysfs_open_file *of,
172 const char __user *buf, size_t count)
177 of->page = (char *)get_zeroed_page(GFP_KERNEL);
181 if (count >= PAGE_SIZE)
182 count = PAGE_SIZE - 1;
183 error = copy_from_user(of->page, buf, count);
186 * If buf is assumed to contain a string, terminate it by \0, so
187 * e.g. sscanf() can scan the string easily.
190 return error ? -EFAULT : count;
194 * flush_write_buffer - push buffer to kobject.
195 * @dentry: dentry to the attribute
197 * @count: number of bytes
199 * Get the correct pointers for the kobject and the attribute we're
200 * dealing with, then call the store() method for the attribute,
201 * passing the buffer that we acquired in fill_write_buffer().
203 static int flush_write_buffer(struct dentry *dentry,
204 struct sysfs_open_file *of, size_t count)
206 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
207 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
208 const struct sysfs_ops *ops;
211 /* need attr_sd for attr and ops, its parent for kobj */
212 if (!sysfs_get_active(attr_sd))
215 ops = sysfs_file_ops(attr_sd);
216 rc = ops->store(kobj, attr_sd->s_attr.attr, of->page, count);
218 sysfs_put_active(attr_sd);
224 * sysfs_write_file - write an attribute.
225 * @file: file pointer
226 * @buf: data to write
227 * @count: number of bytes
228 * @ppos: starting offset
230 * Similar to sysfs_read_file(), though working in the opposite direction.
231 * We allocate and fill the data from the user in fill_write_buffer(),
232 * then push it to the kobject in flush_write_buffer().
233 * There is no easy way for us to know if userspace is only doing a partial
234 * write, so we don't support them. We expect the entire buffer to come
235 * on the first write.
236 * Hint: if you're writing a value, first read the file, modify only the
237 * the value you're changing, then write entire buffer back.
239 static ssize_t sysfs_write_file(struct file *file, const char __user *buf,
240 size_t count, loff_t *ppos)
242 struct sysfs_open_file *of = file->private_data;
245 mutex_lock(&of->mutex);
246 len = fill_write_buffer(of, buf, count);
248 len = flush_write_buffer(file->f_path.dentry, of, len);
251 mutex_unlock(&of->mutex);
256 * sysfs_get_open_dirent - get or create sysfs_open_dirent
257 * @sd: target sysfs_dirent
258 * @of: sysfs_open_file for this instance of open
260 * If @sd->s_attr.open exists, increment its reference count;
261 * otherwise, create one. @of is chained to the files list.
264 * Kernel thread context (may sleep).
267 * 0 on success, -errno on failure.
269 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
270 struct sysfs_open_file *of)
272 struct sysfs_open_dirent *od, *new_od = NULL;
275 mutex_lock(&sysfs_open_file_mutex);
276 spin_lock_irq(&sysfs_open_dirent_lock);
278 if (!sd->s_attr.open && new_od) {
279 sd->s_attr.open = new_od;
283 od = sd->s_attr.open;
285 atomic_inc(&od->refcnt);
286 list_add_tail(&of->list, &od->files);
289 spin_unlock_irq(&sysfs_open_dirent_lock);
290 mutex_unlock(&sysfs_open_file_mutex);
297 /* not there, initialize a new one and retry */
298 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
302 atomic_set(&new_od->refcnt, 0);
303 atomic_set(&new_od->event, 1);
304 init_waitqueue_head(&new_od->poll);
305 INIT_LIST_HEAD(&new_od->files);
310 * sysfs_put_open_dirent - put sysfs_open_dirent
311 * @sd: target sysfs_dirent
312 * @of: associated sysfs_open_file
314 * Put @sd->s_attr.open and unlink @of from the files list. If
315 * reference count reaches zero, disassociate and free it.
320 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
321 struct sysfs_open_file *of)
323 struct sysfs_open_dirent *od = sd->s_attr.open;
326 mutex_lock(&sysfs_open_file_mutex);
327 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
330 if (atomic_dec_and_test(&od->refcnt))
331 sd->s_attr.open = NULL;
335 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
336 mutex_unlock(&sysfs_open_file_mutex);
341 static int sysfs_open_file(struct inode *inode, struct file *file)
343 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
344 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
345 struct sysfs_open_file *of;
346 const struct sysfs_ops *ops;
349 /* need attr_sd for attr and ops, its parent for kobj */
350 if (!sysfs_get_active(attr_sd))
353 /* every kobject with an attribute needs a ktype assigned */
354 ops = sysfs_file_ops(attr_sd);
355 if (WARN(!ops, KERN_ERR
356 "missing sysfs attribute operations for kobject: %s\n",
360 /* File needs write support.
361 * The inode's perms must say it's ok,
362 * and we must have a store method.
364 if (file->f_mode & FMODE_WRITE) {
365 if (!(inode->i_mode & S_IWUGO) || !ops->store)
369 /* File needs read support.
370 * The inode's perms must say it's ok, and we there
371 * must be a show method for it.
373 if (file->f_mode & FMODE_READ) {
374 if (!(inode->i_mode & S_IRUGO) || !ops->show)
379 * No error? Great, allocate a sysfs_open_file for the file, and
380 * store it it in file->private_data for easy access.
383 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
387 mutex_init(&of->mutex);
388 file->private_data = of;
390 /* make sure we have open dirent struct */
391 error = sysfs_get_open_dirent(attr_sd, of);
395 /* open succeeded, put active references */
396 sysfs_put_active(attr_sd);
402 sysfs_put_active(attr_sd);
406 static int sysfs_release(struct inode *inode, struct file *filp)
408 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
409 struct sysfs_open_file *of = filp->private_data;
411 sysfs_put_open_dirent(sd, of);
414 free_page((unsigned long)of->page);
420 /* Sysfs attribute files are pollable. The idea is that you read
421 * the content and then you use 'poll' or 'select' to wait for
422 * the content to change. When the content changes (assuming the
423 * manager for the kobject supports notification), poll will
424 * return POLLERR|POLLPRI, and select will return the fd whether
425 * it is waiting for read, write, or exceptions.
426 * Once poll/select indicates that the value has changed, you
427 * need to close and re-open the file, or seek to 0 and read again.
428 * Reminder: this only works for attributes which actively support
429 * it, and it is not possible to test an attribute from userspace
430 * to see if it supports poll (Neither 'poll' nor 'select' return
431 * an appropriate error code). When in doubt, set a suitable timeout value.
433 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
435 struct sysfs_open_file *of = filp->private_data;
436 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
437 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
439 /* need parent for the kobj, grab both */
440 if (!sysfs_get_active(attr_sd))
443 poll_wait(filp, &od->poll, wait);
445 sysfs_put_active(attr_sd);
447 if (of->event != atomic_read(&od->event))
450 return DEFAULT_POLLMASK;
453 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
456 void sysfs_notify_dirent(struct sysfs_dirent *sd)
458 struct sysfs_open_dirent *od;
461 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
463 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
464 od = sd->s_attr.open;
466 atomic_inc(&od->event);
467 wake_up_interruptible(&od->poll);
471 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
473 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
475 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
477 struct sysfs_dirent *sd = k->sd;
479 mutex_lock(&sysfs_mutex);
482 sd = sysfs_find_dirent(sd, dir, NULL);
484 sd = sysfs_find_dirent(sd, attr, NULL);
486 sysfs_notify_dirent(sd);
488 mutex_unlock(&sysfs_mutex);
490 EXPORT_SYMBOL_GPL(sysfs_notify);
492 const struct file_operations sysfs_file_operations = {
493 .read = sysfs_read_file,
494 .write = sysfs_write_file,
495 .llseek = generic_file_llseek,
496 .open = sysfs_open_file,
497 .release = sysfs_release,
501 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
502 const struct attribute *attr, int type,
503 umode_t amode, const void *ns)
505 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
506 struct sysfs_addrm_cxt acxt;
507 struct sysfs_dirent *sd;
510 sd = sysfs_new_dirent(attr->name, mode, type);
515 sd->s_attr.attr = (void *)attr;
516 sysfs_dirent_init_lockdep(sd);
518 sysfs_addrm_start(&acxt);
519 rc = sysfs_add_one(&acxt, sd, dir_sd);
520 sysfs_addrm_finish(&acxt);
529 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
532 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
536 * sysfs_create_file_ns - create an attribute file for an object with custom ns
537 * @kobj: object we're creating for
538 * @attr: attribute descriptor
539 * @ns: namespace the new file should belong to
541 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
544 BUG_ON(!kobj || !kobj->sd || !attr);
546 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
550 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
552 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
557 for (i = 0; ptr[i] && !err; i++)
558 err = sysfs_create_file(kobj, ptr[i]);
561 sysfs_remove_file(kobj, ptr[i]);
564 EXPORT_SYMBOL_GPL(sysfs_create_files);
567 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
568 * @kobj: object we're acting for.
569 * @attr: attribute descriptor.
570 * @group: group name.
572 int sysfs_add_file_to_group(struct kobject *kobj,
573 const struct attribute *attr, const char *group)
575 struct sysfs_dirent *dir_sd;
579 dir_sd = sysfs_get_dirent(kobj->sd, group);
581 dir_sd = sysfs_get(kobj->sd);
586 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
591 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
594 * sysfs_chmod_file - update the modified mode value on an object attribute.
595 * @kobj: object we're acting for.
596 * @attr: attribute descriptor.
597 * @mode: file permissions.
600 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
603 struct sysfs_dirent *sd;
604 struct iattr newattrs;
607 mutex_lock(&sysfs_mutex);
610 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
614 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
615 newattrs.ia_valid = ATTR_MODE;
616 rc = sysfs_sd_setattr(sd, &newattrs);
619 mutex_unlock(&sysfs_mutex);
622 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
625 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
626 * @kobj: object we're acting for
627 * @attr: attribute descriptor
628 * @ns: namespace tag of the file to remove
630 * Hash the attribute name and namespace tag and kill the victim.
632 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
635 struct sysfs_dirent *dir_sd = kobj->sd;
637 sysfs_hash_and_remove(dir_sd, attr->name, ns);
639 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
641 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
644 for (i = 0; ptr[i]; i++)
645 sysfs_remove_file(kobj, ptr[i]);
647 EXPORT_SYMBOL_GPL(sysfs_remove_files);
650 * sysfs_remove_file_from_group - remove an attribute file from a group.
651 * @kobj: object we're acting for.
652 * @attr: attribute descriptor.
653 * @group: group name.
655 void sysfs_remove_file_from_group(struct kobject *kobj,
656 const struct attribute *attr, const char *group)
658 struct sysfs_dirent *dir_sd;
661 dir_sd = sysfs_get_dirent(kobj->sd, group);
663 dir_sd = sysfs_get(kobj->sd);
665 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
669 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
671 struct sysfs_schedule_callback_struct {
672 struct list_head workq_list;
673 struct kobject *kobj;
674 void (*func)(void *);
676 struct module *owner;
677 struct work_struct work;
680 static struct workqueue_struct *sysfs_workqueue;
681 static DEFINE_MUTEX(sysfs_workq_mutex);
682 static LIST_HEAD(sysfs_workq);
683 static void sysfs_schedule_callback_work(struct work_struct *work)
685 struct sysfs_schedule_callback_struct *ss = container_of(work,
686 struct sysfs_schedule_callback_struct, work);
688 (ss->func)(ss->data);
689 kobject_put(ss->kobj);
690 module_put(ss->owner);
691 mutex_lock(&sysfs_workq_mutex);
692 list_del(&ss->workq_list);
693 mutex_unlock(&sysfs_workq_mutex);
698 * sysfs_schedule_callback - helper to schedule a callback for a kobject
699 * @kobj: object we're acting for.
700 * @func: callback function to invoke later.
701 * @data: argument to pass to @func.
702 * @owner: module owning the callback code
704 * sysfs attribute methods must not unregister themselves or their parent
705 * kobject (which would amount to the same thing). Attempts to do so will
706 * deadlock, since unregistration is mutually exclusive with driver
709 * Instead methods can call this routine, which will attempt to allocate
710 * and schedule a workqueue request to call back @func with @data as its
711 * argument in the workqueue's process context. @kobj will be pinned
712 * until @func returns.
714 * Returns 0 if the request was submitted, -ENOMEM if storage could not
715 * be allocated, -ENODEV if a reference to @owner isn't available,
716 * -EAGAIN if a callback has already been scheduled for @kobj.
718 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
719 void *data, struct module *owner)
721 struct sysfs_schedule_callback_struct *ss, *tmp;
723 if (!try_module_get(owner))
726 mutex_lock(&sysfs_workq_mutex);
727 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
728 if (ss->kobj == kobj) {
730 mutex_unlock(&sysfs_workq_mutex);
733 mutex_unlock(&sysfs_workq_mutex);
735 if (sysfs_workqueue == NULL) {
736 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
737 if (sysfs_workqueue == NULL) {
743 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
753 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
754 INIT_LIST_HEAD(&ss->workq_list);
755 mutex_lock(&sysfs_workq_mutex);
756 list_add_tail(&ss->workq_list, &sysfs_workq);
757 mutex_unlock(&sysfs_workq_mutex);
758 queue_work(sysfs_workqueue, &ss->work);
761 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);