Skip to content

Commit fdbffaa

Browse files
htejungregkh
authored andcommitted
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: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 50b38ca commit fdbffaa

1 file changed

Lines changed: 39 additions & 30 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,19 @@ static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
361361
return len;
362362
}
363363

364-
static void sysfs_bin_vma_open(struct vm_area_struct *vma)
364+
static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
365+
struct vm_area_struct *vma)
366+
{
367+
struct bin_attribute *battr = of->sd->priv;
368+
struct kobject *kobj = of->sd->s_parent->priv;
369+
370+
if (!battr->mmap)
371+
return -ENODEV;
372+
373+
return battr->mmap(of->file, kobj, battr, vma);
374+
}
375+
376+
static void kernfs_vma_open(struct vm_area_struct *vma)
365377
{
366378
struct file *file = vma->vm_file;
367379
struct sysfs_open_file *of = sysfs_of(file);
@@ -378,7 +390,7 @@ static void sysfs_bin_vma_open(struct vm_area_struct *vma)
378390
sysfs_put_active(of->sd);
379391
}
380392

381-
static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
393+
static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
382394
{
383395
struct file *file = vma->vm_file;
384396
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)
398410
return ret;
399411
}
400412

401-
static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
402-
struct vm_fault *vmf)
413+
static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
414+
struct vm_fault *vmf)
403415
{
404416
struct file *file = vma->vm_file;
405417
struct sysfs_open_file *of = sysfs_of(file);
@@ -421,8 +433,8 @@ static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
421433
return ret;
422434
}
423435

424-
static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
425-
void *buf, int len, int write)
436+
static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
437+
void *buf, int len, int write)
426438
{
427439
struct file *file = vma->vm_file;
428440
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,
443455
}
444456

445457
#ifdef CONFIG_NUMA
446-
static int sysfs_bin_set_policy(struct vm_area_struct *vma,
447-
struct mempolicy *new)
458+
static int kernfs_vma_set_policy(struct vm_area_struct *vma,
459+
struct mempolicy *new)
448460
{
449461
struct file *file = vma->vm_file;
450462
struct sysfs_open_file *of = sysfs_of(file);
@@ -464,8 +476,8 @@ static int sysfs_bin_set_policy(struct vm_area_struct *vma,
464476
return ret;
465477
}
466478

467-
static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
468-
unsigned long addr)
479+
static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
480+
unsigned long addr)
469481
{
470482
struct file *file = vma->vm_file;
471483
struct sysfs_open_file *of = sysfs_of(file);
@@ -485,8 +497,9 @@ static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
485497
return pol;
486498
}
487499

488-
static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
489-
const nodemask_t *to, unsigned long flags)
500+
static int kernfs_vma_migrate(struct vm_area_struct *vma,
501+
const nodemask_t *from, const nodemask_t *to,
502+
unsigned long flags)
490503
{
491504
struct file *file = vma->vm_file;
492505
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,
507520
}
508521
#endif
509522

510-
static const struct vm_operations_struct sysfs_bin_vm_ops = {
511-
.open = sysfs_bin_vma_open,
512-
.fault = sysfs_bin_fault,
513-
.page_mkwrite = sysfs_bin_page_mkwrite,
514-
.access = sysfs_bin_access,
523+
static const struct vm_operations_struct kernfs_vm_ops = {
524+
.open = kernfs_vma_open,
525+
.fault = kernfs_vma_fault,
526+
.page_mkwrite = kernfs_vma_page_mkwrite,
527+
.access = kernfs_vma_access,
515528
#ifdef CONFIG_NUMA
516-
.set_policy = sysfs_bin_set_policy,
517-
.get_policy = sysfs_bin_get_policy,
518-
.migrate = sysfs_bin_migrate,
529+
.set_policy = kernfs_vma_set_policy,
530+
.get_policy = kernfs_vma_get_policy,
531+
.migrate = kernfs_vma_migrate,
519532
#endif
520533
};
521534

522-
static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
535+
static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
523536
{
524537
struct sysfs_open_file *of = sysfs_of(file);
525-
struct bin_attribute *battr = of->sd->priv;
526-
struct kobject *kobj = of->sd->s_parent->priv;
527538
int rc;
528539

529540
mutex_lock(&of->mutex);
530541

531-
/* need of->sd for battr, its parent for kobj */
532542
rc = -ENODEV;
533543
if (!sysfs_get_active(of->sd))
534544
goto out_unlock;
535545

536-
if (!battr->mmap)
537-
goto out_put;
538-
539-
rc = battr->mmap(file, kobj, battr, vma);
546+
if (sysfs_is_bin(of->sd))
547+
rc = sysfs_kf_bin_mmap(of, vma);
540548
if (rc)
541549
goto out_put;
542550

@@ -563,7 +571,7 @@ static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
563571
rc = 0;
564572
of->mmapped = 1;
565573
of->vm_ops = vma->vm_ops;
566-
vma->vm_ops = &sysfs_bin_vm_ops;
574+
vma->vm_ops = &kernfs_vm_ops;
567575
out_put:
568576
sysfs_put_active(of->sd);
569577
out_unlock:
@@ -877,6 +885,7 @@ const struct file_operations sysfs_file_operations = {
877885
.read = kernfs_file_read,
878886
.write = kernfs_file_write,
879887
.llseek = generic_file_llseek,
888+
.mmap = kernfs_file_mmap,
880889
.open = sysfs_open_file,
881890
.release = sysfs_release,
882891
.poll = sysfs_poll,
@@ -886,7 +895,7 @@ const struct file_operations sysfs_bin_operations = {
886895
.read = kernfs_file_read,
887896
.write = kernfs_file_write,
888897
.llseek = generic_file_llseek,
889-
.mmap = sysfs_bin_mmap,
898+
.mmap = kernfs_file_mmap,
890899
.open = sysfs_open_file,
891900
.release = sysfs_release,
892901
.poll = sysfs_poll,

0 commit comments

Comments
 (0)