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 * flush_write_buffer - push buffer to kobject
145 * @buf: data buffer for file
146 * @off: file offset to write to
147 * @count: number of bytes
149 * Get the correct pointers for the kobject and the attribute we're dealing
150 * with, then call the store() method for it with @buf.
152 static int flush_write_buffer(struct sysfs_open_file
*of
, char *buf
, loff_t off
,
155 struct kobject
*kobj
= of
->sd
->s_parent
->s_dir
.kobj
;
159 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
160 * nests outside active ref and is just to ensure that the ops
161 * aren't called concurrently for the same open file.
163 mutex_lock(&of
->mutex
);
164 if (!sysfs_get_active(of
->sd
)) {
165 mutex_unlock(&of
->mutex
);
169 if (sysfs_is_bin(of
->sd
)) {
170 struct bin_attribute
*battr
= of
->sd
->s_bin_attr
.bin_attr
;
174 rc
= battr
->write(of
->file
, kobj
, battr
, buf
, off
,
177 const struct sysfs_ops
*ops
= sysfs_file_ops(of
->sd
);
179 rc
= ops
->store(kobj
, of
->sd
->s_attr
.attr
, buf
, count
);
182 sysfs_put_active(of
->sd
);
183 mutex_unlock(&of
->mutex
);
189 * sysfs_write_file - write an attribute
190 * @file: file pointer
191 * @user_buf: data to write
192 * @count: number of bytes
193 * @ppos: starting offset
195 * Copy data in from userland and pass it to the matching
196 * sysfs_ops->store() by invoking flush_write_buffer().
198 * There is no easy way for us to know if userspace is only doing a partial
199 * write, so we don't support them. We expect the entire buffer to come on
200 * the first write. Hint: if you're writing a value, first read the file,
201 * modify only the the value you're changing, then write entire buffer
204 static ssize_t
sysfs_write_file(struct file
*file
, const char __user
*user_buf
,
205 size_t count
, loff_t
*ppos
)
207 struct sysfs_open_file
*of
= sysfs_of(file
);
208 ssize_t len
= min_t(size_t, count
, PAGE_SIZE
);
211 if (sysfs_is_bin(of
->sd
)) {
212 loff_t size
= file_inode(file
)->i_size
;
216 len
= min_t(ssize_t
, len
, size
- *ppos
);
222 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
226 if (copy_from_user(buf
, user_buf
, len
)) {
230 buf
[len
] = '\0'; /* guarantee string termination */
232 len
= flush_write_buffer(of
, buf
, *ppos
, len
);
241 * sysfs_get_open_dirent - get or create sysfs_open_dirent
242 * @sd: target sysfs_dirent
243 * @of: sysfs_open_file for this instance of open
245 * If @sd->s_attr.open exists, increment its reference count;
246 * otherwise, create one. @of is chained to the files list.
249 * Kernel thread context (may sleep).
252 * 0 on success, -errno on failure.
254 static int sysfs_get_open_dirent(struct sysfs_dirent
*sd
,
255 struct sysfs_open_file
*of
)
257 struct sysfs_open_dirent
*od
, *new_od
= NULL
;
260 mutex_lock(&sysfs_open_file_mutex
);
261 spin_lock_irq(&sysfs_open_dirent_lock
);
263 if (!sd
->s_attr
.open
&& new_od
) {
264 sd
->s_attr
.open
= new_od
;
268 od
= sd
->s_attr
.open
;
270 atomic_inc(&od
->refcnt
);
271 list_add_tail(&of
->list
, &od
->files
);
274 spin_unlock_irq(&sysfs_open_dirent_lock
);
275 mutex_unlock(&sysfs_open_file_mutex
);
282 /* not there, initialize a new one and retry */
283 new_od
= kmalloc(sizeof(*new_od
), GFP_KERNEL
);
287 atomic_set(&new_od
->refcnt
, 0);
288 atomic_set(&new_od
->event
, 1);
289 init_waitqueue_head(&new_od
->poll
);
290 INIT_LIST_HEAD(&new_od
->files
);
295 * sysfs_put_open_dirent - put sysfs_open_dirent
296 * @sd: target sysfs_dirent
297 * @of: associated sysfs_open_file
299 * Put @sd->s_attr.open and unlink @of from the files list. If
300 * reference count reaches zero, disassociate and free it.
305 static void sysfs_put_open_dirent(struct sysfs_dirent
*sd
,
306 struct sysfs_open_file
*of
)
308 struct sysfs_open_dirent
*od
= sd
->s_attr
.open
;
311 mutex_lock(&sysfs_open_file_mutex
);
312 spin_lock_irqsave(&sysfs_open_dirent_lock
, flags
);
315 if (atomic_dec_and_test(&od
->refcnt
))
316 sd
->s_attr
.open
= NULL
;
320 spin_unlock_irqrestore(&sysfs_open_dirent_lock
, flags
);
321 mutex_unlock(&sysfs_open_file_mutex
);
326 static int sysfs_open_file(struct inode
*inode
, struct file
*file
)
328 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
329 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
330 struct sysfs_open_file
*of
;
331 const struct sysfs_ops
*ops
;
334 /* need attr_sd for attr and ops, its parent for kobj */
335 if (!sysfs_get_active(attr_sd
))
338 /* every kobject with an attribute needs a ktype assigned */
339 ops
= sysfs_file_ops(attr_sd
);
340 if (WARN(!ops
, KERN_ERR
341 "missing sysfs attribute operations for kobject: %s\n",
345 /* File needs write support.
346 * The inode's perms must say it's ok,
347 * and we must have a store method.
349 if (file
->f_mode
& FMODE_WRITE
) {
350 if (!(inode
->i_mode
& S_IWUGO
) || !ops
->store
)
354 /* File needs read support.
355 * The inode's perms must say it's ok, and we there
356 * must be a show method for it.
358 if (file
->f_mode
& FMODE_READ
) {
359 if (!(inode
->i_mode
& S_IRUGO
) || !ops
->show
)
363 /* allocate a sysfs_open_file for the file */
365 of
= kzalloc(sizeof(struct sysfs_open_file
), GFP_KERNEL
);
369 mutex_init(&of
->mutex
);
374 * Always instantiate seq_file even if read access is not
375 * implemented or requested. This unifies private data access and
376 * most files are readable anyway.
378 error
= single_open(file
, sysfs_seq_show
, of
);
382 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
383 if (file
->f_mode
& FMODE_WRITE
)
384 file
->f_mode
|= FMODE_PWRITE
;
386 /* make sure we have open dirent struct */
387 error
= sysfs_get_open_dirent(attr_sd
, of
);
391 /* open succeeded, put active references */
392 sysfs_put_active(attr_sd
);
396 single_release(inode
, file
);
400 sysfs_put_active(attr_sd
);
404 static int sysfs_release(struct inode
*inode
, struct file
*filp
)
406 struct sysfs_dirent
*sd
= filp
->f_path
.dentry
->d_fsdata
;
407 struct sysfs_open_file
*of
= sysfs_of(filp
);
409 sysfs_put_open_dirent(sd
, of
);
410 single_release(inode
, filp
);
416 /* Sysfs attribute files are pollable. The idea is that you read
417 * the content and then you use 'poll' or 'select' to wait for
418 * the content to change. When the content changes (assuming the
419 * manager for the kobject supports notification), poll will
420 * return POLLERR|POLLPRI, and select will return the fd whether
421 * it is waiting for read, write, or exceptions.
422 * Once poll/select indicates that the value has changed, you
423 * need to close and re-open the file, or seek to 0 and read again.
424 * Reminder: this only works for attributes which actively support
425 * it, and it is not possible to test an attribute from userspace
426 * to see if it supports poll (Neither 'poll' nor 'select' return
427 * an appropriate error code). When in doubt, set a suitable timeout value.
429 static unsigned int sysfs_poll(struct file
*filp
, poll_table
*wait
)
431 struct sysfs_open_file
*of
= sysfs_of(filp
);
432 struct sysfs_dirent
*attr_sd
= filp
->f_path
.dentry
->d_fsdata
;
433 struct sysfs_open_dirent
*od
= attr_sd
->s_attr
.open
;
435 /* need parent for the kobj, grab both */
436 if (!sysfs_get_active(attr_sd
))
439 poll_wait(filp
, &od
->poll
, wait
);
441 sysfs_put_active(attr_sd
);
443 if (of
->event
!= atomic_read(&od
->event
))
446 return DEFAULT_POLLMASK
;
449 return DEFAULT_POLLMASK
|POLLERR
|POLLPRI
;
452 void sysfs_notify_dirent(struct sysfs_dirent
*sd
)
454 struct sysfs_open_dirent
*od
;
457 spin_lock_irqsave(&sysfs_open_dirent_lock
, flags
);
459 if (!WARN_ON(sysfs_type(sd
) != SYSFS_KOBJ_ATTR
)) {
460 od
= sd
->s_attr
.open
;
462 atomic_inc(&od
->event
);
463 wake_up_interruptible(&od
->poll
);
467 spin_unlock_irqrestore(&sysfs_open_dirent_lock
, flags
);
469 EXPORT_SYMBOL_GPL(sysfs_notify_dirent
);
471 void sysfs_notify(struct kobject
*k
, const char *dir
, const char *attr
)
473 struct sysfs_dirent
*sd
= k
->sd
;
475 mutex_lock(&sysfs_mutex
);
478 sd
= sysfs_find_dirent(sd
, dir
, NULL
);
480 sd
= sysfs_find_dirent(sd
, attr
, NULL
);
482 sysfs_notify_dirent(sd
);
484 mutex_unlock(&sysfs_mutex
);
486 EXPORT_SYMBOL_GPL(sysfs_notify
);
488 const struct file_operations sysfs_file_operations
= {
490 .write
= sysfs_write_file
,
492 .open
= sysfs_open_file
,
493 .release
= sysfs_release
,
497 const struct file_operations sysfs_bin_operations
= {
498 .write
= sysfs_write_file
,
499 .llseek
= generic_file_llseek
,
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
);