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>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
73d9714627
commit
49fe604781
@@ -610,37 +610,39 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
|
|||||||
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
|
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
|
||||||
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
|
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
|
||||||
struct sysfs_open_file *of;
|
struct sysfs_open_file *of;
|
||||||
const struct sysfs_ops *ops;
|
bool has_read, has_write;
|
||||||
int error = -EACCES;
|
int error = -EACCES;
|
||||||
|
|
||||||
/* need attr_sd for attr and ops, its parent for kobj */
|
/* need attr_sd for attr and ops, its parent for kobj */
|
||||||
if (!sysfs_get_active(attr_sd))
|
if (!sysfs_get_active(attr_sd))
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
|
if (sysfs_is_bin(attr_sd)) {
|
||||||
|
struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
|
||||||
|
|
||||||
|
has_read = battr->read || battr->mmap;
|
||||||
|
has_write = battr->write || battr->mmap;
|
||||||
|
} else {
|
||||||
|
const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
|
||||||
|
|
||||||
/* every kobject with an attribute needs a ktype assigned */
|
/* every kobject with an attribute needs a ktype assigned */
|
||||||
ops = sysfs_file_ops(attr_sd);
|
|
||||||
if (WARN(!ops, KERN_ERR
|
if (WARN(!ops, KERN_ERR
|
||||||
"missing sysfs attribute operations for kobject: %s\n",
|
"missing sysfs attribute operations for kobject: %s\n",
|
||||||
kobject_name(kobj)))
|
kobject_name(kobj)))
|
||||||
goto err_out;
|
goto err_out;
|
||||||
|
|
||||||
/* File needs write support.
|
has_read = ops->show;
|
||||||
* The inode's perms must say it's ok,
|
has_write = ops->store;
|
||||||
* and we must have a store method.
|
|
||||||
*/
|
|
||||||
if (file->f_mode & FMODE_WRITE) {
|
|
||||||
if (!(inode->i_mode & S_IWUGO) || !ops->store)
|
|
||||||
goto err_out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* File needs read support.
|
/* check perms and supported operations */
|
||||||
* The inode's perms must say it's ok, and we there
|
if ((file->f_mode & FMODE_WRITE) &&
|
||||||
* must be a show method for it.
|
(!(inode->i_mode & S_IWUGO) || !has_write))
|
||||||
*/
|
goto err_out;
|
||||||
if (file->f_mode & FMODE_READ) {
|
|
||||||
if (!(inode->i_mode & S_IRUGO) || !ops->show)
|
if ((file->f_mode & FMODE_READ) &&
|
||||||
|
(!(inode->i_mode & S_IRUGO) || !has_read))
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
|
||||||
|
|
||||||
/* allocate a sysfs_open_file for the file */
|
/* allocate a sysfs_open_file for the file */
|
||||||
error = -ENOMEM;
|
error = -ENOMEM;
|
||||||
@@ -653,10 +655,13 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
|
|||||||
of->file = file;
|
of->file = file;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Always instantiate seq_file even if read access is not
|
* Always instantiate seq_file even if read access doesn't use
|
||||||
* implemented or requested. This unifies private data access and
|
* seq_file or is not requested. This unifies private data access
|
||||||
* most files are readable anyway.
|
* and readable regular files are the vast majority anyway.
|
||||||
*/
|
*/
|
||||||
|
if (sysfs_is_bin(attr_sd))
|
||||||
|
error = single_open(file, NULL, of);
|
||||||
|
else
|
||||||
error = single_open(file, sysfs_seq_show, of);
|
error = single_open(file, sysfs_seq_show, of);
|
||||||
if (error)
|
if (error)
|
||||||
goto err_free;
|
goto err_free;
|
||||||
@@ -807,6 +812,9 @@ const struct file_operations sysfs_bin_operations = {
|
|||||||
.write = sysfs_write_file,
|
.write = sysfs_write_file,
|
||||||
.llseek = generic_file_llseek,
|
.llseek = generic_file_llseek,
|
||||||
.mmap = sysfs_bin_mmap,
|
.mmap = sysfs_bin_mmap,
|
||||||
|
.open = sysfs_open_file,
|
||||||
|
.release = sysfs_release,
|
||||||
|
.poll = sysfs_poll,
|
||||||
};
|
};
|
||||||
|
|
||||||
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
|
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
|
||||||
|
Reference in New Issue
Block a user