Skip to content

Commit 49fe604

Browse files
htejungregkh
authored andcommitted
sysfs: prepare open path for unified regular / bin file handling
sysfs bin file handling will be merged into the regular file support. This patch prepares the open path. This patch updates sysfs_open_file() such that it can handle both regular and bin files. This is a preparation and the new bin file path isn't used yet. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 73d9714 commit 49fe604

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -610,38 +610,40 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
610610
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
611611
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
612612
struct sysfs_open_file *of;
613-
const struct sysfs_ops *ops;
613+
bool has_read, has_write;
614614
int error = -EACCES;
615615

616616
/* need attr_sd for attr and ops, its parent for kobj */
617617
if (!sysfs_get_active(attr_sd))
618618
return -ENODEV;
619619

620-
/* every kobject with an attribute needs a ktype assigned */
621-
ops = sysfs_file_ops(attr_sd);
622-
if (WARN(!ops, KERN_ERR
623-
"missing sysfs attribute operations for kobject: %s\n",
624-
kobject_name(kobj)))
625-
goto err_out;
620+
if (sysfs_is_bin(attr_sd)) {
621+
struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
626622

627-
/* File needs write support.
628-
* The inode's perms must say it's ok,
629-
* and we must have a store method.
630-
*/
631-
if (file->f_mode & FMODE_WRITE) {
632-
if (!(inode->i_mode & S_IWUGO) || !ops->store)
633-
goto err_out;
634-
}
623+
has_read = battr->read || battr->mmap;
624+
has_write = battr->write || battr->mmap;
625+
} else {
626+
const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
635627

636-
/* File needs read support.
637-
* The inode's perms must say it's ok, and we there
638-
* must be a show method for it.
639-
*/
640-
if (file->f_mode & FMODE_READ) {
641-
if (!(inode->i_mode & S_IRUGO) || !ops->show)
628+
/* every kobject with an attribute needs a ktype assigned */
629+
if (WARN(!ops, KERN_ERR
630+
"missing sysfs attribute operations for kobject: %s\n",
631+
kobject_name(kobj)))
642632
goto err_out;
633+
634+
has_read = ops->show;
635+
has_write = ops->store;
643636
}
644637

638+
/* check perms and supported operations */
639+
if ((file->f_mode & FMODE_WRITE) &&
640+
(!(inode->i_mode & S_IWUGO) || !has_write))
641+
goto err_out;
642+
643+
if ((file->f_mode & FMODE_READ) &&
644+
(!(inode->i_mode & S_IRUGO) || !has_read))
645+
goto err_out;
646+
645647
/* allocate a sysfs_open_file for the file */
646648
error = -ENOMEM;
647649
of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
@@ -653,11 +655,14 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
653655
of->file = file;
654656

655657
/*
656-
* Always instantiate seq_file even if read access is not
657-
* implemented or requested. This unifies private data access and
658-
* most files are readable anyway.
658+
* Always instantiate seq_file even if read access doesn't use
659+
* seq_file or is not requested. This unifies private data access
660+
* and readable regular files are the vast majority anyway.
659661
*/
660-
error = single_open(file, sysfs_seq_show, of);
662+
if (sysfs_is_bin(attr_sd))
663+
error = single_open(file, NULL, of);
664+
else
665+
error = single_open(file, sysfs_seq_show, of);
661666
if (error)
662667
goto err_free;
663668

@@ -807,6 +812,9 @@ const struct file_operations sysfs_bin_operations = {
807812
.write = sysfs_write_file,
808813
.llseek = generic_file_llseek,
809814
.mmap = sysfs_bin_mmap,
815+
.open = sysfs_open_file,
816+
.release = sysfs_release,
817+
.poll = sysfs_poll,
810818
};
811819

812820
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,

0 commit comments

Comments
 (0)