Commit fdbffaa4 authored by Tejun Heo's avatar Tejun Heo Committed by Greg Kroah-Hartman

sysfs, kernfs: prepare mmap path for kernfs

We're in the process of separating out core sysfs functionality into
kernfs which will deal with sysfs_dirents directly.  This patch
rearranges mmap path so that the kernfs and sysfs parts are separate.

sysfs_kf_bin_mmap() which handles the interaction with bin_attribute
mmap method is factored out of sysfs_bin_mmap(), which is renamed to
kernfs_file_mmap().  All vma ops are renamed accordingly.

sysfs_bin_mmap() is updated such that it can be used for both file
types.  This will eventually allow using the same file_operations for
both file types, which is necessary to separate out kernfs.

This patch doesn't introduce any behavior changes.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 50b38ca0
...@@ -361,7 +361,19 @@ static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf, ...@@ -361,7 +361,19 @@ static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
return len; return len;
} }
static void sysfs_bin_vma_open(struct vm_area_struct *vma) static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
struct vm_area_struct *vma)
{
struct bin_attribute *battr = of->sd->priv;
struct kobject *kobj = of->sd->s_parent->priv;
if (!battr->mmap)
return -ENODEV;
return battr->mmap(of->file, kobj, battr, vma);
}
static void kernfs_vma_open(struct vm_area_struct *vma)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -378,7 +390,7 @@ static void sysfs_bin_vma_open(struct vm_area_struct *vma) ...@@ -378,7 +390,7 @@ static void sysfs_bin_vma_open(struct vm_area_struct *vma)
sysfs_put_active(of->sd); sysfs_put_active(of->sd);
} }
static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -398,8 +410,8 @@ static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) ...@@ -398,8 +410,8 @@ static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
return ret; return ret;
} }
static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma, static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
struct vm_fault *vmf) struct vm_fault *vmf)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -421,8 +433,8 @@ static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma, ...@@ -421,8 +433,8 @@ static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
return ret; return ret;
} }
static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr, static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
void *buf, int len, int write) void *buf, int len, int write)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -443,8 +455,8 @@ static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr, ...@@ -443,8 +455,8 @@ static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
} }
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
static int sysfs_bin_set_policy(struct vm_area_struct *vma, static int kernfs_vma_set_policy(struct vm_area_struct *vma,
struct mempolicy *new) struct mempolicy *new)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -464,8 +476,8 @@ static int sysfs_bin_set_policy(struct vm_area_struct *vma, ...@@ -464,8 +476,8 @@ static int sysfs_bin_set_policy(struct vm_area_struct *vma,
return ret; return ret;
} }
static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma, static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
unsigned long addr) unsigned long addr)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -485,8 +497,9 @@ static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma, ...@@ -485,8 +497,9 @@ static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
return pol; return pol;
} }
static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, static int kernfs_vma_migrate(struct vm_area_struct *vma,
const nodemask_t *to, unsigned long flags) const nodemask_t *from, const nodemask_t *to,
unsigned long flags)
{ {
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
...@@ -507,36 +520,31 @@ static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, ...@@ -507,36 +520,31 @@ static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
} }
#endif #endif
static const struct vm_operations_struct sysfs_bin_vm_ops = { static const struct vm_operations_struct kernfs_vm_ops = {
.open = sysfs_bin_vma_open, .open = kernfs_vma_open,
.fault = sysfs_bin_fault, .fault = kernfs_vma_fault,
.page_mkwrite = sysfs_bin_page_mkwrite, .page_mkwrite = kernfs_vma_page_mkwrite,
.access = sysfs_bin_access, .access = kernfs_vma_access,
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
.set_policy = sysfs_bin_set_policy, .set_policy = kernfs_vma_set_policy,
.get_policy = sysfs_bin_get_policy, .get_policy = kernfs_vma_get_policy,
.migrate = sysfs_bin_migrate, .migrate = kernfs_vma_migrate,
#endif #endif
}; };
static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma) static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
{ {
struct sysfs_open_file *of = sysfs_of(file); struct sysfs_open_file *of = sysfs_of(file);
struct bin_attribute *battr = of->sd->priv;
struct kobject *kobj = of->sd->s_parent->priv;
int rc; int rc;
mutex_lock(&of->mutex); mutex_lock(&of->mutex);
/* need of->sd for battr, its parent for kobj */
rc = -ENODEV; rc = -ENODEV;
if (!sysfs_get_active(of->sd)) if (!sysfs_get_active(of->sd))
goto out_unlock; goto out_unlock;
if (!battr->mmap) if (sysfs_is_bin(of->sd))
goto out_put; rc = sysfs_kf_bin_mmap(of, vma);
rc = battr->mmap(file, kobj, battr, vma);
if (rc) if (rc)
goto out_put; goto out_put;
...@@ -563,7 +571,7 @@ static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -563,7 +571,7 @@ static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
rc = 0; rc = 0;
of->mmapped = 1; of->mmapped = 1;
of->vm_ops = vma->vm_ops; of->vm_ops = vma->vm_ops;
vma->vm_ops = &sysfs_bin_vm_ops; vma->vm_ops = &kernfs_vm_ops;
out_put: out_put:
sysfs_put_active(of->sd); sysfs_put_active(of->sd);
out_unlock: out_unlock:
...@@ -877,6 +885,7 @@ const struct file_operations sysfs_file_operations = { ...@@ -877,6 +885,7 @@ const struct file_operations sysfs_file_operations = {
.read = kernfs_file_read, .read = kernfs_file_read,
.write = kernfs_file_write, .write = kernfs_file_write,
.llseek = generic_file_llseek, .llseek = generic_file_llseek,
.mmap = kernfs_file_mmap,
.open = sysfs_open_file, .open = sysfs_open_file,
.release = sysfs_release, .release = sysfs_release,
.poll = sysfs_poll, .poll = sysfs_poll,
...@@ -886,7 +895,7 @@ const struct file_operations sysfs_bin_operations = { ...@@ -886,7 +895,7 @@ const struct file_operations sysfs_bin_operations = {
.read = kernfs_file_read, .read = kernfs_file_read,
.write = kernfs_file_write, .write = kernfs_file_write,
.llseek = generic_file_llseek, .llseek = generic_file_llseek,
.mmap = sysfs_bin_mmap, .mmap = kernfs_file_mmap,
.open = sysfs_open_file, .open = sysfs_open_file,
.release = sysfs_release, .release = sysfs_release,
.poll = sysfs_poll, .poll = sysfs_poll,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment