2 * file.c - operations for regular (text) files.
5 #include <linux/module.h>
6 #include <linux/kobject.h>
7 #include <linux/namei.h>
8 #include <linux/poll.h>
9 #include <linux/list.h>
10 #include <linux/mutex.h>
11 #include <asm/uaccess.h>
15 #define to_sattr(a) container_of(a,struct subsys_attribute, attr)
18 * Subsystem file operations.
19 * These operations allow subsystems to have files that can be
23 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
25 struct kset *kset = to_kset(kobj);
26 struct subsys_attribute * sattr = to_sattr(attr);
30 ret = sattr->show(kset, page);
35 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
38 struct kset *kset = to_kset(kobj);
39 struct subsys_attribute * sattr = to_sattr(attr);
43 ret = sattr->store(kset, page, count);
47 static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
53 * There's one sysfs_buffer for each open file and one
54 * sysfs_open_dirent for each sysfs_dirent with one or more open
57 * filp->private_data points to sysfs_buffer and
58 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
59 * is protected by sysfs_open_dirent_lock.
61 static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED;
63 struct sysfs_open_dirent {
66 wait_queue_head_t poll;
67 struct list_head buffers; /* goes through sysfs_buffer.list */
74 struct sysfs_ops * ops;
78 struct list_head list;
82 * fill_read_buffer - allocate and fill buffer from object.
83 * @dentry: dentry pointer.
84 * @buffer: data buffer for file.
86 * Allocate @buffer->page, if it hasn't been already, then call the
87 * kobject's show() method to fill the buffer with this attribute's
89 * This is called only once, on the file's first read unless an error
92 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
94 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
95 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
96 struct sysfs_ops * ops = buffer->ops;
101 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
105 /* need attr_sd for attr and ops, its parent for kobj */
106 if (!sysfs_get_active_two(attr_sd))
109 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
110 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
112 sysfs_put_active_two(attr_sd);
114 BUG_ON(count > (ssize_t)PAGE_SIZE);
116 buffer->needs_read_fill = 0;
117 buffer->count = count;
125 * sysfs_read_file - read an attribute.
126 * @file: file pointer.
127 * @buf: buffer to fill.
128 * @count: number of bytes to read.
129 * @ppos: starting offset in file.
131 * Userspace wants to read an attribute file. The attribute descriptor
132 * is in the file's ->d_fsdata. The target object is in the directory's
135 * We call fill_read_buffer() to allocate and fill the buffer from the
136 * object's show() method exactly once (if the read is happening from
137 * the beginning of the file). That should fill the entire buffer with
138 * all the data the object has to offer for that attribute.
139 * We then call flush_read_buffer() to copy the buffer to userspace
140 * in the increments specified.
144 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
146 struct sysfs_buffer * buffer = file->private_data;
149 mutex_lock(&buffer->mutex);
150 if (buffer->needs_read_fill) {
151 retval = fill_read_buffer(file->f_path.dentry,buffer);
155 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
156 __FUNCTION__, count, *ppos, buffer->page);
157 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
160 mutex_unlock(&buffer->mutex);
165 * fill_write_buffer - copy buffer from userspace.
166 * @buffer: data buffer for file.
167 * @buf: data from user.
168 * @count: number of bytes in @userbuf.
170 * Allocate @buffer->page if it hasn't been already, then
171 * copy the user-supplied buffer into it.
175 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
180 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
184 if (count >= PAGE_SIZE)
185 count = PAGE_SIZE - 1;
186 error = copy_from_user(buffer->page,buf,count);
187 buffer->needs_read_fill = 1;
188 /* if buf is assumed to contain a string, terminate it by \0,
189 so e.g. sscanf() can scan the string easily */
190 buffer->page[count] = 0;
191 return error ? -EFAULT : count;
196 * flush_write_buffer - push buffer to kobject.
197 * @dentry: dentry to the attribute
198 * @buffer: data buffer for file.
199 * @count: number of bytes
201 * Get the correct pointers for the kobject and the attribute we're
202 * dealing with, then call the store() method for the attribute,
203 * passing the buffer that we acquired in fill_write_buffer().
207 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
209 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
210 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
211 struct sysfs_ops * ops = buffer->ops;
214 /* need attr_sd for attr and ops, its parent for kobj */
215 if (!sysfs_get_active_two(attr_sd))
218 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
220 sysfs_put_active_two(attr_sd);
227 * sysfs_write_file - write an attribute.
228 * @file: file pointer
229 * @buf: data to write
230 * @count: number of bytes
231 * @ppos: starting offset
233 * Similar to sysfs_read_file(), though working in the opposite direction.
234 * We allocate and fill the data from the user in fill_write_buffer(),
235 * then push it to the kobject in flush_write_buffer().
236 * There is no easy way for us to know if userspace is only doing a partial
237 * write, so we don't support them. We expect the entire buffer to come
238 * on the first write.
239 * Hint: if you're writing a value, first read the file, modify only the
240 * the value you're changing, then write entire buffer back.
244 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
246 struct sysfs_buffer * buffer = file->private_data;
249 mutex_lock(&buffer->mutex);
250 len = fill_write_buffer(buffer, buf, count);
252 len = flush_write_buffer(file->f_path.dentry, buffer, len);
255 mutex_unlock(&buffer->mutex);
260 * sysfs_get_open_dirent - get or create sysfs_open_dirent
261 * @sd: target sysfs_dirent
262 * @buffer: sysfs_buffer for this instance of open
264 * If @sd->s_attr.open exists, increment its reference count;
265 * otherwise, create one. @buffer is chained to the buffers
269 * Kernel thread context (may sleep).
272 * 0 on success, -errno on failure.
274 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
275 struct sysfs_buffer *buffer)
277 struct sysfs_open_dirent *od, *new_od = NULL;
280 spin_lock(&sysfs_open_dirent_lock);
282 if (!sd->s_attr.open && new_od) {
283 sd->s_attr.open = new_od;
287 od = sd->s_attr.open;
289 atomic_inc(&od->refcnt);
290 list_add_tail(&buffer->list, &od->buffers);
293 spin_unlock(&sysfs_open_dirent_lock);
300 /* not there, initialize a new one and retry */
301 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
305 atomic_set(&new_od->refcnt, 0);
306 atomic_set(&new_od->event, 1);
307 init_waitqueue_head(&new_od->poll);
308 INIT_LIST_HEAD(&new_od->buffers);
313 * sysfs_put_open_dirent - put sysfs_open_dirent
314 * @sd: target sysfs_dirent
315 * @buffer: associated sysfs_buffer
317 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
318 * If reference count reaches zero, disassociate and free it.
323 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
324 struct sysfs_buffer *buffer)
326 struct sysfs_open_dirent *od = sd->s_attr.open;
328 spin_lock(&sysfs_open_dirent_lock);
330 list_del(&buffer->list);
331 if (atomic_dec_and_test(&od->refcnt))
332 sd->s_attr.open = NULL;
336 spin_unlock(&sysfs_open_dirent_lock);
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_buffer * buffer;
346 struct sysfs_ops * ops = NULL;
349 /* need attr_sd for attr and ops, its parent for kobj */
350 if (!sysfs_get_active_two(attr_sd))
353 /* if the kobject has no ktype, then we assume that it is a subsystem
354 * itself, and use ops for it.
356 if (kobj->kset && kobj->kset->ktype)
357 ops = kobj->kset->ktype->sysfs_ops;
358 else if (kobj->ktype)
359 ops = kobj->ktype->sysfs_ops;
361 ops = &subsys_sysfs_ops;
365 /* No sysfs operations, either from having no subsystem,
366 * or the subsystem have no operations.
371 /* File needs write support.
372 * The inode's perms must say it's ok,
373 * and we must have a store method.
375 if (file->f_mode & FMODE_WRITE) {
376 if (!(inode->i_mode & S_IWUGO) || !ops->store)
380 /* File needs read support.
381 * The inode's perms must say it's ok, and we there
382 * must be a show method for it.
384 if (file->f_mode & FMODE_READ) {
385 if (!(inode->i_mode & S_IRUGO) || !ops->show)
389 /* No error? Great, allocate a buffer for the file, and store it
390 * it in file->private_data for easy access.
393 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
397 mutex_init(&buffer->mutex);
398 buffer->needs_read_fill = 1;
400 file->private_data = buffer;
402 /* make sure we have open dirent struct */
403 error = sysfs_get_open_dirent(attr_sd, buffer);
407 /* open succeeded, put active references */
408 sysfs_put_active_two(attr_sd);
414 sysfs_put_active_two(attr_sd);
418 static int sysfs_release(struct inode *inode, struct file *filp)
420 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
421 struct sysfs_buffer *buffer = filp->private_data;
423 sysfs_put_open_dirent(sd, buffer);
426 free_page((unsigned long)buffer->page);
432 /* Sysfs attribute files are pollable. The idea is that you read
433 * the content and then you use 'poll' or 'select' to wait for
434 * the content to change. When the content changes (assuming the
435 * manager for the kobject supports notification), poll will
436 * return POLLERR|POLLPRI, and select will return the fd whether
437 * it is waiting for read, write, or exceptions.
438 * Once poll/select indicates that the value has changed, you
439 * need to close and re-open the file, as simply seeking and reading
440 * again will not get new data, or reset the state of 'poll'.
441 * Reminder: this only works for attributes which actively support
442 * it, and it is not possible to test an attribute from userspace
443 * to see if it supports poll (Neither 'poll' nor 'select' return
444 * an appropriate error code). When in doubt, set a suitable timeout value.
446 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
448 struct sysfs_buffer * buffer = filp->private_data;
449 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
450 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
452 /* need parent for the kobj, grab both */
453 if (!sysfs_get_active_two(attr_sd))
456 poll_wait(filp, &od->poll, wait);
458 sysfs_put_active_two(attr_sd);
460 if (buffer->event != atomic_read(&od->event))
466 buffer->needs_read_fill = 1;
467 return POLLERR|POLLPRI;
470 void sysfs_notify(struct kobject *k, char *dir, char *attr)
472 struct sysfs_dirent *sd = k->sd;
474 mutex_lock(&sysfs_mutex);
477 sd = sysfs_find_dirent(sd, dir);
479 sd = sysfs_find_dirent(sd, attr);
481 struct sysfs_open_dirent *od;
483 spin_lock(&sysfs_open_dirent_lock);
485 od = sd->s_attr.open;
487 atomic_inc(&od->event);
488 wake_up_interruptible(&od->poll);
491 spin_unlock(&sysfs_open_dirent_lock);
494 mutex_unlock(&sysfs_mutex);
496 EXPORT_SYMBOL_GPL(sysfs_notify);
498 const struct file_operations sysfs_file_operations = {
499 .read = sysfs_read_file,
500 .write = sysfs_write_file,
501 .llseek = generic_file_llseek,
502 .open = sysfs_open_file,
503 .release = sysfs_release,
508 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
511 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
512 struct sysfs_addrm_cxt acxt;
513 struct sysfs_dirent *sd;
516 sd = sysfs_new_dirent(attr->name, mode, type);
519 sd->s_attr.attr = (void *)attr;
521 sysfs_addrm_start(&acxt, dir_sd);
522 rc = sysfs_add_one(&acxt, sd);
523 sysfs_addrm_finish(&acxt);
533 * sysfs_create_file - create an attribute file for an object.
534 * @kobj: object we're creating for.
535 * @attr: atrribute descriptor.
538 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
540 BUG_ON(!kobj || !kobj->sd || !attr);
542 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
548 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
549 * @kobj: object we're acting for.
550 * @attr: attribute descriptor.
551 * @group: group name.
553 int sysfs_add_file_to_group(struct kobject *kobj,
554 const struct attribute *attr, const char *group)
556 struct sysfs_dirent *dir_sd;
559 dir_sd = sysfs_get_dirent(kobj->sd, group);
563 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
568 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
571 * sysfs_chmod_file - update the modified mode value on an object attribute.
572 * @kobj: object we're acting for.
573 * @attr: attribute descriptor.
574 * @mode: file permissions.
577 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
579 struct sysfs_dirent *victim_sd = NULL;
580 struct dentry *victim = NULL;
581 struct inode * inode;
582 struct iattr newattrs;
586 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
590 mutex_lock(&sysfs_rename_mutex);
591 victim = sysfs_get_dentry(victim_sd);
592 mutex_unlock(&sysfs_rename_mutex);
593 if (IS_ERR(victim)) {
594 rc = PTR_ERR(victim);
599 inode = victim->d_inode;
601 mutex_lock(&inode->i_mutex);
603 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
604 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
605 rc = notify_change(victim, &newattrs);
608 mutex_lock(&sysfs_mutex);
609 victim_sd->s_mode = newattrs.ia_mode;
610 mutex_unlock(&sysfs_mutex);
613 mutex_unlock(&inode->i_mutex);
616 sysfs_put(victim_sd);
619 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
623 * sysfs_remove_file - remove an object attribute.
624 * @kobj: object we're acting for.
625 * @attr: attribute descriptor.
627 * Hash the attribute name and kill the victim.
630 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
632 sysfs_hash_and_remove(kobj->sd, attr->name);
637 * sysfs_remove_file_from_group - remove an attribute file from a group.
638 * @kobj: object we're acting for.
639 * @attr: attribute descriptor.
640 * @group: group name.
642 void sysfs_remove_file_from_group(struct kobject *kobj,
643 const struct attribute *attr, const char *group)
645 struct sysfs_dirent *dir_sd;
647 dir_sd = sysfs_get_dirent(kobj->sd, group);
649 sysfs_hash_and_remove(dir_sd, attr->name);
653 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
655 struct sysfs_schedule_callback_struct {
656 struct kobject *kobj;
657 void (*func)(void *);
659 struct module *owner;
660 struct work_struct work;
663 static void sysfs_schedule_callback_work(struct work_struct *work)
665 struct sysfs_schedule_callback_struct *ss = container_of(work,
666 struct sysfs_schedule_callback_struct, work);
668 (ss->func)(ss->data);
669 kobject_put(ss->kobj);
670 module_put(ss->owner);
675 * sysfs_schedule_callback - helper to schedule a callback for a kobject
676 * @kobj: object we're acting for.
677 * @func: callback function to invoke later.
678 * @data: argument to pass to @func.
679 * @owner: module owning the callback code
681 * sysfs attribute methods must not unregister themselves or their parent
682 * kobject (which would amount to the same thing). Attempts to do so will
683 * deadlock, since unregistration is mutually exclusive with driver
686 * Instead methods can call this routine, which will attempt to allocate
687 * and schedule a workqueue request to call back @func with @data as its
688 * argument in the workqueue's process context. @kobj will be pinned
689 * until @func returns.
691 * Returns 0 if the request was submitted, -ENOMEM if storage could not
692 * be allocated, -ENODEV if a reference to @owner isn't available.
694 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
695 void *data, struct module *owner)
697 struct sysfs_schedule_callback_struct *ss;
699 if (!try_module_get(owner))
701 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
711 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
712 schedule_work(&ss->work);
715 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
718 EXPORT_SYMBOL_GPL(sysfs_create_file);
719 EXPORT_SYMBOL_GPL(sysfs_remove_file);