1 // SPDX-License-Identifier: GPL-2.0-only
3 * fs/kernfs/file.c - kernfs file implementation
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
11 #include <linux/seq_file.h>
12 #include <linux/slab.h>
13 #include <linux/poll.h>
14 #include <linux/pagemap.h>
15 #include <linux/sched/mm.h>
16 #include <linux/fsnotify.h>
17 #include <linux/uio.h>
19 #include "kernfs-internal.h"
21 struct kernfs_open_node {
22 struct rcu_head rcu_head;
24 wait_queue_head_t poll;
25 struct list_head files; /* goes through kernfs_open_file.list */
26 unsigned int nr_mmapped;
27 unsigned int nr_to_release;
31 * kernfs_notify() may be called from any context and bounces notifications
32 * through a work item. To minimize space overhead in kernfs_node, the
33 * pending queue is implemented as a singly linked list of kernfs_nodes.
34 * The list is terminated with the self pointer so that whether a
35 * kernfs_node is on the list or not can be determined by testing the next
38 #define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list)
40 static DEFINE_SPINLOCK(kernfs_notify_lock);
41 static struct kernfs_node *kernfs_notify_list = KERNFS_NOTIFY_EOL;
43 static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node *kn)
45 int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS);
47 return &kernfs_locks->open_file_mutex[idx];
50 static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node *kn)
54 lock = kernfs_open_file_mutex_ptr(kn);
62 * of_on - Return the kernfs_open_node of the specified kernfs_open_file
63 * @of: taret kernfs_open_file
65 static struct kernfs_open_node *of_on(struct kernfs_open_file *of)
67 return rcu_dereference_protected(of->kn->attr.open,
68 !list_empty(&of->list));
72 * kernfs_deref_open_node_locked - Get kernfs_open_node corresponding to @kn
74 * @kn: target kernfs_node.
76 * Fetch and return ->attr.open of @kn when caller holds the
77 * kernfs_open_file_mutex_ptr(kn).
79 * Update of ->attr.open happens under kernfs_open_file_mutex_ptr(kn). So when
80 * the caller guarantees that this mutex is being held, other updaters can't
81 * change ->attr.open and this means that we can safely deref ->attr.open
82 * outside RCU read-side critical section.
84 * The caller needs to make sure that kernfs_open_file_mutex is held.
86 static struct kernfs_open_node *
87 kernfs_deref_open_node_locked(struct kernfs_node *kn)
89 return rcu_dereference_protected(kn->attr.open,
90 lockdep_is_held(kernfs_open_file_mutex_ptr(kn)));
93 static struct kernfs_open_file *kernfs_of(struct file *file)
95 return ((struct seq_file *)file->private_data)->private;
99 * Determine the kernfs_ops for the given kernfs_node. This function must
100 * be called while holding an active reference.
102 static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn)
104 if (kn->flags & KERNFS_LOCKDEP)
105 lockdep_assert_held(kn);
110 * As kernfs_seq_stop() is also called after kernfs_seq_start() or
111 * kernfs_seq_next() failure, it needs to distinguish whether it's stopping
112 * a seq_file iteration which is fully initialized with an active reference
113 * or an aborted kernfs_seq_start() due to get_active failure. The
114 * position pointer is the only context for each seq_file iteration and
115 * thus the stop condition should be encoded in it. As the return value is
116 * directly visible to userland, ERR_PTR(-ENODEV) is the only acceptable
117 * choice to indicate get_active failure.
119 * Unfortunately, this is complicated due to the optional custom seq_file
120 * operations which may return ERR_PTR(-ENODEV) too. kernfs_seq_stop()
121 * can't distinguish whether ERR_PTR(-ENODEV) is from get_active failure or
122 * custom seq_file operations and thus can't decide whether put_active
123 * should be performed or not only on ERR_PTR(-ENODEV).
125 * This is worked around by factoring out the custom seq_stop() and
126 * put_active part into kernfs_seq_stop_active(), skipping it from
127 * kernfs_seq_stop() if ERR_PTR(-ENODEV) while invoking it directly after
128 * custom seq_file operations fail with ERR_PTR(-ENODEV) - this ensures
129 * that kernfs_seq_stop_active() is skipped only after get_active failure.
131 static void kernfs_seq_stop_active(struct seq_file *sf, void *v)
133 struct kernfs_open_file *of = sf->private;
134 const struct kernfs_ops *ops = kernfs_ops(of->kn);
137 ops->seq_stop(sf, v);
138 kernfs_put_active(of->kn);
141 static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
143 struct kernfs_open_file *of = sf->private;
144 const struct kernfs_ops *ops;
147 * @of->mutex nests outside active ref and is primarily to ensure that
148 * the ops aren't called concurrently for the same open file.
150 mutex_lock(&of->mutex);
151 if (!kernfs_get_active(of->kn))
152 return ERR_PTR(-ENODEV);
154 ops = kernfs_ops(of->kn);
155 if (ops->seq_start) {
156 void *next = ops->seq_start(sf, ppos);
157 /* see the comment above kernfs_seq_stop_active() */
158 if (next == ERR_PTR(-ENODEV))
159 kernfs_seq_stop_active(sf, next);
162 return single_start(sf, ppos);
165 static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
167 struct kernfs_open_file *of = sf->private;
168 const struct kernfs_ops *ops = kernfs_ops(of->kn);
171 void *next = ops->seq_next(sf, v, ppos);
172 /* see the comment above kernfs_seq_stop_active() */
173 if (next == ERR_PTR(-ENODEV))
174 kernfs_seq_stop_active(sf, next);
178 * The same behavior and code as single_open(), always
179 * terminate after the initial read.
186 static void kernfs_seq_stop(struct seq_file *sf, void *v)
188 struct kernfs_open_file *of = sf->private;
190 if (v != ERR_PTR(-ENODEV))
191 kernfs_seq_stop_active(sf, v);
192 mutex_unlock(&of->mutex);
195 static int kernfs_seq_show(struct seq_file *sf, void *v)
197 struct kernfs_open_file *of = sf->private;
199 of->event = atomic_read(&of_on(of)->event);
201 return of->kn->attr.ops->seq_show(sf, v);
204 static const struct seq_operations kernfs_seq_ops = {
205 .start = kernfs_seq_start,
206 .next = kernfs_seq_next,
207 .stop = kernfs_seq_stop,
208 .show = kernfs_seq_show,
212 * As reading a bin file can have side-effects, the exact offset and bytes
213 * specified in read(2) call should be passed to the read callback making
214 * it difficult to use seq_file. Implement simplistic custom buffering for
217 static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
219 struct kernfs_open_file *of = kernfs_of(iocb->ki_filp);
220 ssize_t len = min_t(size_t, iov_iter_count(iter), PAGE_SIZE);
221 const struct kernfs_ops *ops;
224 buf = of->prealloc_buf;
226 mutex_lock(&of->prealloc_mutex);
228 buf = kmalloc(len, GFP_KERNEL);
233 * @of->mutex nests outside active ref and is used both to ensure that
234 * the ops aren't called concurrently for the same open file.
236 mutex_lock(&of->mutex);
237 if (!kernfs_get_active(of->kn)) {
239 mutex_unlock(&of->mutex);
243 of->event = atomic_read(&of_on(of)->event);
245 ops = kernfs_ops(of->kn);
247 len = ops->read(of, buf, len, iocb->ki_pos);
251 kernfs_put_active(of->kn);
252 mutex_unlock(&of->mutex);
257 if (copy_to_iter(buf, len, iter) != len) {
265 if (buf == of->prealloc_buf)
266 mutex_unlock(&of->prealloc_mutex);
272 static ssize_t kernfs_fop_read_iter(struct kiocb *iocb, struct iov_iter *iter)
274 if (kernfs_of(iocb->ki_filp)->kn->flags & KERNFS_HAS_SEQ_SHOW)
275 return seq_read_iter(iocb, iter);
276 return kernfs_file_read_iter(iocb, iter);
280 * Copy data in from userland and pass it to the matching kernfs write
283 * There is no easy way for us to know if userspace is only doing a partial
284 * write, so we don't support them. We expect the entire buffer to come on
285 * the first write. Hint: if you're writing a value, first read the file,
286 * modify only the value you're changing, then write entire buffer
289 static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
291 struct kernfs_open_file *of = kernfs_of(iocb->ki_filp);
292 ssize_t len = iov_iter_count(iter);
293 const struct kernfs_ops *ops;
296 if (of->atomic_write_len) {
297 if (len > of->atomic_write_len)
300 len = min_t(size_t, len, PAGE_SIZE);
303 buf = of->prealloc_buf;
305 mutex_lock(&of->prealloc_mutex);
307 buf = kmalloc(len + 1, GFP_KERNEL);
311 if (copy_from_iter(buf, len, iter) != len) {
315 buf[len] = '\0'; /* guarantee string termination */
318 * @of->mutex nests outside active ref and is used both to ensure that
319 * the ops aren't called concurrently for the same open file.
321 mutex_lock(&of->mutex);
322 if (!kernfs_get_active(of->kn)) {
323 mutex_unlock(&of->mutex);
328 ops = kernfs_ops(of->kn);
330 len = ops->write(of, buf, len, iocb->ki_pos);
334 kernfs_put_active(of->kn);
335 mutex_unlock(&of->mutex);
341 if (buf == of->prealloc_buf)
342 mutex_unlock(&of->prealloc_mutex);
348 static void kernfs_vma_open(struct vm_area_struct *vma)
350 struct file *file = vma->vm_file;
351 struct kernfs_open_file *of = kernfs_of(file);
356 if (!kernfs_get_active(of->kn))
359 if (of->vm_ops->open)
360 of->vm_ops->open(vma);
362 kernfs_put_active(of->kn);
365 static vm_fault_t kernfs_vma_fault(struct vm_fault *vmf)
367 struct file *file = vmf->vma->vm_file;
368 struct kernfs_open_file *of = kernfs_of(file);
372 return VM_FAULT_SIGBUS;
374 if (!kernfs_get_active(of->kn))
375 return VM_FAULT_SIGBUS;
377 ret = VM_FAULT_SIGBUS;
378 if (of->vm_ops->fault)
379 ret = of->vm_ops->fault(vmf);
381 kernfs_put_active(of->kn);
385 static vm_fault_t kernfs_vma_page_mkwrite(struct vm_fault *vmf)
387 struct file *file = vmf->vma->vm_file;
388 struct kernfs_open_file *of = kernfs_of(file);
392 return VM_FAULT_SIGBUS;
394 if (!kernfs_get_active(of->kn))
395 return VM_FAULT_SIGBUS;
398 if (of->vm_ops->page_mkwrite)
399 ret = of->vm_ops->page_mkwrite(vmf);
401 file_update_time(file);
403 kernfs_put_active(of->kn);
407 static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
408 void *buf, int len, int write)
410 struct file *file = vma->vm_file;
411 struct kernfs_open_file *of = kernfs_of(file);
417 if (!kernfs_get_active(of->kn))
421 if (of->vm_ops->access)
422 ret = of->vm_ops->access(vma, addr, buf, len, write);
424 kernfs_put_active(of->kn);
429 static int kernfs_vma_set_policy(struct vm_area_struct *vma,
430 struct mempolicy *new)
432 struct file *file = vma->vm_file;
433 struct kernfs_open_file *of = kernfs_of(file);
439 if (!kernfs_get_active(of->kn))
443 if (of->vm_ops->set_policy)
444 ret = of->vm_ops->set_policy(vma, new);
446 kernfs_put_active(of->kn);
450 static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
453 struct file *file = vma->vm_file;
454 struct kernfs_open_file *of = kernfs_of(file);
455 struct mempolicy *pol;
458 return vma->vm_policy;
460 if (!kernfs_get_active(of->kn))
461 return vma->vm_policy;
463 pol = vma->vm_policy;
464 if (of->vm_ops->get_policy)
465 pol = of->vm_ops->get_policy(vma, addr);
467 kernfs_put_active(of->kn);
473 static const struct vm_operations_struct kernfs_vm_ops = {
474 .open = kernfs_vma_open,
475 .fault = kernfs_vma_fault,
476 .page_mkwrite = kernfs_vma_page_mkwrite,
477 .access = kernfs_vma_access,
479 .set_policy = kernfs_vma_set_policy,
480 .get_policy = kernfs_vma_get_policy,
484 static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
486 struct kernfs_open_file *of = kernfs_of(file);
487 const struct kernfs_ops *ops;
491 * mmap path and of->mutex are prone to triggering spurious lockdep
492 * warnings and we don't want to add spurious locking dependency
493 * between the two. Check whether mmap is actually implemented
494 * without grabbing @of->mutex by testing HAS_MMAP flag. See the
495 * comment in kernfs_file_open() for more details.
497 if (!(of->kn->flags & KERNFS_HAS_MMAP))
500 mutex_lock(&of->mutex);
503 if (!kernfs_get_active(of->kn))
506 ops = kernfs_ops(of->kn);
507 rc = ops->mmap(of, vma);
512 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
513 * to satisfy versions of X which crash if the mmap fails: that
514 * substitutes a new vm_file, and we don't then want bin_vm_ops.
516 if (vma->vm_file != file)
520 if (of->mmapped && of->vm_ops != vma->vm_ops)
524 * It is not possible to successfully wrap close.
525 * So error if someone is trying to use close.
527 if (vma->vm_ops && vma->vm_ops->close)
532 of_on(of)->nr_mmapped++;
533 of->vm_ops = vma->vm_ops;
534 vma->vm_ops = &kernfs_vm_ops;
536 kernfs_put_active(of->kn);
538 mutex_unlock(&of->mutex);
544 * kernfs_get_open_node - get or create kernfs_open_node
545 * @kn: target kernfs_node
546 * @of: kernfs_open_file for this instance of open
548 * If @kn->attr.open exists, increment its reference count; otherwise,
549 * create one. @of is chained to the files list.
552 * Kernel thread context (may sleep).
555 * 0 on success, -errno on failure.
557 static int kernfs_get_open_node(struct kernfs_node *kn,
558 struct kernfs_open_file *of)
560 struct kernfs_open_node *on;
563 mutex = kernfs_open_file_mutex_lock(kn);
564 on = kernfs_deref_open_node_locked(kn);
567 /* not there, initialize a new one */
568 on = kzalloc(sizeof(*on), GFP_KERNEL);
573 atomic_set(&on->event, 1);
574 init_waitqueue_head(&on->poll);
575 INIT_LIST_HEAD(&on->files);
576 rcu_assign_pointer(kn->attr.open, on);
579 list_add_tail(&of->list, &on->files);
580 if (kn->flags & KERNFS_HAS_RELEASE)
588 * kernfs_unlink_open_file - Unlink @of from @kn.
590 * @kn: target kernfs_node
591 * @of: associated kernfs_open_file
592 * @open_failed: ->open() failed, cancel ->release()
594 * Unlink @of from list of @kn's associated open files. If list of
595 * associated open files becomes empty, disassociate and free
601 static void kernfs_unlink_open_file(struct kernfs_node *kn,
602 struct kernfs_open_file *of,
605 struct kernfs_open_node *on;
608 mutex = kernfs_open_file_mutex_lock(kn);
610 on = kernfs_deref_open_node_locked(kn);
617 if (kn->flags & KERNFS_HAS_RELEASE) {
618 WARN_ON_ONCE(of->released == open_failed);
627 if (list_empty(&on->files)) {
628 rcu_assign_pointer(kn->attr.open, NULL);
629 kfree_rcu(on, rcu_head);
635 static int kernfs_fop_open(struct inode *inode, struct file *file)
637 struct kernfs_node *kn = inode->i_private;
638 struct kernfs_root *root = kernfs_root(kn);
639 const struct kernfs_ops *ops;
640 struct kernfs_open_file *of;
641 bool has_read, has_write, has_mmap;
644 if (!kernfs_get_active(kn))
647 ops = kernfs_ops(kn);
649 has_read = ops->seq_show || ops->read || ops->mmap;
650 has_write = ops->write || ops->mmap;
651 has_mmap = ops->mmap;
653 /* see the flag definition for details */
654 if (root->flags & KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK) {
655 if ((file->f_mode & FMODE_WRITE) &&
656 (!(inode->i_mode & S_IWUGO) || !has_write))
659 if ((file->f_mode & FMODE_READ) &&
660 (!(inode->i_mode & S_IRUGO) || !has_read))
664 /* allocate a kernfs_open_file for the file */
666 of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL);
671 * The following is done to give a different lockdep key to
672 * @of->mutex for files which implement mmap. This is a rather
673 * crude way to avoid false positive lockdep warning around
674 * mm->mmap_lock - mmap nests @of->mutex under mm->mmap_lock and
675 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
676 * which mm->mmap_lock nests, while holding @of->mutex. As each
677 * open file has a separate mutex, it's okay as long as those don't
678 * happen on the same file. At this point, we can't easily give
679 * each file a separate locking class. Let's differentiate on
680 * whether the file has mmap or not for now.
682 * Both paths of the branch look the same. They're supposed to
683 * look that way and give @of->mutex different static lockdep keys.
686 mutex_init(&of->mutex);
688 mutex_init(&of->mutex);
694 * Write path needs to atomic_write_len outside active reference.
695 * Cache it in open_file. See kernfs_fop_write_iter() for details.
697 of->atomic_write_len = ops->atomic_write_len;
701 * ->seq_show is incompatible with ->prealloc,
702 * as seq_read does its own allocation.
703 * ->read must be used instead.
705 if (ops->prealloc && ops->seq_show)
708 int len = of->atomic_write_len ?: PAGE_SIZE;
709 of->prealloc_buf = kmalloc(len + 1, GFP_KERNEL);
711 if (!of->prealloc_buf)
713 mutex_init(&of->prealloc_mutex);
717 * Always instantiate seq_file even if read access doesn't use
718 * seq_file or is not requested. This unifies private data access
719 * and readable regular files are the vast majority anyway.
722 error = seq_open(file, &kernfs_seq_ops);
724 error = seq_open(file, NULL);
728 of->seq_file = file->private_data;
729 of->seq_file->private = of;
731 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
732 if (file->f_mode & FMODE_WRITE)
733 file->f_mode |= FMODE_PWRITE;
735 /* make sure we have open node struct */
736 error = kernfs_get_open_node(kn, of);
738 goto err_seq_release;
741 /* nobody has access to @of yet, skip @of->mutex */
742 error = ops->open(of);
747 /* open succeeded, put active references */
748 kernfs_put_active(kn);
752 kernfs_unlink_open_file(kn, of, true);
754 seq_release(inode, file);
756 kfree(of->prealloc_buf);
759 kernfs_put_active(kn);
763 /* used from release/drain to ensure that ->release() is called exactly once */
764 static void kernfs_release_file(struct kernfs_node *kn,
765 struct kernfs_open_file *of)
768 * @of is guaranteed to have no other file operations in flight and
769 * we just want to synchronize release and drain paths.
770 * @kernfs_open_file_mutex_ptr(kn) is enough. @of->mutex can't be used
771 * here because drain path may be called from places which can
772 * cause circular dependency.
774 lockdep_assert_held(kernfs_open_file_mutex_ptr(kn));
778 * A file is never detached without being released and we
779 * need to be able to release files which are deactivated
780 * and being drained. Don't use kernfs_ops().
782 kn->attr.ops->release(of);
784 of_on(of)->nr_to_release--;
788 static int kernfs_fop_release(struct inode *inode, struct file *filp)
790 struct kernfs_node *kn = inode->i_private;
791 struct kernfs_open_file *of = kernfs_of(filp);
793 if (kn->flags & KERNFS_HAS_RELEASE) {
796 mutex = kernfs_open_file_mutex_lock(kn);
797 kernfs_release_file(kn, of);
801 kernfs_unlink_open_file(kn, of, false);
802 seq_release(inode, filp);
803 kfree(of->prealloc_buf);
809 bool kernfs_should_drain_open_files(struct kernfs_node *kn)
811 struct kernfs_open_node *on;
815 * @kn being deactivated guarantees that @kn->attr.open can't change
816 * beneath us making the lockless test below safe.
818 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
821 on = rcu_dereference(kn->attr.open);
822 ret = on && (on->nr_mmapped || on->nr_to_release);
828 void kernfs_drain_open_files(struct kernfs_node *kn)
830 struct kernfs_open_node *on;
831 struct kernfs_open_file *of;
834 mutex = kernfs_open_file_mutex_lock(kn);
835 on = kernfs_deref_open_node_locked(kn);
841 list_for_each_entry(of, &on->files, list) {
842 struct inode *inode = file_inode(of->file);
845 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
850 if (kn->flags & KERNFS_HAS_RELEASE)
851 kernfs_release_file(kn, of);
854 WARN_ON_ONCE(on->nr_mmapped || on->nr_to_release);
859 * Kernfs attribute files are pollable. The idea is that you read
860 * the content and then you use 'poll' or 'select' to wait for
861 * the content to change. When the content changes (assuming the
862 * manager for the kobject supports notification), poll will
863 * return EPOLLERR|EPOLLPRI, and select will return the fd whether
864 * it is waiting for read, write, or exceptions.
865 * Once poll/select indicates that the value has changed, you
866 * need to close and re-open the file, or seek to 0 and read again.
867 * Reminder: this only works for attributes which actively support
868 * it, and it is not possible to test an attribute from userspace
869 * to see if it supports poll (Neither 'poll' nor 'select' return
870 * an appropriate error code). When in doubt, set a suitable timeout value.
872 __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait)
874 struct kernfs_open_node *on = of_on(of);
876 poll_wait(of->file, &on->poll, wait);
878 if (of->event != atomic_read(&on->event))
879 return DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI;
881 return DEFAULT_POLLMASK;
884 static __poll_t kernfs_fop_poll(struct file *filp, poll_table *wait)
886 struct kernfs_open_file *of = kernfs_of(filp);
887 struct kernfs_node *kn = kernfs_dentry_node(filp->f_path.dentry);
890 if (!kernfs_get_active(kn))
891 return DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI;
893 if (kn->attr.ops->poll)
894 ret = kn->attr.ops->poll(of, wait);
896 ret = kernfs_generic_poll(of, wait);
898 kernfs_put_active(kn);
902 static void kernfs_notify_workfn(struct work_struct *work)
904 struct kernfs_node *kn;
905 struct kernfs_super_info *info;
906 struct kernfs_root *root;
908 /* pop one off the notify_list */
909 spin_lock_irq(&kernfs_notify_lock);
910 kn = kernfs_notify_list;
911 if (kn == KERNFS_NOTIFY_EOL) {
912 spin_unlock_irq(&kernfs_notify_lock);
915 kernfs_notify_list = kn->attr.notify_next;
916 kn->attr.notify_next = NULL;
917 spin_unlock_irq(&kernfs_notify_lock);
919 root = kernfs_root(kn);
921 down_write(&root->kernfs_rwsem);
923 list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
924 struct kernfs_node *parent;
925 struct inode *p_inode = NULL;
930 * We want fsnotify_modify() on @kn but as the
931 * modifications aren't originating from userland don't
932 * have the matching @file available. Look up the inodes
933 * and generate the events manually.
935 inode = ilookup(info->sb, kernfs_ino(kn));
939 name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name));
940 parent = kernfs_get_parent(kn);
942 p_inode = ilookup(info->sb, kernfs_ino(parent));
944 fsnotify(FS_MODIFY | FS_EVENT_ON_CHILD,
945 inode, FSNOTIFY_EVENT_INODE,
946 p_inode, &name, inode, 0);
954 fsnotify_inode(inode, FS_MODIFY);
959 up_write(&root->kernfs_rwsem);
965 * kernfs_notify - notify a kernfs file
966 * @kn: file to notify
968 * Notify @kn such that poll(2) on @kn wakes up. Maybe be called from any
971 void kernfs_notify(struct kernfs_node *kn)
973 static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn);
975 struct kernfs_open_node *on;
977 if (WARN_ON(kernfs_type(kn) != KERNFS_FILE))
980 /* kick poll immediately */
982 on = rcu_dereference(kn->attr.open);
984 atomic_inc(&on->event);
985 wake_up_interruptible(&on->poll);
989 /* schedule work to kick fsnotify */
990 spin_lock_irqsave(&kernfs_notify_lock, flags);
991 if (!kn->attr.notify_next) {
993 kn->attr.notify_next = kernfs_notify_list;
994 kernfs_notify_list = kn;
995 schedule_work(&kernfs_notify_work);
997 spin_unlock_irqrestore(&kernfs_notify_lock, flags);
999 EXPORT_SYMBOL_GPL(kernfs_notify);
1001 const struct file_operations kernfs_file_fops = {
1002 .read_iter = kernfs_fop_read_iter,
1003 .write_iter = kernfs_fop_write_iter,
1004 .llseek = generic_file_llseek,
1005 .mmap = kernfs_fop_mmap,
1006 .open = kernfs_fop_open,
1007 .release = kernfs_fop_release,
1008 .poll = kernfs_fop_poll,
1009 .fsync = noop_fsync,
1010 .splice_read = generic_file_splice_read,
1011 .splice_write = iter_file_splice_write,
1015 * __kernfs_create_file - kernfs internal function to create a file
1016 * @parent: directory to create the file in
1017 * @name: name of the file
1018 * @mode: mode of the file
1019 * @uid: uid of the file
1020 * @gid: gid of the file
1021 * @size: size of the file
1022 * @ops: kernfs operations for the file
1023 * @priv: private data for the file
1024 * @ns: optional namespace tag of the file
1025 * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
1027 * Returns the created node on success, ERR_PTR() value on error.
1029 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
1031 umode_t mode, kuid_t uid, kgid_t gid,
1033 const struct kernfs_ops *ops,
1034 void *priv, const void *ns,
1035 struct lock_class_key *key)
1037 struct kernfs_node *kn;
1041 flags = KERNFS_FILE;
1043 kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG,
1046 return ERR_PTR(-ENOMEM);
1049 kn->attr.size = size;
1053 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1055 lockdep_init_map(&kn->dep_map, "kn->active", key, 0);
1056 kn->flags |= KERNFS_LOCKDEP;
1061 * kn->attr.ops is accessible only while holding active ref. We
1062 * need to know whether some ops are implemented outside active
1063 * ref. Cache their existence in flags.
1066 kn->flags |= KERNFS_HAS_SEQ_SHOW;
1068 kn->flags |= KERNFS_HAS_MMAP;
1070 kn->flags |= KERNFS_HAS_RELEASE;
1072 rc = kernfs_add_one(kn);