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/namei.h>
16 #include <linux/poll.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/limits.h>
20 #include <asm/uaccess.h>
24 /* used in crash dumps to help with debugging */
25 static char last_sysfs_file
[PATH_MAX
];
26 void sysfs_printk_last_file(void)
28 printk(KERN_EMERG
"last sysfs file: %s\n", last_sysfs_file
);
32 * There's one sysfs_buffer for each open file and one
33 * sysfs_open_dirent for each sysfs_dirent with one or more open
36 * filp->private_data points to sysfs_buffer and
37 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
38 * is protected by sysfs_open_dirent_lock.
40 static DEFINE_SPINLOCK(sysfs_open_dirent_lock
);
42 struct sysfs_open_dirent
{
45 wait_queue_head_t poll
;
46 struct list_head buffers
; /* goes through sysfs_buffer.list */
53 struct sysfs_ops
* ops
;
57 struct list_head list
;
61 * fill_read_buffer - allocate and fill buffer from object.
62 * @dentry: dentry pointer.
63 * @buffer: data buffer for file.
65 * Allocate @buffer->page, if it hasn't been already, then call the
66 * kobject's show() method to fill the buffer with this attribute's
68 * This is called only once, on the file's first read unless an error
71 static int fill_read_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
)
73 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
74 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
75 struct sysfs_ops
* ops
= buffer
->ops
;
80 buffer
->page
= (char *) get_zeroed_page(GFP_KERNEL
);
84 /* need attr_sd for attr and ops, its parent for kobj */
85 if (!sysfs_get_active_two(attr_sd
))
88 buffer
->event
= atomic_read(&attr_sd
->s_attr
.open
->event
);
89 count
= ops
->show(kobj
, attr_sd
->s_attr
.attr
, buffer
->page
);
91 sysfs_put_active_two(attr_sd
);
93 BUG_ON(count
>= (ssize_t
)PAGE_SIZE
);
95 buffer
->needs_read_fill
= 0;
96 buffer
->count
= count
;
104 * sysfs_read_file - read an attribute.
105 * @file: file pointer.
106 * @buf: buffer to fill.
107 * @count: number of bytes to read.
108 * @ppos: starting offset in file.
110 * Userspace wants to read an attribute file. The attribute descriptor
111 * is in the file's ->d_fsdata. The target object is in the directory's
114 * We call fill_read_buffer() to allocate and fill the buffer from the
115 * object's show() method exactly once (if the read is happening from
116 * the beginning of the file). That should fill the entire buffer with
117 * all the data the object has to offer for that attribute.
118 * We then call flush_read_buffer() to copy the buffer to userspace
119 * in the increments specified.
123 sysfs_read_file(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
125 struct sysfs_buffer
* buffer
= file
->private_data
;
128 mutex_lock(&buffer
->mutex
);
129 if (buffer
->needs_read_fill
) {
130 retval
= fill_read_buffer(file
->f_path
.dentry
,buffer
);
134 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
135 __FUNCTION__
, count
, *ppos
, buffer
->page
);
136 retval
= simple_read_from_buffer(buf
, count
, ppos
, buffer
->page
,
139 mutex_unlock(&buffer
->mutex
);
144 * fill_write_buffer - copy buffer from userspace.
145 * @buffer: data buffer for file.
146 * @buf: data from user.
147 * @count: number of bytes in @userbuf.
149 * Allocate @buffer->page if it hasn't been already, then
150 * copy the user-supplied buffer into it.
154 fill_write_buffer(struct sysfs_buffer
* buffer
, const char __user
* buf
, size_t count
)
159 buffer
->page
= (char *)get_zeroed_page(GFP_KERNEL
);
163 if (count
>= PAGE_SIZE
)
164 count
= PAGE_SIZE
- 1;
165 error
= copy_from_user(buffer
->page
,buf
,count
);
166 buffer
->needs_read_fill
= 1;
167 /* if buf is assumed to contain a string, terminate it by \0,
168 so e.g. sscanf() can scan the string easily */
169 buffer
->page
[count
] = 0;
170 return error
? -EFAULT
: count
;
175 * flush_write_buffer - push buffer to kobject.
176 * @dentry: dentry to the attribute
177 * @buffer: data buffer for file.
178 * @count: number of bytes
180 * Get the correct pointers for the kobject and the attribute we're
181 * dealing with, then call the store() method for the attribute,
182 * passing the buffer that we acquired in fill_write_buffer().
186 flush_write_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
, size_t count
)
188 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
189 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
190 struct sysfs_ops
* ops
= buffer
->ops
;
193 /* need attr_sd for attr and ops, its parent for kobj */
194 if (!sysfs_get_active_two(attr_sd
))
197 rc
= ops
->store(kobj
, attr_sd
->s_attr
.attr
, buffer
->page
, count
);
199 sysfs_put_active_two(attr_sd
);
206 * sysfs_write_file - write an attribute.
207 * @file: file pointer
208 * @buf: data to write
209 * @count: number of bytes
210 * @ppos: starting offset
212 * Similar to sysfs_read_file(), though working in the opposite direction.
213 * We allocate and fill the data from the user in fill_write_buffer(),
214 * then push it to the kobject in flush_write_buffer().
215 * There is no easy way for us to know if userspace is only doing a partial
216 * write, so we don't support them. We expect the entire buffer to come
217 * on the first write.
218 * Hint: if you're writing a value, first read the file, modify only the
219 * the value you're changing, then write entire buffer back.
223 sysfs_write_file(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
225 struct sysfs_buffer
* buffer
= file
->private_data
;
228 mutex_lock(&buffer
->mutex
);
229 len
= fill_write_buffer(buffer
, buf
, count
);
231 len
= flush_write_buffer(file
->f_path
.dentry
, buffer
, len
);
234 mutex_unlock(&buffer
->mutex
);
239 * sysfs_get_open_dirent - get or create sysfs_open_dirent
240 * @sd: target sysfs_dirent
241 * @buffer: sysfs_buffer for this instance of open
243 * If @sd->s_attr.open exists, increment its reference count;
244 * otherwise, create one. @buffer is chained to the buffers
248 * Kernel thread context (may sleep).
251 * 0 on success, -errno on failure.
253 static int sysfs_get_open_dirent(struct sysfs_dirent
*sd
,
254 struct sysfs_buffer
*buffer
)
256 struct sysfs_open_dirent
*od
, *new_od
= NULL
;
259 spin_lock(&sysfs_open_dirent_lock
);
261 if (!sd
->s_attr
.open
&& new_od
) {
262 sd
->s_attr
.open
= new_od
;
266 od
= sd
->s_attr
.open
;
268 atomic_inc(&od
->refcnt
);
269 list_add_tail(&buffer
->list
, &od
->buffers
);
272 spin_unlock(&sysfs_open_dirent_lock
);
279 /* not there, initialize a new one and retry */
280 new_od
= kmalloc(sizeof(*new_od
), GFP_KERNEL
);
284 atomic_set(&new_od
->refcnt
, 0);
285 atomic_set(&new_od
->event
, 1);
286 init_waitqueue_head(&new_od
->poll
);
287 INIT_LIST_HEAD(&new_od
->buffers
);
292 * sysfs_put_open_dirent - put sysfs_open_dirent
293 * @sd: target sysfs_dirent
294 * @buffer: associated sysfs_buffer
296 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
297 * If reference count reaches zero, disassociate and free it.
302 static void sysfs_put_open_dirent(struct sysfs_dirent
*sd
,
303 struct sysfs_buffer
*buffer
)
305 struct sysfs_open_dirent
*od
= sd
->s_attr
.open
;
307 spin_lock(&sysfs_open_dirent_lock
);
309 list_del(&buffer
->list
);
310 if (atomic_dec_and_test(&od
->refcnt
))
311 sd
->s_attr
.open
= NULL
;
315 spin_unlock(&sysfs_open_dirent_lock
);
320 static int sysfs_open_file(struct inode
*inode
, struct file
*file
)
322 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
323 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
324 struct sysfs_buffer
*buffer
;
325 struct sysfs_ops
*ops
;
329 p
= d_path(&file
->f_path
, last_sysfs_file
, sizeof(last_sysfs_file
));
331 memmove(last_sysfs_file
, p
, strlen(p
) + 1);
333 /* need attr_sd for attr and ops, its parent for kobj */
334 if (!sysfs_get_active_two(attr_sd
))
337 /* every kobject with an attribute needs a ktype assigned */
338 if (kobj
->ktype
&& kobj
->ktype
->sysfs_ops
)
339 ops
= kobj
->ktype
->sysfs_ops
;
341 printk(KERN_ERR
"missing sysfs attribute operations for "
342 "kobject: %s\n", kobject_name(kobj
));
347 /* File needs write support.
348 * The inode's perms must say it's ok,
349 * and we must have a store method.
351 if (file
->f_mode
& FMODE_WRITE
) {
352 if (!(inode
->i_mode
& S_IWUGO
) || !ops
->store
)
356 /* File needs read support.
357 * The inode's perms must say it's ok, and we there
358 * must be a show method for it.
360 if (file
->f_mode
& FMODE_READ
) {
361 if (!(inode
->i_mode
& S_IRUGO
) || !ops
->show
)
365 /* No error? Great, allocate a buffer for the file, and store it
366 * it in file->private_data for easy access.
369 buffer
= kzalloc(sizeof(struct sysfs_buffer
), GFP_KERNEL
);
373 mutex_init(&buffer
->mutex
);
374 buffer
->needs_read_fill
= 1;
376 file
->private_data
= buffer
;
378 /* make sure we have open dirent struct */
379 error
= sysfs_get_open_dirent(attr_sd
, buffer
);
383 /* open succeeded, put active references */
384 sysfs_put_active_two(attr_sd
);
390 sysfs_put_active_two(attr_sd
);
394 static int sysfs_release(struct inode
*inode
, struct file
*filp
)
396 struct sysfs_dirent
*sd
= filp
->f_path
.dentry
->d_fsdata
;
397 struct sysfs_buffer
*buffer
= filp
->private_data
;
399 sysfs_put_open_dirent(sd
, buffer
);
402 free_page((unsigned long)buffer
->page
);
408 /* Sysfs attribute files are pollable. The idea is that you read
409 * the content and then you use 'poll' or 'select' to wait for
410 * the content to change. When the content changes (assuming the
411 * manager for the kobject supports notification), poll will
412 * return POLLERR|POLLPRI, and select will return the fd whether
413 * it is waiting for read, write, or exceptions.
414 * Once poll/select indicates that the value has changed, you
415 * need to close and re-open the file, as simply seeking and reading
416 * again will not get new data, or reset the state of 'poll'.
417 * Reminder: this only works for attributes which actively support
418 * it, and it is not possible to test an attribute from userspace
419 * to see if it supports poll (Neither 'poll' nor 'select' return
420 * an appropriate error code). When in doubt, set a suitable timeout value.
422 static unsigned int sysfs_poll(struct file
*filp
, poll_table
*wait
)
424 struct sysfs_buffer
* buffer
= filp
->private_data
;
425 struct sysfs_dirent
*attr_sd
= filp
->f_path
.dentry
->d_fsdata
;
426 struct sysfs_open_dirent
*od
= attr_sd
->s_attr
.open
;
428 /* need parent for the kobj, grab both */
429 if (!sysfs_get_active_two(attr_sd
))
432 poll_wait(filp
, &od
->poll
, wait
);
434 sysfs_put_active_two(attr_sd
);
436 if (buffer
->event
!= atomic_read(&od
->event
))
442 buffer
->needs_read_fill
= 1;
443 return POLLERR
|POLLPRI
;
446 void sysfs_notify(struct kobject
*k
, char *dir
, char *attr
)
448 struct sysfs_dirent
*sd
= k
->sd
;
450 mutex_lock(&sysfs_mutex
);
453 sd
= sysfs_find_dirent(sd
, dir
);
455 sd
= sysfs_find_dirent(sd
, attr
);
457 struct sysfs_open_dirent
*od
;
459 spin_lock(&sysfs_open_dirent_lock
);
461 od
= sd
->s_attr
.open
;
463 atomic_inc(&od
->event
);
464 wake_up_interruptible(&od
->poll
);
467 spin_unlock(&sysfs_open_dirent_lock
);
470 mutex_unlock(&sysfs_mutex
);
472 EXPORT_SYMBOL_GPL(sysfs_notify
);
474 const struct file_operations sysfs_file_operations
= {
475 .read
= sysfs_read_file
,
476 .write
= sysfs_write_file
,
477 .llseek
= generic_file_llseek
,
478 .open
= sysfs_open_file
,
479 .release
= sysfs_release
,
484 int sysfs_add_file(struct sysfs_dirent
*dir_sd
, const struct attribute
*attr
,
487 umode_t mode
= (attr
->mode
& S_IALLUGO
) | S_IFREG
;
488 struct sysfs_addrm_cxt acxt
;
489 struct sysfs_dirent
*sd
;
492 sd
= sysfs_new_dirent(attr
->name
, mode
, type
);
495 sd
->s_attr
.attr
= (void *)attr
;
497 sysfs_addrm_start(&acxt
, dir_sd
);
498 rc
= sysfs_add_one(&acxt
, sd
);
499 sysfs_addrm_finish(&acxt
);
509 * sysfs_create_file - create an attribute file for an object.
510 * @kobj: object we're creating for.
511 * @attr: attribute descriptor.
514 int sysfs_create_file(struct kobject
* kobj
, const struct attribute
* attr
)
516 BUG_ON(!kobj
|| !kobj
->sd
|| !attr
);
518 return sysfs_add_file(kobj
->sd
, attr
, SYSFS_KOBJ_ATTR
);
524 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
525 * @kobj: object we're acting for.
526 * @attr: attribute descriptor.
527 * @group: group name.
529 int sysfs_add_file_to_group(struct kobject
*kobj
,
530 const struct attribute
*attr
, const char *group
)
532 struct sysfs_dirent
*dir_sd
;
535 dir_sd
= sysfs_get_dirent(kobj
->sd
, group
);
539 error
= sysfs_add_file(dir_sd
, attr
, SYSFS_KOBJ_ATTR
);
544 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group
);
547 * sysfs_chmod_file - update the modified mode value on an object attribute.
548 * @kobj: object we're acting for.
549 * @attr: attribute descriptor.
550 * @mode: file permissions.
553 int sysfs_chmod_file(struct kobject
*kobj
, struct attribute
*attr
, mode_t mode
)
555 struct sysfs_dirent
*victim_sd
= NULL
;
556 struct dentry
*victim
= NULL
;
557 struct inode
* inode
;
558 struct iattr newattrs
;
562 victim_sd
= sysfs_get_dirent(kobj
->sd
, attr
->name
);
566 mutex_lock(&sysfs_rename_mutex
);
567 victim
= sysfs_get_dentry(victim_sd
);
568 mutex_unlock(&sysfs_rename_mutex
);
569 if (IS_ERR(victim
)) {
570 rc
= PTR_ERR(victim
);
575 inode
= victim
->d_inode
;
577 mutex_lock(&inode
->i_mutex
);
579 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
580 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
581 rc
= notify_change(victim
, &newattrs
);
584 mutex_lock(&sysfs_mutex
);
585 victim_sd
->s_mode
= newattrs
.ia_mode
;
586 mutex_unlock(&sysfs_mutex
);
589 mutex_unlock(&inode
->i_mutex
);
592 sysfs_put(victim_sd
);
595 EXPORT_SYMBOL_GPL(sysfs_chmod_file
);
599 * sysfs_remove_file - remove an object attribute.
600 * @kobj: object we're acting for.
601 * @attr: attribute descriptor.
603 * Hash the attribute name and kill the victim.
606 void sysfs_remove_file(struct kobject
* kobj
, const struct attribute
* attr
)
608 sysfs_hash_and_remove(kobj
->sd
, attr
->name
);
613 * sysfs_remove_file_from_group - remove an attribute file from a group.
614 * @kobj: object we're acting for.
615 * @attr: attribute descriptor.
616 * @group: group name.
618 void sysfs_remove_file_from_group(struct kobject
*kobj
,
619 const struct attribute
*attr
, const char *group
)
621 struct sysfs_dirent
*dir_sd
;
623 dir_sd
= sysfs_get_dirent(kobj
->sd
, group
);
625 sysfs_hash_and_remove(dir_sd
, attr
->name
);
629 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group
);
631 struct sysfs_schedule_callback_struct
{
632 struct kobject
*kobj
;
633 void (*func
)(void *);
635 struct module
*owner
;
636 struct work_struct work
;
639 static void sysfs_schedule_callback_work(struct work_struct
*work
)
641 struct sysfs_schedule_callback_struct
*ss
= container_of(work
,
642 struct sysfs_schedule_callback_struct
, work
);
644 (ss
->func
)(ss
->data
);
645 kobject_put(ss
->kobj
);
646 module_put(ss
->owner
);
651 * sysfs_schedule_callback - helper to schedule a callback for a kobject
652 * @kobj: object we're acting for.
653 * @func: callback function to invoke later.
654 * @data: argument to pass to @func.
655 * @owner: module owning the callback code
657 * sysfs attribute methods must not unregister themselves or their parent
658 * kobject (which would amount to the same thing). Attempts to do so will
659 * deadlock, since unregistration is mutually exclusive with driver
662 * Instead methods can call this routine, which will attempt to allocate
663 * and schedule a workqueue request to call back @func with @data as its
664 * argument in the workqueue's process context. @kobj will be pinned
665 * until @func returns.
667 * Returns 0 if the request was submitted, -ENOMEM if storage could not
668 * be allocated, -ENODEV if a reference to @owner isn't available.
670 int sysfs_schedule_callback(struct kobject
*kobj
, void (*func
)(void *),
671 void *data
, struct module
*owner
)
673 struct sysfs_schedule_callback_struct
*ss
;
675 if (!try_module_get(owner
))
677 ss
= kmalloc(sizeof(*ss
), GFP_KERNEL
);
687 INIT_WORK(&ss
->work
, sysfs_schedule_callback_work
);
688 schedule_work(&ss
->work
);
691 EXPORT_SYMBOL_GPL(sysfs_schedule_callback
);
694 EXPORT_SYMBOL_GPL(sysfs_create_file
);
695 EXPORT_SYMBOL_GPL(sysfs_remove_file
);