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>
30 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
31 * for each sysfs_dirent with one or more open files.
33 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
36 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
38 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
40 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
41 static DEFINE_MUTEX(sysfs_open_file_mutex);
43 struct sysfs_open_dirent {
46 wait_queue_head_t poll;
47 struct list_head files; /* goes through sysfs_open_file.list */
50 struct sysfs_open_file {
51 struct sysfs_dirent *sd;
55 struct list_head list;
58 const struct vm_operations_struct *vm_ops;
61 static bool sysfs_is_bin(struct sysfs_dirent *sd)
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
66 static struct sysfs_open_file *sysfs_of(struct file *file)
68 return ((struct seq_file *)file->private_data)->private;
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference.
75 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
77 struct kobject *kobj = sd->s_parent->s_dir.kobj;
79 if (!sysfs_ignore_lockdep(sd))
80 lockdep_assert_held(sd);
81 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
85 * Reads on sysfs are handled through seq_file, which takes care of hairy
86 * details like buffering and seeking. The following function pipes
87 * sysfs_ops->show() result through seq_file.
89 static int sysfs_seq_show(struct seq_file *sf, void *v)
91 struct sysfs_open_file *of = sf->private;
92 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
93 const struct sysfs_ops *ops;
97 /* acquire buffer and ensure that it's >= PAGE_SIZE */
98 count = seq_get_buf(sf, &buf);
99 if (count < PAGE_SIZE) {
105 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
106 * nests outside active ref and is just to ensure that the ops
107 * aren't called concurrently for the same open file.
109 mutex_lock(&of->mutex);
110 if (!sysfs_get_active(of->sd)) {
111 mutex_unlock(&of->mutex);
115 of->event = atomic_read(&of->sd->s_attr.open->event);
118 * Lookup @ops and invoke show(). Control may reach here via seq
119 * file lseek even if @ops->show() isn't implemented.
121 ops = sysfs_file_ops(of->sd);
123 count = ops->show(kobj, of->sd->s_attr.attr, buf);
127 sysfs_put_active(of->sd);
128 mutex_unlock(&of->mutex);
134 * The code works fine with PAGE_SIZE return but it's likely to
135 * indicate truncated result or overflow in normal use cases.
137 if (count >= (ssize_t)PAGE_SIZE) {
138 print_symbol("fill_read_buffer: %s returned bad count\n",
139 (unsigned long)ops->show);
140 /* Try to struggle along */
141 count = PAGE_SIZE - 1;
143 seq_commit(sf, count);
148 * Read method for bin files. As reading a bin file can have side-effects,
149 * the exact offset and bytes specified in read(2) call should be passed to
150 * the read callback making it difficult to use seq_file. Implement
151 * simplistic custom buffering for bin files.
153 static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
154 size_t bytes, loff_t *off)
156 struct sysfs_open_file *of = sysfs_of(file);
157 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
158 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
159 loff_t size = file_inode(file)->i_size;
160 int count = min_t(size_t, bytes, PAGE_SIZE);
170 if (offs + count > size)
174 buf = kmalloc(count, GFP_KERNEL);
178 /* need of->sd for battr, its parent for kobj */
179 mutex_lock(&of->mutex);
180 if (!sysfs_get_active(of->sd)) {
182 mutex_unlock(&of->mutex);
187 count = battr->read(file, kobj, battr, buf, offs, count);
191 sysfs_put_active(of->sd);
192 mutex_unlock(&of->mutex);
197 if (copy_to_user(userbuf, buf, count)) {
202 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
212 * flush_write_buffer - push buffer to kobject
214 * @buf: data buffer for file
215 * @off: file offset to write to
216 * @count: number of bytes
218 * Get the correct pointers for the kobject and the attribute we're dealing
219 * with, then call the store() method for it with @buf.
221 static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
224 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
228 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
229 * nests outside active ref and is just to ensure that the ops
230 * aren't called concurrently for the same open file.
232 mutex_lock(&of->mutex);
233 if (!sysfs_get_active(of->sd)) {
234 mutex_unlock(&of->mutex);
238 if (sysfs_is_bin(of->sd)) {
239 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
243 rc = battr->write(of->file, kobj, battr, buf, off,
246 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
248 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
251 sysfs_put_active(of->sd);
252 mutex_unlock(&of->mutex);
258 * sysfs_write_file - write an attribute
259 * @file: file pointer
260 * @user_buf: data to write
261 * @count: number of bytes
262 * @ppos: starting offset
264 * Copy data in from userland and pass it to the matching
265 * sysfs_ops->store() by invoking flush_write_buffer().
267 * There is no easy way for us to know if userspace is only doing a partial
268 * write, so we don't support them. We expect the entire buffer to come on
269 * the first write. Hint: if you're writing a value, first read the file,
270 * modify only the the value you're changing, then write entire buffer
273 static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
274 size_t count, loff_t *ppos)
276 struct sysfs_open_file *of = sysfs_of(file);
277 ssize_t len = min_t(size_t, count, PAGE_SIZE);
280 if (sysfs_is_bin(of->sd)) {
281 loff_t size = file_inode(file)->i_size;
285 len = min_t(ssize_t, len, size - *ppos);
291 buf = kmalloc(len + 1, GFP_KERNEL);
295 if (copy_from_user(buf, user_buf, len)) {
299 buf[len] = '\0'; /* guarantee string termination */
301 len = flush_write_buffer(of, buf, *ppos, len);
309 static void sysfs_bin_vma_open(struct vm_area_struct *vma)
311 struct file *file = vma->vm_file;
312 struct sysfs_open_file *of = sysfs_of(file);
317 if (!sysfs_get_active(of->sd))
320 if (of->vm_ops->open)
321 of->vm_ops->open(vma);
323 sysfs_put_active(of->sd);
326 static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
328 struct file *file = vma->vm_file;
329 struct sysfs_open_file *of = sysfs_of(file);
333 return VM_FAULT_SIGBUS;
335 if (!sysfs_get_active(of->sd))
336 return VM_FAULT_SIGBUS;
338 ret = VM_FAULT_SIGBUS;
339 if (of->vm_ops->fault)
340 ret = of->vm_ops->fault(vma, vmf);
342 sysfs_put_active(of->sd);
346 static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
347 struct vm_fault *vmf)
349 struct file *file = vma->vm_file;
350 struct sysfs_open_file *of = sysfs_of(file);
354 return VM_FAULT_SIGBUS;
356 if (!sysfs_get_active(of->sd))
357 return VM_FAULT_SIGBUS;
360 if (of->vm_ops->page_mkwrite)
361 ret = of->vm_ops->page_mkwrite(vma, vmf);
363 file_update_time(file);
365 sysfs_put_active(of->sd);
369 static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
370 void *buf, int len, int write)
372 struct file *file = vma->vm_file;
373 struct sysfs_open_file *of = sysfs_of(file);
379 if (!sysfs_get_active(of->sd))
383 if (of->vm_ops->access)
384 ret = of->vm_ops->access(vma, addr, buf, len, write);
386 sysfs_put_active(of->sd);
391 static int sysfs_bin_set_policy(struct vm_area_struct *vma,
392 struct mempolicy *new)
394 struct file *file = vma->vm_file;
395 struct sysfs_open_file *of = sysfs_of(file);
401 if (!sysfs_get_active(of->sd))
405 if (of->vm_ops->set_policy)
406 ret = of->vm_ops->set_policy(vma, new);
408 sysfs_put_active(of->sd);
412 static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
415 struct file *file = vma->vm_file;
416 struct sysfs_open_file *of = sysfs_of(file);
417 struct mempolicy *pol;
420 return vma->vm_policy;
422 if (!sysfs_get_active(of->sd))
423 return vma->vm_policy;
425 pol = vma->vm_policy;
426 if (of->vm_ops->get_policy)
427 pol = of->vm_ops->get_policy(vma, addr);
429 sysfs_put_active(of->sd);
433 static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
434 const nodemask_t *to, unsigned long flags)
436 struct file *file = vma->vm_file;
437 struct sysfs_open_file *of = sysfs_of(file);
443 if (!sysfs_get_active(of->sd))
447 if (of->vm_ops->migrate)
448 ret = of->vm_ops->migrate(vma, from, to, flags);
450 sysfs_put_active(of->sd);
455 static const struct vm_operations_struct sysfs_bin_vm_ops = {
456 .open = sysfs_bin_vma_open,
457 .fault = sysfs_bin_fault,
458 .page_mkwrite = sysfs_bin_page_mkwrite,
459 .access = sysfs_bin_access,
461 .set_policy = sysfs_bin_set_policy,
462 .get_policy = sysfs_bin_get_policy,
463 .migrate = sysfs_bin_migrate,
467 static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
469 struct sysfs_open_file *of = sysfs_of(file);
470 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
471 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
474 mutex_lock(&of->mutex);
476 /* need of->sd for battr, its parent for kobj */
478 if (!sysfs_get_active(of->sd))
485 rc = battr->mmap(file, kobj, battr, vma);
490 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
491 * to satisfy versions of X which crash if the mmap fails: that
492 * substitutes a new vm_file, and we don't then want bin_vm_ops.
494 if (vma->vm_file != file)
498 if (of->mmapped && of->vm_ops != vma->vm_ops)
502 * It is not possible to successfully wrap close.
503 * So error if someone is trying to use close.
506 if (vma->vm_ops && vma->vm_ops->close)
511 of->vm_ops = vma->vm_ops;
512 vma->vm_ops = &sysfs_bin_vm_ops;
514 sysfs_put_active(of->sd);
516 mutex_unlock(&of->mutex);
522 * sysfs_get_open_dirent - get or create sysfs_open_dirent
523 * @sd: target sysfs_dirent
524 * @of: sysfs_open_file for this instance of open
526 * If @sd->s_attr.open exists, increment its reference count;
527 * otherwise, create one. @of is chained to the files list.
530 * Kernel thread context (may sleep).
533 * 0 on success, -errno on failure.
535 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
536 struct sysfs_open_file *of)
538 struct sysfs_open_dirent *od, *new_od = NULL;
541 mutex_lock(&sysfs_open_file_mutex);
542 spin_lock_irq(&sysfs_open_dirent_lock);
544 if (!sd->s_attr.open && new_od) {
545 sd->s_attr.open = new_od;
549 od = sd->s_attr.open;
551 atomic_inc(&od->refcnt);
552 list_add_tail(&of->list, &od->files);
555 spin_unlock_irq(&sysfs_open_dirent_lock);
556 mutex_unlock(&sysfs_open_file_mutex);
563 /* not there, initialize a new one and retry */
564 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
568 atomic_set(&new_od->refcnt, 0);
569 atomic_set(&new_od->event, 1);
570 init_waitqueue_head(&new_od->poll);
571 INIT_LIST_HEAD(&new_od->files);
576 * sysfs_put_open_dirent - put sysfs_open_dirent
577 * @sd: target sysfs_dirent
578 * @of: associated sysfs_open_file
580 * Put @sd->s_attr.open and unlink @of from the files list. If
581 * reference count reaches zero, disassociate and free it.
586 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
587 struct sysfs_open_file *of)
589 struct sysfs_open_dirent *od = sd->s_attr.open;
592 mutex_lock(&sysfs_open_file_mutex);
593 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
598 if (atomic_dec_and_test(&od->refcnt))
599 sd->s_attr.open = NULL;
603 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
604 mutex_unlock(&sysfs_open_file_mutex);
609 static int sysfs_open_file(struct inode *inode, struct file *file)
611 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
612 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
613 struct sysfs_open_file *of;
614 bool has_read, has_write;
617 /* need attr_sd for attr and ops, its parent for kobj */
618 if (!sysfs_get_active(attr_sd))
621 if (sysfs_is_bin(attr_sd)) {
622 struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
624 has_read = battr->read || battr->mmap;
625 has_write = battr->write || battr->mmap;
627 const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
629 /* every kobject with an attribute needs a ktype assigned */
630 if (WARN(!ops, KERN_ERR
631 "missing sysfs attribute operations for kobject: %s\n",
635 has_read = ops->show;
636 has_write = ops->store;
639 /* check perms and supported operations */
640 if ((file->f_mode & FMODE_WRITE) &&
641 (!(inode->i_mode & S_IWUGO) || !has_write))
644 if ((file->f_mode & FMODE_READ) &&
645 (!(inode->i_mode & S_IRUGO) || !has_read))
648 /* allocate a sysfs_open_file for the file */
650 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
654 mutex_init(&of->mutex);
659 * Always instantiate seq_file even if read access doesn't use
660 * seq_file or is not requested. This unifies private data access
661 * and readable regular files are the vast majority anyway.
663 if (sysfs_is_bin(attr_sd))
664 error = single_open(file, NULL, of);
666 error = single_open(file, sysfs_seq_show, of);
670 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
671 if (file->f_mode & FMODE_WRITE)
672 file->f_mode |= FMODE_PWRITE;
674 /* make sure we have open dirent struct */
675 error = sysfs_get_open_dirent(attr_sd, of);
679 /* open succeeded, put active references */
680 sysfs_put_active(attr_sd);
684 single_release(inode, file);
688 sysfs_put_active(attr_sd);
692 static int sysfs_release(struct inode *inode, struct file *filp)
694 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
695 struct sysfs_open_file *of = sysfs_of(filp);
697 sysfs_put_open_dirent(sd, of);
698 single_release(inode, filp);
704 void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
706 struct sysfs_open_dirent *od;
707 struct sysfs_open_file *of;
709 if (!sysfs_is_bin(sd))
712 spin_lock_irq(&sysfs_open_dirent_lock);
713 od = sd->s_attr.open;
715 atomic_inc(&od->refcnt);
716 spin_unlock_irq(&sysfs_open_dirent_lock);
720 mutex_lock(&sysfs_open_file_mutex);
721 list_for_each_entry(of, &od->files, list) {
722 struct inode *inode = file_inode(of->file);
723 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
725 mutex_unlock(&sysfs_open_file_mutex);
727 sysfs_put_open_dirent(sd, NULL);
730 /* Sysfs attribute files are pollable. The idea is that you read
731 * the content and then you use 'poll' or 'select' to wait for
732 * the content to change. When the content changes (assuming the
733 * manager for the kobject supports notification), poll will
734 * return POLLERR|POLLPRI, and select will return the fd whether
735 * it is waiting for read, write, or exceptions.
736 * Once poll/select indicates that the value has changed, you
737 * need to close and re-open the file, or seek to 0 and read again.
738 * Reminder: this only works for attributes which actively support
739 * it, and it is not possible to test an attribute from userspace
740 * to see if it supports poll (Neither 'poll' nor 'select' return
741 * an appropriate error code). When in doubt, set a suitable timeout value.
743 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
745 struct sysfs_open_file *of = sysfs_of(filp);
746 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
747 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
749 /* need parent for the kobj, grab both */
750 if (!sysfs_get_active(attr_sd))
753 poll_wait(filp, &od->poll, wait);
755 sysfs_put_active(attr_sd);
757 if (of->event != atomic_read(&od->event))
760 return DEFAULT_POLLMASK;
763 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
766 void sysfs_notify_dirent(struct sysfs_dirent *sd)
768 struct sysfs_open_dirent *od;
771 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
773 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
774 od = sd->s_attr.open;
776 atomic_inc(&od->event);
777 wake_up_interruptible(&od->poll);
781 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
783 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
785 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
787 struct sysfs_dirent *sd = k->sd;
789 mutex_lock(&sysfs_mutex);
792 sd = sysfs_find_dirent(sd, dir, NULL);
794 sd = sysfs_find_dirent(sd, attr, NULL);
796 sysfs_notify_dirent(sd);
798 mutex_unlock(&sysfs_mutex);
800 EXPORT_SYMBOL_GPL(sysfs_notify);
802 const struct file_operations sysfs_file_operations = {
804 .write = sysfs_write_file,
806 .open = sysfs_open_file,
807 .release = sysfs_release,
811 const struct file_operations sysfs_bin_operations = {
812 .read = sysfs_bin_read,
813 .write = sysfs_write_file,
814 .llseek = generic_file_llseek,
815 .mmap = sysfs_bin_mmap,
816 .open = sysfs_open_file,
817 .release = sysfs_release,
821 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
822 const struct attribute *attr, int type,
823 umode_t amode, const void *ns)
825 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
826 struct sysfs_addrm_cxt acxt;
827 struct sysfs_dirent *sd;
830 sd = sysfs_new_dirent(attr->name, mode, type);
835 sd->s_attr.attr = (void *)attr;
836 sysfs_dirent_init_lockdep(sd);
838 sysfs_addrm_start(&acxt);
839 rc = sysfs_add_one(&acxt, sd, dir_sd);
840 sysfs_addrm_finish(&acxt);
849 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
852 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
856 * sysfs_create_file_ns - create an attribute file for an object with custom ns
857 * @kobj: object we're creating for
858 * @attr: attribute descriptor
859 * @ns: namespace the new file should belong to
861 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
864 BUG_ON(!kobj || !kobj->sd || !attr);
866 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
870 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
872 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
877 for (i = 0; ptr[i] && !err; i++)
878 err = sysfs_create_file(kobj, ptr[i]);
881 sysfs_remove_file(kobj, ptr[i]);
884 EXPORT_SYMBOL_GPL(sysfs_create_files);
887 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
888 * @kobj: object we're acting for.
889 * @attr: attribute descriptor.
890 * @group: group name.
892 int sysfs_add_file_to_group(struct kobject *kobj,
893 const struct attribute *attr, const char *group)
895 struct sysfs_dirent *dir_sd;
899 dir_sd = sysfs_get_dirent(kobj->sd, group);
901 dir_sd = sysfs_get(kobj->sd);
906 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
911 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
914 * sysfs_chmod_file - update the modified mode value on an object attribute.
915 * @kobj: object we're acting for.
916 * @attr: attribute descriptor.
917 * @mode: file permissions.
920 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
923 struct sysfs_dirent *sd;
924 struct iattr newattrs;
927 mutex_lock(&sysfs_mutex);
930 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
934 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
935 newattrs.ia_valid = ATTR_MODE;
936 rc = sysfs_sd_setattr(sd, &newattrs);
939 mutex_unlock(&sysfs_mutex);
942 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
945 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
946 * @kobj: object we're acting for
947 * @attr: attribute descriptor
948 * @ns: namespace tag of the file to remove
950 * Hash the attribute name and namespace tag and kill the victim.
952 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
955 struct sysfs_dirent *dir_sd = kobj->sd;
957 sysfs_hash_and_remove(dir_sd, attr->name, ns);
959 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
961 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
964 for (i = 0; ptr[i]; i++)
965 sysfs_remove_file(kobj, ptr[i]);
967 EXPORT_SYMBOL_GPL(sysfs_remove_files);
970 * sysfs_remove_file_from_group - remove an attribute file from a group.
971 * @kobj: object we're acting for.
972 * @attr: attribute descriptor.
973 * @group: group name.
975 void sysfs_remove_file_from_group(struct kobject *kobj,
976 const struct attribute *attr, const char *group)
978 struct sysfs_dirent *dir_sd;
981 dir_sd = sysfs_get_dirent(kobj->sd, group);
983 dir_sd = sysfs_get(kobj->sd);
985 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
989 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
992 * sysfs_create_bin_file - create binary file for object.
994 * @attr: attribute descriptor.
996 int sysfs_create_bin_file(struct kobject *kobj,
997 const struct bin_attribute *attr)
999 BUG_ON(!kobj || !kobj->sd || !attr);
1001 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1003 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1006 * sysfs_remove_bin_file - remove binary file for object.
1008 * @attr: attribute descriptor.
1010 void sysfs_remove_bin_file(struct kobject *kobj,
1011 const struct bin_attribute *attr)
1013 sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
1015 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1017 struct sysfs_schedule_callback_struct {
1018 struct list_head workq_list;
1019 struct kobject *kobj;
1020 void (*func)(void *);
1022 struct module *owner;
1023 struct work_struct work;
1026 static struct workqueue_struct *sysfs_workqueue;
1027 static DEFINE_MUTEX(sysfs_workq_mutex);
1028 static LIST_HEAD(sysfs_workq);
1029 static void sysfs_schedule_callback_work(struct work_struct *work)
1031 struct sysfs_schedule_callback_struct *ss = container_of(work,
1032 struct sysfs_schedule_callback_struct, work);
1034 (ss->func)(ss->data);
1035 kobject_put(ss->kobj);
1036 module_put(ss->owner);
1037 mutex_lock(&sysfs_workq_mutex);
1038 list_del(&ss->workq_list);
1039 mutex_unlock(&sysfs_workq_mutex);
1044 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1045 * @kobj: object we're acting for.
1046 * @func: callback function to invoke later.
1047 * @data: argument to pass to @func.
1048 * @owner: module owning the callback code
1050 * sysfs attribute methods must not unregister themselves or their parent
1051 * kobject (which would amount to the same thing). Attempts to do so will
1052 * deadlock, since unregistration is mutually exclusive with driver
1055 * Instead methods can call this routine, which will attempt to allocate
1056 * and schedule a workqueue request to call back @func with @data as its
1057 * argument in the workqueue's process context. @kobj will be pinned
1058 * until @func returns.
1060 * Returns 0 if the request was submitted, -ENOMEM if storage could not
1061 * be allocated, -ENODEV if a reference to @owner isn't available,
1062 * -EAGAIN if a callback has already been scheduled for @kobj.
1064 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
1065 void *data, struct module *owner)
1067 struct sysfs_schedule_callback_struct *ss, *tmp;
1069 if (!try_module_get(owner))
1072 mutex_lock(&sysfs_workq_mutex);
1073 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1074 if (ss->kobj == kobj) {
1076 mutex_unlock(&sysfs_workq_mutex);
1079 mutex_unlock(&sysfs_workq_mutex);
1081 if (sysfs_workqueue == NULL) {
1082 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
1083 if (sysfs_workqueue == NULL) {
1089 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
1099 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
1100 INIT_LIST_HEAD(&ss->workq_list);
1101 mutex_lock(&sysfs_workq_mutex);
1102 list_add_tail(&ss->workq_list, &sysfs_workq);
1103 mutex_unlock(&sysfs_workq_mutex);
1104 queue_work(sysfs_workqueue, &ss->work);
1107 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);