2 * fs/kernfs/file.c - kernfs file implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8 * This file is released under the GPLv2.
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/poll.h>
15 #include <linux/pagemap.h>
16 #include <linux/poll.h>
17 #include <linux/sched.h>
19 #include "kernfs-internal.h"
22 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
23 * for each sysfs_dirent with one or more open files.
25 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
26 * protected by sysfs_open_dirent_lock.
28 * filp->private_data points to seq_file whose ->private points to
29 * sysfs_open_file. sysfs_open_files are chained at
30 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
32 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
33 static DEFINE_MUTEX(sysfs_open_file_mutex);
35 struct sysfs_open_dirent {
38 wait_queue_head_t poll;
39 struct list_head files; /* goes through sysfs_open_file.list */
42 static struct sysfs_open_file *sysfs_of(struct file *file)
44 return ((struct seq_file *)file->private_data)->private;
48 * Determine the kernfs_ops for the given sysfs_dirent. This function must
49 * be called while holding an active reference.
51 static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
53 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
54 lockdep_assert_held(sd);
55 return sd->s_attr.ops;
58 static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
60 struct sysfs_open_file *of = sf->private;
61 const struct kernfs_ops *ops;
64 * @of->mutex nests outside active ref and is just to ensure that
65 * the ops aren't called concurrently for the same open file.
67 mutex_lock(&of->mutex);
68 if (!sysfs_get_active(of->sd))
69 return ERR_PTR(-ENODEV);
71 ops = kernfs_ops(of->sd);
73 return ops->seq_start(sf, ppos);
76 * The same behavior and code as single_open(). Returns
77 * !NULL if pos is at the beginning; otherwise, NULL.
83 static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
85 struct sysfs_open_file *of = sf->private;
86 const struct kernfs_ops *ops = kernfs_ops(of->sd);
89 return ops->seq_next(sf, v, ppos);
92 * The same behavior and code as single_open(), always
93 * terminate after the initial read.
100 static void kernfs_seq_stop(struct seq_file *sf, void *v)
102 struct sysfs_open_file *of = sf->private;
103 const struct kernfs_ops *ops = kernfs_ops(of->sd);
106 ops->seq_stop(sf, v);
108 sysfs_put_active(of->sd);
109 mutex_unlock(&of->mutex);
112 static int kernfs_seq_show(struct seq_file *sf, void *v)
114 struct sysfs_open_file *of = sf->private;
116 of->event = atomic_read(&of->sd->s_attr.open->event);
118 return of->sd->s_attr.ops->seq_show(sf, v);
121 static const struct seq_operations kernfs_seq_ops = {
122 .start = kernfs_seq_start,
123 .next = kernfs_seq_next,
124 .stop = kernfs_seq_stop,
125 .show = kernfs_seq_show,
129 * As reading a bin file can have side-effects, the exact offset and bytes
130 * specified in read(2) call should be passed to the read callback making
131 * it difficult to use seq_file. Implement simplistic custom buffering for
134 static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
135 char __user *user_buf, size_t count,
138 ssize_t len = min_t(size_t, count, PAGE_SIZE);
139 const struct kernfs_ops *ops;
142 buf = kmalloc(len, GFP_KERNEL);
147 * @of->mutex nests outside active ref and is just to ensure that
148 * the ops aren't called concurrently for the same open file.
150 mutex_lock(&of->mutex);
151 if (!sysfs_get_active(of->sd)) {
153 mutex_unlock(&of->mutex);
157 ops = kernfs_ops(of->sd);
159 len = ops->read(of, buf, len, *ppos);
163 sysfs_put_active(of->sd);
164 mutex_unlock(&of->mutex);
169 if (copy_to_user(user_buf, buf, len)) {
182 * kernfs_file_read - kernfs vfs read callback
183 * @file: file pointer
184 * @user_buf: data to write
185 * @count: number of bytes
186 * @ppos: starting offset
188 static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
189 size_t count, loff_t *ppos)
191 struct sysfs_open_file *of = sysfs_of(file);
193 if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
194 return seq_read(file, user_buf, count, ppos);
196 return kernfs_file_direct_read(of, user_buf, count, ppos);
200 * kernfs_file_write - kernfs vfs write callback
201 * @file: file pointer
202 * @user_buf: data to write
203 * @count: number of bytes
204 * @ppos: starting offset
206 * Copy data in from userland and pass it to the matching kernfs write
209 * There is no easy way for us to know if userspace is only doing a partial
210 * write, so we don't support them. We expect the entire buffer to come on
211 * the first write. Hint: if you're writing a value, first read the file,
212 * modify only the the value you're changing, then write entire buffer
215 static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
216 size_t count, loff_t *ppos)
218 struct sysfs_open_file *of = sysfs_of(file);
219 ssize_t len = min_t(size_t, count, PAGE_SIZE);
220 const struct kernfs_ops *ops;
223 buf = kmalloc(len + 1, GFP_KERNEL);
227 if (copy_from_user(buf, user_buf, len)) {
231 buf[len] = '\0'; /* guarantee string termination */
234 * @of->mutex nests outside active ref and is just to ensure that
235 * the ops aren't called concurrently for the same open file.
237 mutex_lock(&of->mutex);
238 if (!sysfs_get_active(of->sd)) {
239 mutex_unlock(&of->mutex);
244 ops = kernfs_ops(of->sd);
246 len = ops->write(of, buf, len, *ppos);
250 sysfs_put_active(of->sd);
251 mutex_unlock(&of->mutex);
260 static void kernfs_vma_open(struct vm_area_struct *vma)
262 struct file *file = vma->vm_file;
263 struct sysfs_open_file *of = sysfs_of(file);
268 if (!sysfs_get_active(of->sd))
271 if (of->vm_ops->open)
272 of->vm_ops->open(vma);
274 sysfs_put_active(of->sd);
277 static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
279 struct file *file = vma->vm_file;
280 struct sysfs_open_file *of = sysfs_of(file);
284 return VM_FAULT_SIGBUS;
286 if (!sysfs_get_active(of->sd))
287 return VM_FAULT_SIGBUS;
289 ret = VM_FAULT_SIGBUS;
290 if (of->vm_ops->fault)
291 ret = of->vm_ops->fault(vma, vmf);
293 sysfs_put_active(of->sd);
297 static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
298 struct vm_fault *vmf)
300 struct file *file = vma->vm_file;
301 struct sysfs_open_file *of = sysfs_of(file);
305 return VM_FAULT_SIGBUS;
307 if (!sysfs_get_active(of->sd))
308 return VM_FAULT_SIGBUS;
311 if (of->vm_ops->page_mkwrite)
312 ret = of->vm_ops->page_mkwrite(vma, vmf);
314 file_update_time(file);
316 sysfs_put_active(of->sd);
320 static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
321 void *buf, int len, int write)
323 struct file *file = vma->vm_file;
324 struct sysfs_open_file *of = sysfs_of(file);
330 if (!sysfs_get_active(of->sd))
334 if (of->vm_ops->access)
335 ret = of->vm_ops->access(vma, addr, buf, len, write);
337 sysfs_put_active(of->sd);
342 static int kernfs_vma_set_policy(struct vm_area_struct *vma,
343 struct mempolicy *new)
345 struct file *file = vma->vm_file;
346 struct sysfs_open_file *of = sysfs_of(file);
352 if (!sysfs_get_active(of->sd))
356 if (of->vm_ops->set_policy)
357 ret = of->vm_ops->set_policy(vma, new);
359 sysfs_put_active(of->sd);
363 static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
366 struct file *file = vma->vm_file;
367 struct sysfs_open_file *of = sysfs_of(file);
368 struct mempolicy *pol;
371 return vma->vm_policy;
373 if (!sysfs_get_active(of->sd))
374 return vma->vm_policy;
376 pol = vma->vm_policy;
377 if (of->vm_ops->get_policy)
378 pol = of->vm_ops->get_policy(vma, addr);
380 sysfs_put_active(of->sd);
384 static int kernfs_vma_migrate(struct vm_area_struct *vma,
385 const nodemask_t *from, const nodemask_t *to,
388 struct file *file = vma->vm_file;
389 struct sysfs_open_file *of = sysfs_of(file);
395 if (!sysfs_get_active(of->sd))
399 if (of->vm_ops->migrate)
400 ret = of->vm_ops->migrate(vma, from, to, flags);
402 sysfs_put_active(of->sd);
407 static const struct vm_operations_struct kernfs_vm_ops = {
408 .open = kernfs_vma_open,
409 .fault = kernfs_vma_fault,
410 .page_mkwrite = kernfs_vma_page_mkwrite,
411 .access = kernfs_vma_access,
413 .set_policy = kernfs_vma_set_policy,
414 .get_policy = kernfs_vma_get_policy,
415 .migrate = kernfs_vma_migrate,
419 static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
421 struct sysfs_open_file *of = sysfs_of(file);
422 const struct kernfs_ops *ops;
425 mutex_lock(&of->mutex);
428 if (!sysfs_get_active(of->sd))
431 ops = kernfs_ops(of->sd);
433 rc = ops->mmap(of, vma);
438 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
439 * to satisfy versions of X which crash if the mmap fails: that
440 * substitutes a new vm_file, and we don't then want bin_vm_ops.
442 if (vma->vm_file != file)
446 if (of->mmapped && of->vm_ops != vma->vm_ops)
450 * It is not possible to successfully wrap close.
451 * So error if someone is trying to use close.
454 if (vma->vm_ops && vma->vm_ops->close)
459 of->vm_ops = vma->vm_ops;
460 vma->vm_ops = &kernfs_vm_ops;
462 sysfs_put_active(of->sd);
464 mutex_unlock(&of->mutex);
470 * sysfs_get_open_dirent - get or create sysfs_open_dirent
471 * @sd: target sysfs_dirent
472 * @of: sysfs_open_file for this instance of open
474 * If @sd->s_attr.open exists, increment its reference count;
475 * otherwise, create one. @of is chained to the files list.
478 * Kernel thread context (may sleep).
481 * 0 on success, -errno on failure.
483 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
484 struct sysfs_open_file *of)
486 struct sysfs_open_dirent *od, *new_od = NULL;
489 mutex_lock(&sysfs_open_file_mutex);
490 spin_lock_irq(&sysfs_open_dirent_lock);
492 if (!sd->s_attr.open && new_od) {
493 sd->s_attr.open = new_od;
497 od = sd->s_attr.open;
499 atomic_inc(&od->refcnt);
500 list_add_tail(&of->list, &od->files);
503 spin_unlock_irq(&sysfs_open_dirent_lock);
504 mutex_unlock(&sysfs_open_file_mutex);
511 /* not there, initialize a new one and retry */
512 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
516 atomic_set(&new_od->refcnt, 0);
517 atomic_set(&new_od->event, 1);
518 init_waitqueue_head(&new_od->poll);
519 INIT_LIST_HEAD(&new_od->files);
524 * sysfs_put_open_dirent - put sysfs_open_dirent
525 * @sd: target sysfs_dirent
526 * @of: associated sysfs_open_file
528 * Put @sd->s_attr.open and unlink @of from the files list. If
529 * reference count reaches zero, disassociate and free it.
534 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
535 struct sysfs_open_file *of)
537 struct sysfs_open_dirent *od = sd->s_attr.open;
540 mutex_lock(&sysfs_open_file_mutex);
541 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
546 if (atomic_dec_and_test(&od->refcnt))
547 sd->s_attr.open = NULL;
551 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
552 mutex_unlock(&sysfs_open_file_mutex);
557 static int kernfs_file_open(struct inode *inode, struct file *file)
559 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
560 const struct kernfs_ops *ops;
561 struct sysfs_open_file *of;
562 bool has_read, has_write, has_mmap;
565 if (!sysfs_get_active(attr_sd))
568 ops = kernfs_ops(attr_sd);
570 has_read = ops->seq_show || ops->read || ops->mmap;
571 has_write = ops->write || ops->mmap;
572 has_mmap = ops->mmap;
574 /* check perms and supported operations */
575 if ((file->f_mode & FMODE_WRITE) &&
576 (!(inode->i_mode & S_IWUGO) || !has_write))
579 if ((file->f_mode & FMODE_READ) &&
580 (!(inode->i_mode & S_IRUGO) || !has_read))
583 /* allocate a sysfs_open_file for the file */
585 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
590 * The following is done to give a different lockdep key to
591 * @of->mutex for files which implement mmap. This is a rather
592 * crude way to avoid false positive lockdep warning around
593 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
594 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
595 * which mm->mmap_sem nests, while holding @of->mutex. As each
596 * open file has a separate mutex, it's okay as long as those don't
597 * happen on the same file. At this point, we can't easily give
598 * each file a separate locking class. Let's differentiate on
599 * whether the file has mmap or not for now.
602 mutex_init(&of->mutex);
604 mutex_init(&of->mutex);
610 * Always instantiate seq_file even if read access doesn't use
611 * seq_file or is not requested. This unifies private data access
612 * and readable regular files are the vast majority anyway.
615 error = seq_open(file, &kernfs_seq_ops);
617 error = seq_open(file, NULL);
621 ((struct seq_file *)file->private_data)->private = of;
623 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
624 if (file->f_mode & FMODE_WRITE)
625 file->f_mode |= FMODE_PWRITE;
627 /* make sure we have open dirent struct */
628 error = sysfs_get_open_dirent(attr_sd, of);
632 /* open succeeded, put active references */
633 sysfs_put_active(attr_sd);
637 seq_release(inode, file);
641 sysfs_put_active(attr_sd);
645 static int kernfs_file_release(struct inode *inode, struct file *filp)
647 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
648 struct sysfs_open_file *of = sysfs_of(filp);
650 sysfs_put_open_dirent(sd, of);
651 seq_release(inode, filp);
657 void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
659 struct sysfs_open_dirent *od;
660 struct sysfs_open_file *of;
662 if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
665 spin_lock_irq(&sysfs_open_dirent_lock);
666 od = sd->s_attr.open;
668 atomic_inc(&od->refcnt);
669 spin_unlock_irq(&sysfs_open_dirent_lock);
673 mutex_lock(&sysfs_open_file_mutex);
674 list_for_each_entry(of, &od->files, list) {
675 struct inode *inode = file_inode(of->file);
676 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
678 mutex_unlock(&sysfs_open_file_mutex);
680 sysfs_put_open_dirent(sd, NULL);
683 /* Sysfs attribute files are pollable. The idea is that you read
684 * the content and then you use 'poll' or 'select' to wait for
685 * the content to change. When the content changes (assuming the
686 * manager for the kobject supports notification), poll will
687 * return POLLERR|POLLPRI, and select will return the fd whether
688 * it is waiting for read, write, or exceptions.
689 * Once poll/select indicates that the value has changed, you
690 * need to close and re-open the file, or seek to 0 and read again.
691 * Reminder: this only works for attributes which actively support
692 * it, and it is not possible to test an attribute from userspace
693 * to see if it supports poll (Neither 'poll' nor 'select' return
694 * an appropriate error code). When in doubt, set a suitable timeout value.
696 static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
698 struct sysfs_open_file *of = sysfs_of(filp);
699 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
700 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
702 /* need parent for the kobj, grab both */
703 if (!sysfs_get_active(attr_sd))
706 poll_wait(filp, &od->poll, wait);
708 sysfs_put_active(attr_sd);
710 if (of->event != atomic_read(&od->event))
713 return DEFAULT_POLLMASK;
716 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
720 * kernfs_notify - notify a kernfs file
721 * @sd: file to notify
723 * Notify @sd such that poll(2) on @sd wakes up.
725 void kernfs_notify(struct sysfs_dirent *sd)
727 struct sysfs_open_dirent *od;
730 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
732 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
733 od = sd->s_attr.open;
735 atomic_inc(&od->event);
736 wake_up_interruptible(&od->poll);
740 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
742 EXPORT_SYMBOL_GPL(kernfs_notify);
744 const struct file_operations kernfs_file_operations = {
745 .read = kernfs_file_read,
746 .write = kernfs_file_write,
747 .llseek = generic_file_llseek,
748 .mmap = kernfs_file_mmap,
749 .open = kernfs_file_open,
750 .release = kernfs_file_release,
751 .poll = kernfs_file_poll,
755 * kernfs_create_file_ns_key - create a file
756 * @parent: directory to create the file in
757 * @name: name of the file
758 * @mode: mode of the file
759 * @size: size of the file
760 * @ops: kernfs operations for the file
761 * @priv: private data for the file
762 * @ns: optional namespace tag of the file
763 * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
765 * Returns the created node on success, ERR_PTR() value on error.
767 struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
769 umode_t mode, loff_t size,
770 const struct kernfs_ops *ops,
771 void *priv, const void *ns,
772 struct lock_class_key *key)
774 struct sysfs_addrm_cxt acxt;
775 struct sysfs_dirent *sd;
778 sd = sysfs_new_dirent(name, (mode & S_IALLUGO) | S_IFREG,
781 return ERR_PTR(-ENOMEM);
783 sd->s_attr.ops = ops;
784 sd->s_attr.size = size;
788 #ifdef CONFIG_DEBUG_LOCK_ALLOC
790 lockdep_init_map(&sd->dep_map, "s_active", key, 0);
791 sd->s_flags |= SYSFS_FLAG_LOCKDEP;
796 * sd->s_attr.ops is accesible only while holding active ref. We
797 * need to know whether some ops are implemented outside active
798 * ref. Cache their existence in flags.
801 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
803 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
805 sysfs_addrm_start(&acxt);
806 rc = sysfs_add_one(&acxt, sd, parent);
807 sysfs_addrm_finish(&acxt);