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 {
65 struct list_head buffers; /* goes through sysfs_buffer.list */
72 struct sysfs_ops * ops;
76 struct list_head list;
80 * fill_read_buffer - allocate and fill buffer from object.
81 * @dentry: dentry pointer.
82 * @buffer: data buffer for file.
84 * Allocate @buffer->page, if it hasn't been already, then call the
85 * kobject's show() method to fill the buffer with this attribute's
87 * This is called only once, on the file's first read unless an error
90 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
92 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
93 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
94 struct sysfs_ops * ops = buffer->ops;
99 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
103 /* need attr_sd for attr and ops, its parent for kobj */
104 if (!sysfs_get_active_two(attr_sd))
107 buffer->event = atomic_read(&attr_sd->s_event);
108 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
110 sysfs_put_active_two(attr_sd);
112 BUG_ON(count > (ssize_t)PAGE_SIZE);
114 buffer->needs_read_fill = 0;
115 buffer->count = count;
123 * sysfs_read_file - read an attribute.
124 * @file: file pointer.
125 * @buf: buffer to fill.
126 * @count: number of bytes to read.
127 * @ppos: starting offset in file.
129 * Userspace wants to read an attribute file. The attribute descriptor
130 * is in the file's ->d_fsdata. The target object is in the directory's
133 * We call fill_read_buffer() to allocate and fill the buffer from the
134 * object's show() method exactly once (if the read is happening from
135 * the beginning of the file). That should fill the entire buffer with
136 * all the data the object has to offer for that attribute.
137 * We then call flush_read_buffer() to copy the buffer to userspace
138 * in the increments specified.
142 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
144 struct sysfs_buffer * buffer = file->private_data;
147 mutex_lock(&buffer->mutex);
148 if (buffer->needs_read_fill) {
149 retval = fill_read_buffer(file->f_path.dentry,buffer);
153 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
154 __FUNCTION__, count, *ppos, buffer->page);
155 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
158 mutex_unlock(&buffer->mutex);
163 * fill_write_buffer - copy buffer from userspace.
164 * @buffer: data buffer for file.
165 * @buf: data from user.
166 * @count: number of bytes in @userbuf.
168 * Allocate @buffer->page if it hasn't been already, then
169 * copy the user-supplied buffer into it.
173 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
178 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
182 if (count >= PAGE_SIZE)
183 count = PAGE_SIZE - 1;
184 error = copy_from_user(buffer->page,buf,count);
185 buffer->needs_read_fill = 1;
186 /* if buf is assumed to contain a string, terminate it by \0,
187 so e.g. sscanf() can scan the string easily */
188 buffer->page[count] = 0;
189 return error ? -EFAULT : count;
194 * flush_write_buffer - push buffer to kobject.
195 * @dentry: dentry to the attribute
196 * @buffer: data buffer for file.
197 * @count: number of bytes
199 * Get the correct pointers for the kobject and the attribute we're
200 * dealing with, then call the store() method for the attribute,
201 * passing the buffer that we acquired in fill_write_buffer().
205 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
207 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
208 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
209 struct sysfs_ops * ops = buffer->ops;
212 /* need attr_sd for attr and ops, its parent for kobj */
213 if (!sysfs_get_active_two(attr_sd))
216 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
218 sysfs_put_active_two(attr_sd);
225 * sysfs_write_file - write an attribute.
226 * @file: file pointer
227 * @buf: data to write
228 * @count: number of bytes
229 * @ppos: starting offset
231 * Similar to sysfs_read_file(), though working in the opposite direction.
232 * We allocate and fill the data from the user in fill_write_buffer(),
233 * then push it to the kobject in flush_write_buffer().
234 * There is no easy way for us to know if userspace is only doing a partial
235 * write, so we don't support them. We expect the entire buffer to come
236 * on the first write.
237 * Hint: if you're writing a value, first read the file, modify only the
238 * the value you're changing, then write entire buffer back.
242 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
244 struct sysfs_buffer * buffer = file->private_data;
247 mutex_lock(&buffer->mutex);
248 len = fill_write_buffer(buffer, buf, count);
250 len = flush_write_buffer(file->f_path.dentry, buffer, len);
253 mutex_unlock(&buffer->mutex);
258 * sysfs_get_open_dirent - get or create sysfs_open_dirent
259 * @sd: target sysfs_dirent
260 * @buffer: sysfs_buffer for this instance of open
262 * If @sd->s_attr.open exists, increment its reference count;
263 * otherwise, create one. @buffer is chained to the buffers
267 * Kernel thread context (may sleep).
270 * 0 on success, -errno on failure.
272 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
273 struct sysfs_buffer *buffer)
275 struct sysfs_open_dirent *od, *new_od = NULL;
278 spin_lock(&sysfs_open_dirent_lock);
280 if (!sd->s_attr.open && new_od) {
281 sd->s_attr.open = new_od;
285 od = sd->s_attr.open;
287 atomic_inc(&od->refcnt);
288 list_add_tail(&buffer->list, &od->buffers);
291 spin_unlock(&sysfs_open_dirent_lock);
298 /* not there, initialize a new one and retry */
299 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
303 atomic_set(&new_od->refcnt, 0);
304 INIT_LIST_HEAD(&new_od->buffers);
309 * sysfs_put_open_dirent - put sysfs_open_dirent
310 * @sd: target sysfs_dirent
311 * @buffer: associated sysfs_buffer
313 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
314 * If reference count reaches zero, disassociate and free it.
319 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
320 struct sysfs_buffer *buffer)
322 struct sysfs_open_dirent *od = sd->s_attr.open;
324 spin_lock(&sysfs_open_dirent_lock);
326 list_del(&buffer->list);
327 if (atomic_dec_and_test(&od->refcnt))
328 sd->s_attr.open = NULL;
332 spin_unlock(&sysfs_open_dirent_lock);
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 struct sysfs_ops * ops = NULL;
345 /* need attr_sd for attr and ops, its parent for kobj */
346 if (!sysfs_get_active_two(attr_sd))
349 /* if the kobject has no ktype, then we assume that it is a subsystem
350 * itself, and use ops for it.
352 if (kobj->kset && kobj->kset->ktype)
353 ops = kobj->kset->ktype->sysfs_ops;
354 else if (kobj->ktype)
355 ops = kobj->ktype->sysfs_ops;
357 ops = &subsys_sysfs_ops;
361 /* No sysfs operations, either from having no subsystem,
362 * or the subsystem have no operations.
367 /* File needs write support.
368 * The inode's perms must say it's ok,
369 * and we must have a store method.
371 if (file->f_mode & FMODE_WRITE) {
372 if (!(inode->i_mode & S_IWUGO) || !ops->store)
376 /* File needs read support.
377 * The inode's perms must say it's ok, and we there
378 * must be a show method for it.
380 if (file->f_mode & FMODE_READ) {
381 if (!(inode->i_mode & S_IRUGO) || !ops->show)
385 /* No error? Great, allocate a buffer for the file, and store it
386 * it in file->private_data for easy access.
389 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
393 mutex_init(&buffer->mutex);
394 buffer->needs_read_fill = 1;
396 file->private_data = buffer;
398 /* make sure we have open dirent struct */
399 error = sysfs_get_open_dirent(attr_sd, buffer);
403 /* open succeeded, put active references */
404 sysfs_put_active_two(attr_sd);
410 sysfs_put_active_two(attr_sd);
414 static int sysfs_release(struct inode *inode, struct file *filp)
416 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
417 struct sysfs_buffer *buffer = filp->private_data;
419 sysfs_put_open_dirent(sd, buffer);
422 free_page((unsigned long)buffer->page);
428 /* Sysfs attribute files are pollable. The idea is that you read
429 * the content and then you use 'poll' or 'select' to wait for
430 * the content to change. When the content changes (assuming the
431 * manager for the kobject supports notification), poll will
432 * return POLLERR|POLLPRI, and select will return the fd whether
433 * it is waiting for read, write, or exceptions.
434 * Once poll/select indicates that the value has changed, you
435 * need to close and re-open the file, as simply seeking and reading
436 * again will not get new data, or reset the state of 'poll'.
437 * Reminder: this only works for attributes which actively support
438 * it, and it is not possible to test an attribute from userspace
439 * to see if it supports poll (Neither 'poll' nor 'select' return
440 * an appropriate error code). When in doubt, set a suitable timeout value.
442 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
444 struct sysfs_buffer * buffer = filp->private_data;
445 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
446 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
448 /* need parent for the kobj, grab both */
449 if (!sysfs_get_active_two(attr_sd))
452 poll_wait(filp, &kobj->poll, wait);
454 sysfs_put_active_two(attr_sd);
456 if (buffer->event != atomic_read(&attr_sd->s_event))
462 buffer->needs_read_fill = 1;
463 return POLLERR|POLLPRI;
466 void sysfs_notify(struct kobject *k, char *dir, char *attr)
468 struct sysfs_dirent *sd = k->sd;
470 mutex_lock(&sysfs_mutex);
473 sd = sysfs_find_dirent(sd, dir);
475 sd = sysfs_find_dirent(sd, attr);
477 atomic_inc(&sd->s_event);
478 wake_up_interruptible(&k->poll);
481 mutex_unlock(&sysfs_mutex);
483 EXPORT_SYMBOL_GPL(sysfs_notify);
485 const struct file_operations sysfs_file_operations = {
486 .read = sysfs_read_file,
487 .write = sysfs_write_file,
488 .llseek = generic_file_llseek,
489 .open = sysfs_open_file,
490 .release = sysfs_release,
495 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
498 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
499 struct sysfs_addrm_cxt acxt;
500 struct sysfs_dirent *sd;
503 sd = sysfs_new_dirent(attr->name, mode, type);
506 sd->s_attr.attr = (void *)attr;
508 sysfs_addrm_start(&acxt, dir_sd);
509 rc = sysfs_add_one(&acxt, sd);
510 sysfs_addrm_finish(&acxt);
520 * sysfs_create_file - create an attribute file for an object.
521 * @kobj: object we're creating for.
522 * @attr: atrribute descriptor.
525 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
527 BUG_ON(!kobj || !kobj->sd || !attr);
529 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
535 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
536 * @kobj: object we're acting for.
537 * @attr: attribute descriptor.
538 * @group: group name.
540 int sysfs_add_file_to_group(struct kobject *kobj,
541 const struct attribute *attr, const char *group)
543 struct sysfs_dirent *dir_sd;
546 dir_sd = sysfs_get_dirent(kobj->sd, group);
550 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
555 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
558 * sysfs_chmod_file - update the modified mode value on an object attribute.
559 * @kobj: object we're acting for.
560 * @attr: attribute descriptor.
561 * @mode: file permissions.
564 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
566 struct sysfs_dirent *victim_sd = NULL;
567 struct dentry *victim = NULL;
568 struct inode * inode;
569 struct iattr newattrs;
573 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
577 mutex_lock(&sysfs_rename_mutex);
578 victim = sysfs_get_dentry(victim_sd);
579 mutex_unlock(&sysfs_rename_mutex);
580 if (IS_ERR(victim)) {
581 rc = PTR_ERR(victim);
586 inode = victim->d_inode;
588 mutex_lock(&inode->i_mutex);
590 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
591 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
592 rc = notify_change(victim, &newattrs);
595 mutex_lock(&sysfs_mutex);
596 victim_sd->s_mode = newattrs.ia_mode;
597 mutex_unlock(&sysfs_mutex);
600 mutex_unlock(&inode->i_mutex);
603 sysfs_put(victim_sd);
606 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
610 * sysfs_remove_file - remove an object attribute.
611 * @kobj: object we're acting for.
612 * @attr: attribute descriptor.
614 * Hash the attribute name and kill the victim.
617 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
619 sysfs_hash_and_remove(kobj->sd, attr->name);
624 * sysfs_remove_file_from_group - remove an attribute file from a group.
625 * @kobj: object we're acting for.
626 * @attr: attribute descriptor.
627 * @group: group name.
629 void sysfs_remove_file_from_group(struct kobject *kobj,
630 const struct attribute *attr, const char *group)
632 struct sysfs_dirent *dir_sd;
634 dir_sd = sysfs_get_dirent(kobj->sd, group);
636 sysfs_hash_and_remove(dir_sd, attr->name);
640 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
642 struct sysfs_schedule_callback_struct {
643 struct kobject *kobj;
644 void (*func)(void *);
646 struct module *owner;
647 struct work_struct work;
650 static void sysfs_schedule_callback_work(struct work_struct *work)
652 struct sysfs_schedule_callback_struct *ss = container_of(work,
653 struct sysfs_schedule_callback_struct, work);
655 (ss->func)(ss->data);
656 kobject_put(ss->kobj);
657 module_put(ss->owner);
662 * sysfs_schedule_callback - helper to schedule a callback for a kobject
663 * @kobj: object we're acting for.
664 * @func: callback function to invoke later.
665 * @data: argument to pass to @func.
666 * @owner: module owning the callback code
668 * sysfs attribute methods must not unregister themselves or their parent
669 * kobject (which would amount to the same thing). Attempts to do so will
670 * deadlock, since unregistration is mutually exclusive with driver
673 * Instead methods can call this routine, which will attempt to allocate
674 * and schedule a workqueue request to call back @func with @data as its
675 * argument in the workqueue's process context. @kobj will be pinned
676 * until @func returns.
678 * Returns 0 if the request was submitted, -ENOMEM if storage could not
679 * be allocated, -ENODEV if a reference to @owner isn't available.
681 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
682 void *data, struct module *owner)
684 struct sysfs_schedule_callback_struct *ss;
686 if (!try_module_get(owner))
688 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
698 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
699 schedule_work(&ss->work);
702 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
705 EXPORT_SYMBOL_GPL(sysfs_create_file);
706 EXPORT_SYMBOL_GPL(sysfs_remove_file);