Skip to content

Commit 471bd7b

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: add sysfs_dirent->s_attr.size
sysfs sets the size of regular files unconditionally at PAGE_SIZE and takes the size of bin files from bin_attribute. The latter is a pretty bad interface which forces bin_attribute users to create a separate copy of bin_attribute for each instance of the file - e.g. pci resource files. Add sysfs_dirent->s_attr.size so that the size can be specified separately. This unifies inode init paths of ATTR and BIN_ATTR identical and allows for generic size handling for kernfs. Unfortunately, this grows the size of sysfs_dirent by sizeof(loff_t). Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f6acf8b commit 471bd7b

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
923923
const struct kernfs_ops *ops;
924924
struct sysfs_addrm_cxt acxt;
925925
struct sysfs_dirent *sd;
926+
loff_t size;
926927
int rc;
927928

928929
if (type == SYSFS_KOBJ_ATTR) {
@@ -943,6 +944,8 @@ int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
943944
ops = &sysfs_file_kfops_wo;
944945
else
945946
ops = &sysfs_file_kfops_empty;
947+
948+
size = PAGE_SIZE;
946949
} else {
947950
struct bin_attribute *battr = (void *)attr;
948951

@@ -954,13 +957,16 @@ int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
954957
ops = &sysfs_bin_kfops_wo;
955958
else
956959
ops = &sysfs_file_kfops_empty;
960+
961+
size = battr->size;
957962
}
958963

959964
sd = sysfs_new_dirent(attr->name, mode, type);
960965
if (!sd)
961966
return -ENOMEM;
962967

963968
sd->s_attr.ops = ops;
969+
sd->s_attr.size = size;
964970
sd->s_ns = ns;
965971
sd->priv = (void *)attr;
966972
sysfs_dirent_init_lockdep(sd);

‎fs/sysfs/inode.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
254254

255255
static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
256256
{
257-
struct bin_attribute *bin_attr;
258-
259257
inode->i_private = sysfs_get(sd);
260258
inode->i_mapping->a_ops = &sysfs_aops;
261259
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
@@ -271,12 +269,8 @@ static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
271269
inode->i_fop = &sysfs_dir_operations;
272270
break;
273271
case SYSFS_KOBJ_ATTR:
274-
inode->i_size = PAGE_SIZE;
275-
inode->i_fop = &kernfs_file_operations;
276-
break;
277272
case SYSFS_KOBJ_BIN_ATTR:
278-
bin_attr = sd->priv;
279-
inode->i_size = bin_attr->size;
273+
inode->i_size = sd->s_attr.size;
280274
inode->i_fop = &kernfs_file_operations;
281275
break;
282276
case SYSFS_KOBJ_LINK:

‎fs/sysfs/sysfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct sysfs_elem_symlink {
2929
struct sysfs_elem_attr {
3030
const struct kernfs_ops *ops;
3131
struct sysfs_open_dirent *open;
32+
loff_t size;
3233
};
3334

3435
struct sysfs_inode_attrs {

0 commit comments

Comments
 (0)