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 static bool sysfs_is_bin(struct sysfs_dirent *sd)
52 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
55 static struct sysfs_open_file *sysfs_of(struct file *file)
57 return ((struct seq_file *)file->private_data)->private;
61 * Determine the kernfs_ops for the given sysfs_dirent. This function must
62 * be called while holding an active reference.
64 static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
66 if (!sysfs_ignore_lockdep(sd))
67 lockdep_assert_held(sd);
68 return sd->s_attr.ops;
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->priv;
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_kf_seq_show(struct seq_file *sf, void *v)
91 struct sysfs_open_file *of = sf->private;
92 struct kobject *kobj = of->sd->s_parent->priv;
93 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
97 /* acquire buffer and ensure that it's >= PAGE_SIZE */
98 count = seq_get_buf(sf, &buf);
99 if (count < PAGE_SIZE) {
105 * Invoke show(). Control may reach here via seq file lseek even
106 * if @ops->show() isn't implemented.
109 count = ops->show(kobj, of->sd->priv, buf);
115 * The code works fine with PAGE_SIZE return but it's likely to
116 * indicate truncated result or overflow in normal use cases.
118 if (count >= (ssize_t)PAGE_SIZE) {
119 print_symbol("fill_read_buffer: %s returned bad count\n",
120 (unsigned long)ops->show);
121 /* Try to struggle along */
122 count = PAGE_SIZE - 1;
124 seq_commit(sf, count);
128 static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
129 size_t count, loff_t pos)
131 struct bin_attribute *battr = of->sd->priv;
132 struct kobject *kobj = of->sd->s_parent->priv;
133 loff_t size = file_inode(of->file)->i_size;
141 if (pos + count > size)
148 return battr->read(of->file, kobj, battr, buf, pos, count);
151 static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
153 struct sysfs_open_file *of = sf->private;
156 * @of->mutex nests outside active ref and is just to ensure that
157 * the ops aren't called concurrently for the same open file.
159 mutex_lock(&of->mutex);
160 if (!sysfs_get_active(of->sd))
161 return ERR_PTR(-ENODEV);
164 * The same behavior and code as single_open(). Returns !NULL if
165 * pos is at the beginning; otherwise, NULL.
167 return NULL + !*ppos;
170 static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
173 * The same behavior and code as single_open(), always terminate
174 * after the initial read.
180 static void kernfs_seq_stop(struct seq_file *sf, void *v)
182 struct sysfs_open_file *of = sf->private;
184 sysfs_put_active(of->sd);
185 mutex_unlock(&of->mutex);
188 static int kernfs_seq_show(struct seq_file *sf, void *v)
190 struct sysfs_open_file *of = sf->private;
192 of->event = atomic_read(&of->sd->s_attr.open->event);
194 return of->sd->s_attr.ops->seq_show(sf, v);
197 static const struct seq_operations kernfs_seq_ops = {
198 .start = kernfs_seq_start,
199 .next = kernfs_seq_next,
200 .stop = kernfs_seq_stop,
201 .show = kernfs_seq_show,
205 * As reading a bin file can have side-effects, the exact offset and bytes
206 * specified in read(2) call should be passed to the read callback making
207 * it difficult to use seq_file. Implement simplistic custom buffering for
210 static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
211 char __user *user_buf, size_t count,
214 ssize_t len = min_t(size_t, count, PAGE_SIZE);
215 const struct kernfs_ops *ops;
218 buf = kmalloc(len, GFP_KERNEL);
223 * @of->mutex nests outside active ref and is just to ensure that
224 * the ops aren't called concurrently for the same open file.
226 mutex_lock(&of->mutex);
227 if (!sysfs_get_active(of->sd)) {
229 mutex_unlock(&of->mutex);
233 ops = kernfs_ops(of->sd);
235 len = ops->read(of, buf, len, *ppos);
239 sysfs_put_active(of->sd);
240 mutex_unlock(&of->mutex);
245 if (copy_to_user(user_buf, buf, len)) {
258 * kernfs_file_read - kernfs vfs read callback
259 * @file: file pointer
260 * @user_buf: data to write
261 * @count: number of bytes
262 * @ppos: starting offset
264 static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
265 size_t count, loff_t *ppos)
267 struct sysfs_open_file *of = sysfs_of(file);
269 if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
270 return seq_read(file, user_buf, count, ppos);
272 return kernfs_file_direct_read(of, user_buf, count, ppos);
275 /* kernfs write callback for regular sysfs files */
276 static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
277 size_t count, loff_t pos)
279 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
280 struct kobject *kobj = of->sd->s_parent->priv;
285 return ops->store(kobj, of->sd->priv, buf, count);
288 /* kernfs write callback for bin sysfs files */
289 static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
290 size_t count, loff_t pos)
292 struct bin_attribute *battr = of->sd->priv;
293 struct kobject *kobj = of->sd->s_parent->priv;
294 loff_t size = file_inode(of->file)->i_size;
299 count = min_t(ssize_t, count, size - pos);
307 return battr->write(of->file, kobj, battr, buf, pos, count);
311 * kernfs_file_write - kernfs vfs write callback
312 * @file: file pointer
313 * @user_buf: data to write
314 * @count: number of bytes
315 * @ppos: starting offset
317 * Copy data in from userland and pass it to the matching kernfs write
320 * There is no easy way for us to know if userspace is only doing a partial
321 * write, so we don't support them. We expect the entire buffer to come on
322 * the first write. Hint: if you're writing a value, first read the file,
323 * modify only the the value you're changing, then write entire buffer
326 static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
327 size_t count, loff_t *ppos)
329 struct sysfs_open_file *of = sysfs_of(file);
330 ssize_t len = min_t(size_t, count, PAGE_SIZE);
331 const struct kernfs_ops *ops;
334 buf = kmalloc(len + 1, GFP_KERNEL);
338 if (copy_from_user(buf, user_buf, len)) {
342 buf[len] = '\0'; /* guarantee string termination */
345 * @of->mutex nests outside active ref and is just to ensure that
346 * the ops aren't called concurrently for the same open file.
348 mutex_lock(&of->mutex);
349 if (!sysfs_get_active(of->sd)) {
350 mutex_unlock(&of->mutex);
355 ops = kernfs_ops(of->sd);
357 len = ops->write(of, buf, len, *ppos);
361 sysfs_put_active(of->sd);
362 mutex_unlock(&of->mutex);
371 static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
372 struct vm_area_struct *vma)
374 struct bin_attribute *battr = of->sd->priv;
375 struct kobject *kobj = of->sd->s_parent->priv;
380 return battr->mmap(of->file, kobj, battr, vma);
383 static void kernfs_vma_open(struct vm_area_struct *vma)
385 struct file *file = vma->vm_file;
386 struct sysfs_open_file *of = sysfs_of(file);
391 if (!sysfs_get_active(of->sd))
394 if (of->vm_ops->open)
395 of->vm_ops->open(vma);
397 sysfs_put_active(of->sd);
400 static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
402 struct file *file = vma->vm_file;
403 struct sysfs_open_file *of = sysfs_of(file);
407 return VM_FAULT_SIGBUS;
409 if (!sysfs_get_active(of->sd))
410 return VM_FAULT_SIGBUS;
412 ret = VM_FAULT_SIGBUS;
413 if (of->vm_ops->fault)
414 ret = of->vm_ops->fault(vma, vmf);
416 sysfs_put_active(of->sd);
420 static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
421 struct vm_fault *vmf)
423 struct file *file = vma->vm_file;
424 struct sysfs_open_file *of = sysfs_of(file);
428 return VM_FAULT_SIGBUS;
430 if (!sysfs_get_active(of->sd))
431 return VM_FAULT_SIGBUS;
434 if (of->vm_ops->page_mkwrite)
435 ret = of->vm_ops->page_mkwrite(vma, vmf);
437 file_update_time(file);
439 sysfs_put_active(of->sd);
443 static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
444 void *buf, int len, int write)
446 struct file *file = vma->vm_file;
447 struct sysfs_open_file *of = sysfs_of(file);
453 if (!sysfs_get_active(of->sd))
457 if (of->vm_ops->access)
458 ret = of->vm_ops->access(vma, addr, buf, len, write);
460 sysfs_put_active(of->sd);
465 static int kernfs_vma_set_policy(struct vm_area_struct *vma,
466 struct mempolicy *new)
468 struct file *file = vma->vm_file;
469 struct sysfs_open_file *of = sysfs_of(file);
475 if (!sysfs_get_active(of->sd))
479 if (of->vm_ops->set_policy)
480 ret = of->vm_ops->set_policy(vma, new);
482 sysfs_put_active(of->sd);
486 static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
489 struct file *file = vma->vm_file;
490 struct sysfs_open_file *of = sysfs_of(file);
491 struct mempolicy *pol;
494 return vma->vm_policy;
496 if (!sysfs_get_active(of->sd))
497 return vma->vm_policy;
499 pol = vma->vm_policy;
500 if (of->vm_ops->get_policy)
501 pol = of->vm_ops->get_policy(vma, addr);
503 sysfs_put_active(of->sd);
507 static int kernfs_vma_migrate(struct vm_area_struct *vma,
508 const nodemask_t *from, const nodemask_t *to,
511 struct file *file = vma->vm_file;
512 struct sysfs_open_file *of = sysfs_of(file);
518 if (!sysfs_get_active(of->sd))
522 if (of->vm_ops->migrate)
523 ret = of->vm_ops->migrate(vma, from, to, flags);
525 sysfs_put_active(of->sd);
530 static const struct vm_operations_struct kernfs_vm_ops = {
531 .open = kernfs_vma_open,
532 .fault = kernfs_vma_fault,
533 .page_mkwrite = kernfs_vma_page_mkwrite,
534 .access = kernfs_vma_access,
536 .set_policy = kernfs_vma_set_policy,
537 .get_policy = kernfs_vma_get_policy,
538 .migrate = kernfs_vma_migrate,
542 static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
544 struct sysfs_open_file *of = sysfs_of(file);
545 const struct kernfs_ops *ops;
548 mutex_lock(&of->mutex);
551 if (!sysfs_get_active(of->sd))
554 ops = kernfs_ops(of->sd);
556 rc = ops->mmap(of, vma);
561 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
562 * to satisfy versions of X which crash if the mmap fails: that
563 * substitutes a new vm_file, and we don't then want bin_vm_ops.
565 if (vma->vm_file != file)
569 if (of->mmapped && of->vm_ops != vma->vm_ops)
573 * It is not possible to successfully wrap close.
574 * So error if someone is trying to use close.
577 if (vma->vm_ops && vma->vm_ops->close)
582 of->vm_ops = vma->vm_ops;
583 vma->vm_ops = &kernfs_vm_ops;
585 sysfs_put_active(of->sd);
587 mutex_unlock(&of->mutex);
593 * sysfs_get_open_dirent - get or create sysfs_open_dirent
594 * @sd: target sysfs_dirent
595 * @of: sysfs_open_file for this instance of open
597 * If @sd->s_attr.open exists, increment its reference count;
598 * otherwise, create one. @of is chained to the files list.
601 * Kernel thread context (may sleep).
604 * 0 on success, -errno on failure.
606 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
607 struct sysfs_open_file *of)
609 struct sysfs_open_dirent *od, *new_od = NULL;
612 mutex_lock(&sysfs_open_file_mutex);
613 spin_lock_irq(&sysfs_open_dirent_lock);
615 if (!sd->s_attr.open && new_od) {
616 sd->s_attr.open = new_od;
620 od = sd->s_attr.open;
622 atomic_inc(&od->refcnt);
623 list_add_tail(&of->list, &od->files);
626 spin_unlock_irq(&sysfs_open_dirent_lock);
627 mutex_unlock(&sysfs_open_file_mutex);
634 /* not there, initialize a new one and retry */
635 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
639 atomic_set(&new_od->refcnt, 0);
640 atomic_set(&new_od->event, 1);
641 init_waitqueue_head(&new_od->poll);
642 INIT_LIST_HEAD(&new_od->files);
647 * sysfs_put_open_dirent - put sysfs_open_dirent
648 * @sd: target sysfs_dirent
649 * @of: associated sysfs_open_file
651 * Put @sd->s_attr.open and unlink @of from the files list. If
652 * reference count reaches zero, disassociate and free it.
657 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
658 struct sysfs_open_file *of)
660 struct sysfs_open_dirent *od = sd->s_attr.open;
663 mutex_lock(&sysfs_open_file_mutex);
664 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
669 if (atomic_dec_and_test(&od->refcnt))
670 sd->s_attr.open = NULL;
674 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
675 mutex_unlock(&sysfs_open_file_mutex);
680 static int kernfs_file_open(struct inode *inode, struct file *file)
682 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
683 const struct kernfs_ops *ops;
684 struct sysfs_open_file *of;
685 bool has_read, has_write, has_mmap;
688 if (!sysfs_get_active(attr_sd))
691 ops = kernfs_ops(attr_sd);
693 has_read = ops->seq_show || ops->read || ops->mmap;
694 has_write = ops->write || ops->mmap;
695 has_mmap = ops->mmap;
697 /* check perms and supported operations */
698 if ((file->f_mode & FMODE_WRITE) &&
699 (!(inode->i_mode & S_IWUGO) || !has_write))
702 if ((file->f_mode & FMODE_READ) &&
703 (!(inode->i_mode & S_IRUGO) || !has_read))
706 /* allocate a sysfs_open_file for the file */
708 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
713 * The following is done to give a different lockdep key to
714 * @of->mutex for files which implement mmap. This is a rather
715 * crude way to avoid false positive lockdep warning around
716 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
717 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
718 * which mm->mmap_sem nests, while holding @of->mutex. As each
719 * open file has a separate mutex, it's okay as long as those don't
720 * happen on the same file. At this point, we can't easily give
721 * each file a separate locking class. Let's differentiate on
722 * whether the file has mmap or not for now.
725 mutex_init(&of->mutex);
727 mutex_init(&of->mutex);
733 * Always instantiate seq_file even if read access doesn't use
734 * seq_file or is not requested. This unifies private data access
735 * and readable regular files are the vast majority anyway.
738 error = seq_open(file, &kernfs_seq_ops);
740 error = seq_open(file, NULL);
744 ((struct seq_file *)file->private_data)->private = of;
746 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
747 if (file->f_mode & FMODE_WRITE)
748 file->f_mode |= FMODE_PWRITE;
750 /* make sure we have open dirent struct */
751 error = sysfs_get_open_dirent(attr_sd, of);
755 /* open succeeded, put active references */
756 sysfs_put_active(attr_sd);
760 seq_release(inode, file);
764 sysfs_put_active(attr_sd);
768 static int kernfs_file_release(struct inode *inode, struct file *filp)
770 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
771 struct sysfs_open_file *of = sysfs_of(filp);
773 sysfs_put_open_dirent(sd, of);
774 seq_release(inode, filp);
780 void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
782 struct sysfs_open_dirent *od;
783 struct sysfs_open_file *of;
785 if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
788 spin_lock_irq(&sysfs_open_dirent_lock);
789 od = sd->s_attr.open;
791 atomic_inc(&od->refcnt);
792 spin_unlock_irq(&sysfs_open_dirent_lock);
796 mutex_lock(&sysfs_open_file_mutex);
797 list_for_each_entry(of, &od->files, list) {
798 struct inode *inode = file_inode(of->file);
799 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
801 mutex_unlock(&sysfs_open_file_mutex);
803 sysfs_put_open_dirent(sd, NULL);
806 /* Sysfs attribute files are pollable. The idea is that you read
807 * the content and then you use 'poll' or 'select' to wait for
808 * the content to change. When the content changes (assuming the
809 * manager for the kobject supports notification), poll will
810 * return POLLERR|POLLPRI, and select will return the fd whether
811 * it is waiting for read, write, or exceptions.
812 * Once poll/select indicates that the value has changed, you
813 * need to close and re-open the file, or seek to 0 and read again.
814 * Reminder: this only works for attributes which actively support
815 * it, and it is not possible to test an attribute from userspace
816 * to see if it supports poll (Neither 'poll' nor 'select' return
817 * an appropriate error code). When in doubt, set a suitable timeout value.
819 static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
821 struct sysfs_open_file *of = sysfs_of(filp);
822 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
823 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
825 /* need parent for the kobj, grab both */
826 if (!sysfs_get_active(attr_sd))
829 poll_wait(filp, &od->poll, wait);
831 sysfs_put_active(attr_sd);
833 if (of->event != atomic_read(&od->event))
836 return DEFAULT_POLLMASK;
839 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
842 void sysfs_notify_dirent(struct sysfs_dirent *sd)
844 struct sysfs_open_dirent *od;
847 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
849 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
850 od = sd->s_attr.open;
852 atomic_inc(&od->event);
853 wake_up_interruptible(&od->poll);
857 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
859 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
861 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
863 struct sysfs_dirent *sd = k->sd;
865 mutex_lock(&sysfs_mutex);
868 sd = sysfs_find_dirent(sd, dir, NULL);
870 sd = sysfs_find_dirent(sd, attr, NULL);
872 sysfs_notify_dirent(sd);
874 mutex_unlock(&sysfs_mutex);
876 EXPORT_SYMBOL_GPL(sysfs_notify);
878 const struct file_operations kernfs_file_operations = {
879 .read = kernfs_file_read,
880 .write = kernfs_file_write,
881 .llseek = generic_file_llseek,
882 .mmap = kernfs_file_mmap,
883 .open = kernfs_file_open,
884 .release = kernfs_file_release,
885 .poll = kernfs_file_poll,
888 static const struct kernfs_ops sysfs_file_kfops_empty = {
891 static const struct kernfs_ops sysfs_file_kfops_ro = {
892 .seq_show = sysfs_kf_seq_show,
895 static const struct kernfs_ops sysfs_file_kfops_wo = {
896 .write = sysfs_kf_write,
899 static const struct kernfs_ops sysfs_file_kfops_rw = {
900 .seq_show = sysfs_kf_seq_show,
901 .write = sysfs_kf_write,
904 static const struct kernfs_ops sysfs_bin_kfops_ro = {
905 .read = sysfs_kf_bin_read,
908 static const struct kernfs_ops sysfs_bin_kfops_wo = {
909 .write = sysfs_kf_bin_write,
912 static const struct kernfs_ops sysfs_bin_kfops_rw = {
913 .read = sysfs_kf_bin_read,
914 .write = sysfs_kf_bin_write,
915 .mmap = sysfs_kf_bin_mmap,
918 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
919 const struct attribute *attr, int type,
920 umode_t amode, const void *ns)
922 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
923 const struct kernfs_ops *ops;
924 struct sysfs_addrm_cxt acxt;
925 struct sysfs_dirent *sd;
928 if (type == SYSFS_KOBJ_ATTR) {
929 struct kobject *kobj = dir_sd->priv;
930 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
932 /* every kobject with an attribute needs a ktype assigned */
933 if (WARN(!sysfs_ops, KERN_ERR
934 "missing sysfs attribute operations for kobject: %s\n",
938 if (sysfs_ops->show && sysfs_ops->store)
939 ops = &sysfs_file_kfops_rw;
940 else if (sysfs_ops->show)
941 ops = &sysfs_file_kfops_ro;
942 else if (sysfs_ops->store)
943 ops = &sysfs_file_kfops_wo;
945 ops = &sysfs_file_kfops_empty;
947 struct bin_attribute *battr = (void *)attr;
949 if ((battr->read && battr->write) || battr->mmap)
950 ops = &sysfs_bin_kfops_rw;
951 else if (battr->read)
952 ops = &sysfs_bin_kfops_ro;
953 else if (battr->write)
954 ops = &sysfs_bin_kfops_wo;
956 ops = &sysfs_file_kfops_empty;
959 sd = sysfs_new_dirent(attr->name, mode, type);
963 sd->s_attr.ops = ops;
965 sd->priv = (void *)attr;
966 sysfs_dirent_init_lockdep(sd);
969 * sd->s_attr.ops is accesible only while holding active ref. We
970 * need to know whether some ops are implemented outside active
971 * ref. Cache their existence in flags.
974 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
976 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
978 sysfs_addrm_start(&acxt);
979 rc = sysfs_add_one(&acxt, sd, dir_sd);
980 sysfs_addrm_finish(&acxt);
989 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
992 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
996 * sysfs_create_file_ns - create an attribute file for an object with custom ns
997 * @kobj: object we're creating for
998 * @attr: attribute descriptor
999 * @ns: namespace the new file should belong to
1001 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
1004 BUG_ON(!kobj || !kobj->sd || !attr);
1006 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
1010 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
1012 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
1017 for (i = 0; ptr[i] && !err; i++)
1018 err = sysfs_create_file(kobj, ptr[i]);
1021 sysfs_remove_file(kobj, ptr[i]);
1024 EXPORT_SYMBOL_GPL(sysfs_create_files);
1027 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
1028 * @kobj: object we're acting for.
1029 * @attr: attribute descriptor.
1030 * @group: group name.
1032 int sysfs_add_file_to_group(struct kobject *kobj,
1033 const struct attribute *attr, const char *group)
1035 struct sysfs_dirent *dir_sd;
1039 dir_sd = sysfs_get_dirent(kobj->sd, group);
1041 dir_sd = sysfs_get(kobj->sd);
1046 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
1051 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
1054 * sysfs_chmod_file - update the modified mode value on an object attribute.
1055 * @kobj: object we're acting for.
1056 * @attr: attribute descriptor.
1057 * @mode: file permissions.
1060 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
1063 struct sysfs_dirent *sd;
1064 struct iattr newattrs;
1067 sd = sysfs_get_dirent(kobj->sd, attr->name);
1071 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
1072 newattrs.ia_valid = ATTR_MODE;
1074 rc = kernfs_setattr(sd, &newattrs);
1079 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1082 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1083 * @kobj: object we're acting for
1084 * @attr: attribute descriptor
1085 * @ns: namespace tag of the file to remove
1087 * Hash the attribute name and namespace tag and kill the victim.
1089 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1092 struct sysfs_dirent *dir_sd = kobj->sd;
1094 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
1096 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
1098 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
1101 for (i = 0; ptr[i]; i++)
1102 sysfs_remove_file(kobj, ptr[i]);
1104 EXPORT_SYMBOL_GPL(sysfs_remove_files);
1107 * sysfs_remove_file_from_group - remove an attribute file from a group.
1108 * @kobj: object we're acting for.
1109 * @attr: attribute descriptor.
1110 * @group: group name.
1112 void sysfs_remove_file_from_group(struct kobject *kobj,
1113 const struct attribute *attr, const char *group)
1115 struct sysfs_dirent *dir_sd;
1118 dir_sd = sysfs_get_dirent(kobj->sd, group);
1120 dir_sd = sysfs_get(kobj->sd);
1122 kernfs_remove_by_name(dir_sd, attr->name);
1126 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1129 * sysfs_create_bin_file - create binary file for object.
1131 * @attr: attribute descriptor.
1133 int sysfs_create_bin_file(struct kobject *kobj,
1134 const struct bin_attribute *attr)
1136 BUG_ON(!kobj || !kobj->sd || !attr);
1138 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1140 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1143 * sysfs_remove_bin_file - remove binary file for object.
1145 * @attr: attribute descriptor.
1147 void sysfs_remove_bin_file(struct kobject *kobj,
1148 const struct bin_attribute *attr)
1150 kernfs_remove_by_name(kobj->sd, attr->attr.name);
1152 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1154 struct sysfs_schedule_callback_struct {
1155 struct list_head workq_list;
1156 struct kobject *kobj;
1157 void (*func)(void *);
1159 struct module *owner;
1160 struct work_struct work;
1163 static struct workqueue_struct *sysfs_workqueue;
1164 static DEFINE_MUTEX(sysfs_workq_mutex);
1165 static LIST_HEAD(sysfs_workq);
1166 static void sysfs_schedule_callback_work(struct work_struct *work)
1168 struct sysfs_schedule_callback_struct *ss = container_of(work,
1169 struct sysfs_schedule_callback_struct, work);
1171 (ss->func)(ss->data);
1172 kobject_put(ss->kobj);
1173 module_put(ss->owner);
1174 mutex_lock(&sysfs_workq_mutex);
1175 list_del(&ss->workq_list);
1176 mutex_unlock(&sysfs_workq_mutex);
1181 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1182 * @kobj: object we're acting for.
1183 * @func: callback function to invoke later.
1184 * @data: argument to pass to @func.
1185 * @owner: module owning the callback code
1187 * sysfs attribute methods must not unregister themselves or their parent
1188 * kobject (which would amount to the same thing). Attempts to do so will
1189 * deadlock, since unregistration is mutually exclusive with driver
1192 * Instead methods can call this routine, which will attempt to allocate
1193 * and schedule a workqueue request to call back @func with @data as its
1194 * argument in the workqueue's process context. @kobj will be pinned
1195 * until @func returns.
1197 * Returns 0 if the request was submitted, -ENOMEM if storage could not
1198 * be allocated, -ENODEV if a reference to @owner isn't available,
1199 * -EAGAIN if a callback has already been scheduled for @kobj.
1201 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
1202 void *data, struct module *owner)
1204 struct sysfs_schedule_callback_struct *ss, *tmp;
1206 if (!try_module_get(owner))
1209 mutex_lock(&sysfs_workq_mutex);
1210 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1211 if (ss->kobj == kobj) {
1213 mutex_unlock(&sysfs_workq_mutex);
1216 mutex_unlock(&sysfs_workq_mutex);
1218 if (sysfs_workqueue == NULL) {
1219 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
1220 if (sysfs_workqueue == NULL) {
1226 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
1236 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
1237 INIT_LIST_HEAD(&ss->workq_list);
1238 mutex_lock(&sysfs_workq_mutex);
1239 list_add_tail(&ss->workq_list, &sysfs_workq);
1240 mutex_unlock(&sysfs_workq_mutex);
1241 queue_work(sysfs_workqueue, &ss->work);
1244 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);