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 {
48 struct sysfs_dirent *sd;
54 struct list_head list;
58 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
59 * must be called while holding an active reference.
61 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
63 struct kobject *kobj = sd->s_parent->s_dir.kobj;
65 lockdep_assert_held(sd);
66 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
70 * fill_read_buffer - allocate and fill buffer from object.
71 * @dentry: dentry pointer.
72 * @buffer: data buffer for file.
74 * Allocate @buffer->page, if it hasn't been already, then call the
75 * kobject's show() method to fill the buffer with this attribute's
77 * This is called only once, on the file's first read unless an error
80 static int fill_read_buffer(struct dentry *dentry, struct sysfs_open_file *of)
82 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
83 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
84 const struct sysfs_ops *ops;
89 of->page = (char *) get_zeroed_page(GFP_KERNEL);
93 /* need attr_sd for attr and ops, its parent for kobj */
94 if (!sysfs_get_active(attr_sd))
97 of->event = atomic_read(&attr_sd->s_attr.open->event);
99 ops = sysfs_file_ops(attr_sd);
100 count = ops->show(kobj, attr_sd->s_attr.attr, of->page);
102 sysfs_put_active(attr_sd);
105 * The code works fine with PAGE_SIZE return but it's likely to
106 * indicate truncated result or overflow in normal use cases.
108 if (count >= (ssize_t)PAGE_SIZE) {
109 print_symbol("fill_read_buffer: %s returned bad count\n",
110 (unsigned long)ops->show);
111 /* Try to struggle along */
112 count = PAGE_SIZE - 1;
122 * sysfs_read_file - read an attribute.
123 * @file: file pointer.
124 * @buf: buffer to fill.
125 * @count: number of bytes to read.
126 * @ppos: starting offset in file.
128 * Userspace wants to read an attribute file. The attribute descriptor
129 * is in the file's ->d_fsdata. The target object is in the directory's
132 * We call fill_read_buffer() to allocate and fill the buffer from the
133 * object's show() method exactly once (if the read is happening from
134 * the beginning of the file). That should fill the entire buffer with
135 * all the data the object has to offer for that attribute.
136 * We then call flush_read_buffer() to copy the buffer to userspace
137 * in the increments specified.
141 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143 struct sysfs_open_file *of = file->private_data;
146 mutex_lock(&of->mutex);
148 * Fill on zero offset and the first read so that silly things like
149 * "dd bs=1 skip=N" can work on sysfs files.
151 if (*ppos == 0 || !of->page) {
152 retval = fill_read_buffer(file->f_path.dentry, of);
156 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
157 __func__, count, *ppos, of->page);
158 retval = simple_read_from_buffer(buf, count, ppos, of->page, of->count);
160 mutex_unlock(&of->mutex);
165 * fill_write_buffer - copy buffer from userspace.
166 * @of: open file struct.
167 * @buf: data from user.
168 * @count: number of bytes in @userbuf.
170 * Allocate @of->page if it hasn't been already, then copy the
171 * user-supplied buffer into it.
173 static int fill_write_buffer(struct sysfs_open_file *of,
174 const char __user *buf, size_t count)
179 of->page = (char *)get_zeroed_page(GFP_KERNEL);
183 if (count >= PAGE_SIZE)
184 count = PAGE_SIZE - 1;
185 error = copy_from_user(of->page, buf, count);
188 * If buf is assumed to contain a string, terminate it by \0, so
189 * e.g. sscanf() can scan the string easily.
192 return error ? -EFAULT : count;
196 * flush_write_buffer - push buffer to kobject.
198 * @count: number of bytes
200 * Get the correct pointers for the kobject and the attribute we're
201 * dealing with, then call the store() method for the attribute,
202 * passing the buffer that we acquired in fill_write_buffer().
204 static int flush_write_buffer(struct sysfs_open_file *of, size_t count)
206 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
207 const struct sysfs_ops *ops;
210 /* need @of->sd for attr and ops, its parent for kobj */
211 if (!sysfs_get_active(of->sd))
214 ops = sysfs_file_ops(of->sd);
215 rc = ops->store(kobj, of->sd->s_attr.attr, of->page, count);
217 sysfs_put_active(of->sd);
223 * sysfs_write_file - write an attribute.
224 * @file: file pointer
225 * @buf: data to write
226 * @count: number of bytes
227 * @ppos: starting offset
229 * Similar to sysfs_read_file(), though working in the opposite direction.
230 * We allocate and fill the data from the user in fill_write_buffer(),
231 * then push it to the kobject in flush_write_buffer().
232 * There is no easy way for us to know if userspace is only doing a partial
233 * write, so we don't support them. We expect the entire buffer to come
234 * on the first write.
235 * Hint: if you're writing a value, first read the file, modify only the
236 * the value you're changing, then write entire buffer back.
238 static ssize_t sysfs_write_file(struct file *file, const char __user *buf,
239 size_t count, loff_t *ppos)
241 struct sysfs_open_file *of = file->private_data;
244 mutex_lock(&of->mutex);
245 len = fill_write_buffer(of, buf, count);
247 len = flush_write_buffer(of, len);
250 mutex_unlock(&of->mutex);
255 * sysfs_get_open_dirent - get or create sysfs_open_dirent
256 * @sd: target sysfs_dirent
257 * @of: sysfs_open_file for this instance of open
259 * If @sd->s_attr.open exists, increment its reference count;
260 * otherwise, create one. @of is chained to the files list.
263 * Kernel thread context (may sleep).
266 * 0 on success, -errno on failure.
268 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
269 struct sysfs_open_file *of)
271 struct sysfs_open_dirent *od, *new_od = NULL;
274 mutex_lock(&sysfs_open_file_mutex);
275 spin_lock_irq(&sysfs_open_dirent_lock);
277 if (!sd->s_attr.open && new_od) {
278 sd->s_attr.open = new_od;
282 od = sd->s_attr.open;
284 atomic_inc(&od->refcnt);
285 list_add_tail(&of->list, &od->files);
288 spin_unlock_irq(&sysfs_open_dirent_lock);
289 mutex_unlock(&sysfs_open_file_mutex);
296 /* not there, initialize a new one and retry */
297 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
301 atomic_set(&new_od->refcnt, 0);
302 atomic_set(&new_od->event, 1);
303 init_waitqueue_head(&new_od->poll);
304 INIT_LIST_HEAD(&new_od->files);
309 * sysfs_put_open_dirent - put sysfs_open_dirent
310 * @sd: target sysfs_dirent
311 * @of: associated sysfs_open_file
313 * Put @sd->s_attr.open and unlink @of from the files list. If
314 * reference count reaches zero, disassociate and free it.
319 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
320 struct sysfs_open_file *of)
322 struct sysfs_open_dirent *od = sd->s_attr.open;
325 mutex_lock(&sysfs_open_file_mutex);
326 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
329 if (atomic_dec_and_test(&od->refcnt))
330 sd->s_attr.open = NULL;
334 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
335 mutex_unlock(&sysfs_open_file_mutex);
340 static int sysfs_open_file(struct inode *inode, struct file *file)
342 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
343 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
344 struct sysfs_open_file *of;
345 const struct sysfs_ops *ops;
348 /* need attr_sd for attr and ops, its parent for kobj */
349 if (!sysfs_get_active(attr_sd))
352 /* every kobject with an attribute needs a ktype assigned */
353 ops = sysfs_file_ops(attr_sd);
354 if (WARN(!ops, KERN_ERR
355 "missing sysfs attribute operations for kobject: %s\n",
359 /* File needs write support.
360 * The inode's perms must say it's ok,
361 * and we must have a store method.
363 if (file->f_mode & FMODE_WRITE) {
364 if (!(inode->i_mode & S_IWUGO) || !ops->store)
368 /* File needs read support.
369 * The inode's perms must say it's ok, and we there
370 * must be a show method for it.
372 if (file->f_mode & FMODE_READ) {
373 if (!(inode->i_mode & S_IRUGO) || !ops->show)
378 * No error? Great, allocate a sysfs_open_file for the file, and
379 * store it it in file->private_data for easy access.
382 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
386 mutex_init(&of->mutex);
389 file->private_data = of;
391 /* make sure we have open dirent struct */
392 error = sysfs_get_open_dirent(attr_sd, of);
396 /* open succeeded, put active references */
397 sysfs_put_active(attr_sd);
403 sysfs_put_active(attr_sd);
407 static int sysfs_release(struct inode *inode, struct file *filp)
409 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
410 struct sysfs_open_file *of = filp->private_data;
412 sysfs_put_open_dirent(sd, of);
415 free_page((unsigned long)of->page);
421 /* Sysfs attribute files are pollable. The idea is that you read
422 * the content and then you use 'poll' or 'select' to wait for
423 * the content to change. When the content changes (assuming the
424 * manager for the kobject supports notification), poll will
425 * return POLLERR|POLLPRI, and select will return the fd whether
426 * it is waiting for read, write, or exceptions.
427 * Once poll/select indicates that the value has changed, you
428 * need to close and re-open the file, or seek to 0 and read again.
429 * Reminder: this only works for attributes which actively support
430 * it, and it is not possible to test an attribute from userspace
431 * to see if it supports poll (Neither 'poll' nor 'select' return
432 * an appropriate error code). When in doubt, set a suitable timeout value.
434 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
436 struct sysfs_open_file *of = filp->private_data;
437 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
438 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
440 /* need parent for the kobj, grab both */
441 if (!sysfs_get_active(attr_sd))
444 poll_wait(filp, &od->poll, wait);
446 sysfs_put_active(attr_sd);
448 if (of->event != atomic_read(&od->event))
451 return DEFAULT_POLLMASK;
454 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
457 void sysfs_notify_dirent(struct sysfs_dirent *sd)
459 struct sysfs_open_dirent *od;
462 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
464 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
465 od = sd->s_attr.open;
467 atomic_inc(&od->event);
468 wake_up_interruptible(&od->poll);
472 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
474 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
476 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
478 struct sysfs_dirent *sd = k->sd;
480 mutex_lock(&sysfs_mutex);
483 sd = sysfs_find_dirent(sd, dir, NULL);
485 sd = sysfs_find_dirent(sd, attr, NULL);
487 sysfs_notify_dirent(sd);
489 mutex_unlock(&sysfs_mutex);
491 EXPORT_SYMBOL_GPL(sysfs_notify);
493 const struct file_operations sysfs_file_operations = {
494 .read = sysfs_read_file,
495 .write = sysfs_write_file,
496 .llseek = generic_file_llseek,
497 .open = sysfs_open_file,
498 .release = sysfs_release,
502 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
503 const struct attribute *attr, int type,
504 umode_t amode, const void *ns)
506 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
507 struct sysfs_addrm_cxt acxt;
508 struct sysfs_dirent *sd;
511 sd = sysfs_new_dirent(attr->name, mode, type);
516 sd->s_attr.attr = (void *)attr;
517 sysfs_dirent_init_lockdep(sd);
519 sysfs_addrm_start(&acxt);
520 rc = sysfs_add_one(&acxt, sd, dir_sd);
521 sysfs_addrm_finish(&acxt);
530 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
533 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
537 * sysfs_create_file_ns - create an attribute file for an object with custom ns
538 * @kobj: object we're creating for
539 * @attr: attribute descriptor
540 * @ns: namespace the new file should belong to
542 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
545 BUG_ON(!kobj || !kobj->sd || !attr);
547 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
551 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
553 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
558 for (i = 0; ptr[i] && !err; i++)
559 err = sysfs_create_file(kobj, ptr[i]);
562 sysfs_remove_file(kobj, ptr[i]);
565 EXPORT_SYMBOL_GPL(sysfs_create_files);
568 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
569 * @kobj: object we're acting for.
570 * @attr: attribute descriptor.
571 * @group: group name.
573 int sysfs_add_file_to_group(struct kobject *kobj,
574 const struct attribute *attr, const char *group)
576 struct sysfs_dirent *dir_sd;
580 dir_sd = sysfs_get_dirent(kobj->sd, group);
582 dir_sd = sysfs_get(kobj->sd);
587 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
592 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
595 * sysfs_chmod_file - update the modified mode value on an object attribute.
596 * @kobj: object we're acting for.
597 * @attr: attribute descriptor.
598 * @mode: file permissions.
601 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
604 struct sysfs_dirent *sd;
605 struct iattr newattrs;
608 mutex_lock(&sysfs_mutex);
611 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
615 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
616 newattrs.ia_valid = ATTR_MODE;
617 rc = sysfs_sd_setattr(sd, &newattrs);
620 mutex_unlock(&sysfs_mutex);
623 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
626 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
627 * @kobj: object we're acting for
628 * @attr: attribute descriptor
629 * @ns: namespace tag of the file to remove
631 * Hash the attribute name and namespace tag and kill the victim.
633 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
636 struct sysfs_dirent *dir_sd = kobj->sd;
638 sysfs_hash_and_remove(dir_sd, attr->name, ns);
640 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
642 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
645 for (i = 0; ptr[i]; i++)
646 sysfs_remove_file(kobj, ptr[i]);
648 EXPORT_SYMBOL_GPL(sysfs_remove_files);
651 * sysfs_remove_file_from_group - remove an attribute file from a group.
652 * @kobj: object we're acting for.
653 * @attr: attribute descriptor.
654 * @group: group name.
656 void sysfs_remove_file_from_group(struct kobject *kobj,
657 const struct attribute *attr, const char *group)
659 struct sysfs_dirent *dir_sd;
662 dir_sd = sysfs_get_dirent(kobj->sd, group);
664 dir_sd = sysfs_get(kobj->sd);
666 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
670 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
672 struct sysfs_schedule_callback_struct {
673 struct list_head workq_list;
674 struct kobject *kobj;
675 void (*func)(void *);
677 struct module *owner;
678 struct work_struct work;
681 static struct workqueue_struct *sysfs_workqueue;
682 static DEFINE_MUTEX(sysfs_workq_mutex);
683 static LIST_HEAD(sysfs_workq);
684 static void sysfs_schedule_callback_work(struct work_struct *work)
686 struct sysfs_schedule_callback_struct *ss = container_of(work,
687 struct sysfs_schedule_callback_struct, work);
689 (ss->func)(ss->data);
690 kobject_put(ss->kobj);
691 module_put(ss->owner);
692 mutex_lock(&sysfs_workq_mutex);
693 list_del(&ss->workq_list);
694 mutex_unlock(&sysfs_workq_mutex);
699 * sysfs_schedule_callback - helper to schedule a callback for a kobject
700 * @kobj: object we're acting for.
701 * @func: callback function to invoke later.
702 * @data: argument to pass to @func.
703 * @owner: module owning the callback code
705 * sysfs attribute methods must not unregister themselves or their parent
706 * kobject (which would amount to the same thing). Attempts to do so will
707 * deadlock, since unregistration is mutually exclusive with driver
710 * Instead methods can call this routine, which will attempt to allocate
711 * and schedule a workqueue request to call back @func with @data as its
712 * argument in the workqueue's process context. @kobj will be pinned
713 * until @func returns.
715 * Returns 0 if the request was submitted, -ENOMEM if storage could not
716 * be allocated, -ENODEV if a reference to @owner isn't available,
717 * -EAGAIN if a callback has already been scheduled for @kobj.
719 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
720 void *data, struct module *owner)
722 struct sysfs_schedule_callback_struct *ss, *tmp;
724 if (!try_module_get(owner))
727 mutex_lock(&sysfs_workq_mutex);
728 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
729 if (ss->kobj == kobj) {
731 mutex_unlock(&sysfs_workq_mutex);
734 mutex_unlock(&sysfs_workq_mutex);
736 if (sysfs_workqueue == NULL) {
737 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
738 if (sysfs_workqueue == NULL) {
744 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
754 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
755 INIT_LIST_HEAD(&ss->workq_list);
756 mutex_lock(&sysfs_workq_mutex);
757 list_add_tail(&ss->workq_list, &sysfs_workq);
758 mutex_unlock(&sysfs_workq_mutex);
759 queue_work(sysfs_workqueue, &ss->work);
762 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);