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_buffer for each open file and one
29 * sysfs_open_dirent for each sysfs_dirent with one or more open
32 * filp->private_data points to sysfs_buffer and
33 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
34 * is protected by sysfs_open_dirent_lock.
36 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
38 struct sysfs_open_dirent {
41 wait_queue_head_t poll;
42 struct list_head buffers; /* goes through sysfs_buffer.list */
50 struct list_head list;
54 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
55 * must be called while holding an active reference.
57 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
59 struct kobject *kobj = sd->s_parent->s_dir.kobj;
61 lockdep_assert_held(sd);
62 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
66 * fill_read_buffer - allocate and fill buffer from object.
67 * @dentry: dentry pointer.
68 * @buffer: data buffer for file.
70 * Allocate @buffer->page, if it hasn't been already, then call the
71 * kobject's show() method to fill the buffer with this attribute's
73 * This is called only once, on the file's first read unless an error
76 static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer)
78 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
79 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
80 const struct sysfs_ops *ops;
85 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
89 /* need attr_sd for attr and ops, its parent for kobj */
90 if (!sysfs_get_active(attr_sd))
93 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
95 ops = sysfs_file_ops(attr_sd);
96 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
98 sysfs_put_active(attr_sd);
101 * The code works fine with PAGE_SIZE return but it's likely to
102 * indicate truncated result or overflow in normal use cases.
104 if (count >= (ssize_t)PAGE_SIZE) {
105 print_symbol("fill_read_buffer: %s returned bad count\n",
106 (unsigned long)ops->show);
107 /* Try to struggle along */
108 count = PAGE_SIZE - 1;
111 buffer->count = count;
118 * sysfs_read_file - read an attribute.
119 * @file: file pointer.
120 * @buf: buffer to fill.
121 * @count: number of bytes to read.
122 * @ppos: starting offset in file.
124 * Userspace wants to read an attribute file. The attribute descriptor
125 * is in the file's ->d_fsdata. The target object is in the directory's
128 * We call fill_read_buffer() to allocate and fill the buffer from the
129 * object's show() method exactly once (if the read is happening from
130 * the beginning of the file). That should fill the entire buffer with
131 * all the data the object has to offer for that attribute.
132 * We then call flush_read_buffer() to copy the buffer to userspace
133 * in the increments specified.
137 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
139 struct sysfs_buffer *buffer = file->private_data;
142 mutex_lock(&buffer->mutex);
144 * Fill on zero offset and the first read so that silly things like
145 * "dd bs=1 skip=N" can work on sysfs files.
147 if (*ppos == 0 || !buffer->page) {
148 retval = fill_read_buffer(file->f_path.dentry, buffer);
152 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
153 __func__, count, *ppos, buffer->page);
154 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
157 mutex_unlock(&buffer->mutex);
162 * fill_write_buffer - copy buffer from userspace.
163 * @buffer: data buffer for file.
164 * @buf: data from user.
165 * @count: number of bytes in @userbuf.
167 * Allocate @buffer->page if it hasn't been already, then
168 * copy the user-supplied buffer into it.
170 static int fill_write_buffer(struct sysfs_buffer *buffer,
171 const char __user *buf, size_t count)
176 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
180 if (count >= PAGE_SIZE)
181 count = PAGE_SIZE - 1;
182 error = copy_from_user(buffer->page, buf, count);
185 * If buf is assumed to contain a string, terminate it by \0, so
186 * e.g. sscanf() can scan the string easily.
188 buffer->page[count] = 0;
189 return error ? -EFAULT : count;
193 * flush_write_buffer - push buffer to kobject.
194 * @dentry: dentry to the attribute
195 * @buffer: data buffer for file.
196 * @count: number of bytes
198 * Get the correct pointers for the kobject and the attribute we're
199 * dealing with, then call the store() method for the attribute,
200 * passing the buffer that we acquired in fill_write_buffer().
202 static int flush_write_buffer(struct dentry *dentry,
203 struct sysfs_buffer *buffer, size_t count)
205 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
206 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
207 const struct sysfs_ops *ops;
210 /* need attr_sd for attr and ops, its parent for kobj */
211 if (!sysfs_get_active(attr_sd))
214 ops = sysfs_file_ops(attr_sd);
215 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
217 sysfs_put_active(attr_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_buffer *buffer = file->private_data;
244 mutex_lock(&buffer->mutex);
245 len = fill_write_buffer(buffer, buf, count);
247 len = flush_write_buffer(file->f_path.dentry, buffer, len);
250 mutex_unlock(&buffer->mutex);
255 * sysfs_get_open_dirent - get or create sysfs_open_dirent
256 * @sd: target sysfs_dirent
257 * @buffer: sysfs_buffer for this instance of open
259 * If @sd->s_attr.open exists, increment its reference count;
260 * otherwise, create one. @buffer is chained to the buffers
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_buffer *buffer)
272 struct sysfs_open_dirent *od, *new_od = NULL;
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(&buffer->list, &od->buffers);
288 spin_unlock_irq(&sysfs_open_dirent_lock);
295 /* not there, initialize a new one and retry */
296 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
300 atomic_set(&new_od->refcnt, 0);
301 atomic_set(&new_od->event, 1);
302 init_waitqueue_head(&new_od->poll);
303 INIT_LIST_HEAD(&new_od->buffers);
308 * sysfs_put_open_dirent - put sysfs_open_dirent
309 * @sd: target sysfs_dirent
310 * @buffer: associated sysfs_buffer
312 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
313 * If reference count reaches zero, disassociate and free it.
318 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
319 struct sysfs_buffer *buffer)
321 struct sysfs_open_dirent *od = sd->s_attr.open;
324 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
326 list_del(&buffer->list);
327 if (atomic_dec_and_test(&od->refcnt))
328 sd->s_attr.open = NULL;
332 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
337 static int sysfs_open_file(struct inode *inode, struct file *file)
339 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
340 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
341 struct sysfs_buffer *buffer;
342 const struct sysfs_ops *ops;
345 /* need attr_sd for attr and ops, its parent for kobj */
346 if (!sysfs_get_active(attr_sd))
349 /* every kobject with an attribute needs a ktype assigned */
350 ops = sysfs_file_ops(attr_sd);
351 if (WARN(!ops, KERN_ERR
352 "missing sysfs attribute operations for kobject: %s\n",
356 /* File needs write support.
357 * The inode's perms must say it's ok,
358 * and we must have a store method.
360 if (file->f_mode & FMODE_WRITE) {
361 if (!(inode->i_mode & S_IWUGO) || !ops->store)
365 /* File needs read support.
366 * The inode's perms must say it's ok, and we there
367 * must be a show method for it.
369 if (file->f_mode & FMODE_READ) {
370 if (!(inode->i_mode & S_IRUGO) || !ops->show)
374 /* No error? Great, allocate a buffer for the file, and store it
375 * it in file->private_data for easy access.
378 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
382 mutex_init(&buffer->mutex);
383 file->private_data = buffer;
385 /* make sure we have open dirent struct */
386 error = sysfs_get_open_dirent(attr_sd, buffer);
390 /* open succeeded, put active references */
391 sysfs_put_active(attr_sd);
397 sysfs_put_active(attr_sd);
401 static int sysfs_release(struct inode *inode, struct file *filp)
403 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
404 struct sysfs_buffer *buffer = filp->private_data;
406 sysfs_put_open_dirent(sd, buffer);
409 free_page((unsigned long)buffer->page);
415 /* Sysfs attribute files are pollable. The idea is that you read
416 * the content and then you use 'poll' or 'select' to wait for
417 * the content to change. When the content changes (assuming the
418 * manager for the kobject supports notification), poll will
419 * return POLLERR|POLLPRI, and select will return the fd whether
420 * it is waiting for read, write, or exceptions.
421 * Once poll/select indicates that the value has changed, you
422 * need to close and re-open the file, or seek to 0 and read again.
423 * Reminder: this only works for attributes which actively support
424 * it, and it is not possible to test an attribute from userspace
425 * to see if it supports poll (Neither 'poll' nor 'select' return
426 * an appropriate error code). When in doubt, set a suitable timeout value.
428 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
430 struct sysfs_buffer *buffer = filp->private_data;
431 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
432 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
434 /* need parent for the kobj, grab both */
435 if (!sysfs_get_active(attr_sd))
438 poll_wait(filp, &od->poll, wait);
440 sysfs_put_active(attr_sd);
442 if (buffer->event != atomic_read(&od->event))
445 return DEFAULT_POLLMASK;
448 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
451 void sysfs_notify_dirent(struct sysfs_dirent *sd)
453 struct sysfs_open_dirent *od;
456 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
458 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
459 od = sd->s_attr.open;
461 atomic_inc(&od->event);
462 wake_up_interruptible(&od->poll);
466 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
468 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
470 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
472 struct sysfs_dirent *sd = k->sd;
474 mutex_lock(&sysfs_mutex);
477 sd = sysfs_find_dirent(sd, dir, NULL);
479 sd = sysfs_find_dirent(sd, attr, NULL);
481 sysfs_notify_dirent(sd);
483 mutex_unlock(&sysfs_mutex);
485 EXPORT_SYMBOL_GPL(sysfs_notify);
487 const struct file_operations sysfs_file_operations = {
488 .read = sysfs_read_file,
489 .write = sysfs_write_file,
490 .llseek = generic_file_llseek,
491 .open = sysfs_open_file,
492 .release = sysfs_release,
496 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
497 const struct attribute *attr, int type,
498 umode_t amode, const void *ns)
500 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
501 struct sysfs_addrm_cxt acxt;
502 struct sysfs_dirent *sd;
505 sd = sysfs_new_dirent(attr->name, mode, type);
510 sd->s_attr.attr = (void *)attr;
511 sysfs_dirent_init_lockdep(sd);
513 sysfs_addrm_start(&acxt);
514 rc = sysfs_add_one(&acxt, sd, dir_sd);
515 sysfs_addrm_finish(&acxt);
524 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
527 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
531 * sysfs_create_file_ns - create an attribute file for an object with custom ns
532 * @kobj: object we're creating for
533 * @attr: attribute descriptor
534 * @ns: namespace the new file should belong to
536 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
539 BUG_ON(!kobj || !kobj->sd || !attr);
541 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
545 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
547 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
552 for (i = 0; ptr[i] && !err; i++)
553 err = sysfs_create_file(kobj, ptr[i]);
556 sysfs_remove_file(kobj, ptr[i]);
559 EXPORT_SYMBOL_GPL(sysfs_create_files);
562 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
563 * @kobj: object we're acting for.
564 * @attr: attribute descriptor.
565 * @group: group name.
567 int sysfs_add_file_to_group(struct kobject *kobj,
568 const struct attribute *attr, const char *group)
570 struct sysfs_dirent *dir_sd;
574 dir_sd = sysfs_get_dirent(kobj->sd, group);
576 dir_sd = sysfs_get(kobj->sd);
581 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
586 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
589 * sysfs_chmod_file - update the modified mode value on an object attribute.
590 * @kobj: object we're acting for.
591 * @attr: attribute descriptor.
592 * @mode: file permissions.
595 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
598 struct sysfs_dirent *sd;
599 struct iattr newattrs;
602 mutex_lock(&sysfs_mutex);
605 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
609 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
610 newattrs.ia_valid = ATTR_MODE;
611 rc = sysfs_sd_setattr(sd, &newattrs);
614 mutex_unlock(&sysfs_mutex);
617 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
620 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
621 * @kobj: object we're acting for
622 * @attr: attribute descriptor
623 * @ns: namespace tag of the file to remove
625 * Hash the attribute name and namespace tag and kill the victim.
627 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
630 struct sysfs_dirent *dir_sd = kobj->sd;
632 sysfs_hash_and_remove(dir_sd, attr->name, ns);
634 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
636 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
639 for (i = 0; ptr[i]; i++)
640 sysfs_remove_file(kobj, ptr[i]);
642 EXPORT_SYMBOL_GPL(sysfs_remove_files);
645 * sysfs_remove_file_from_group - remove an attribute file from a group.
646 * @kobj: object we're acting for.
647 * @attr: attribute descriptor.
648 * @group: group name.
650 void sysfs_remove_file_from_group(struct kobject *kobj,
651 const struct attribute *attr, const char *group)
653 struct sysfs_dirent *dir_sd;
656 dir_sd = sysfs_get_dirent(kobj->sd, group);
658 dir_sd = sysfs_get(kobj->sd);
660 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
664 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
666 struct sysfs_schedule_callback_struct {
667 struct list_head workq_list;
668 struct kobject *kobj;
669 void (*func)(void *);
671 struct module *owner;
672 struct work_struct work;
675 static struct workqueue_struct *sysfs_workqueue;
676 static DEFINE_MUTEX(sysfs_workq_mutex);
677 static LIST_HEAD(sysfs_workq);
678 static void sysfs_schedule_callback_work(struct work_struct *work)
680 struct sysfs_schedule_callback_struct *ss = container_of(work,
681 struct sysfs_schedule_callback_struct, work);
683 (ss->func)(ss->data);
684 kobject_put(ss->kobj);
685 module_put(ss->owner);
686 mutex_lock(&sysfs_workq_mutex);
687 list_del(&ss->workq_list);
688 mutex_unlock(&sysfs_workq_mutex);
693 * sysfs_schedule_callback - helper to schedule a callback for a kobject
694 * @kobj: object we're acting for.
695 * @func: callback function to invoke later.
696 * @data: argument to pass to @func.
697 * @owner: module owning the callback code
699 * sysfs attribute methods must not unregister themselves or their parent
700 * kobject (which would amount to the same thing). Attempts to do so will
701 * deadlock, since unregistration is mutually exclusive with driver
704 * Instead methods can call this routine, which will attempt to allocate
705 * and schedule a workqueue request to call back @func with @data as its
706 * argument in the workqueue's process context. @kobj will be pinned
707 * until @func returns.
709 * Returns 0 if the request was submitted, -ENOMEM if storage could not
710 * be allocated, -ENODEV if a reference to @owner isn't available,
711 * -EAGAIN if a callback has already been scheduled for @kobj.
713 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
714 void *data, struct module *owner)
716 struct sysfs_schedule_callback_struct *ss, *tmp;
718 if (!try_module_get(owner))
721 mutex_lock(&sysfs_workq_mutex);
722 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
723 if (ss->kobj == kobj) {
725 mutex_unlock(&sysfs_workq_mutex);
728 mutex_unlock(&sysfs_workq_mutex);
730 if (sysfs_workqueue == NULL) {
731 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
732 if (sysfs_workqueue == NULL) {
738 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
748 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
749 INIT_LIST_HEAD(&ss->workq_list);
750 mutex_lock(&sysfs_workq_mutex);
751 list_add_tail(&ss->workq_list, &sysfs_workq);
752 mutex_unlock(&sysfs_workq_mutex);
753 queue_work(sysfs_workqueue, &ss->work);
756 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);