Skip to content

Commit 35df63c

Browse files
ebiedermgregkh
authored andcommitted
sysfs: Fix locking and factor out sysfs_sd_setattr
Cleanly separate the work that is specific to setting the attributes of a sysfs_dirent from what is needed to update the attributes of a vfs inode. Additionally grab the sysfs_mutex to keep any nasties from surprising us when updating the sysfs_dirent. Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 4be3df2 commit 35df63c

2 files changed

Lines changed: 32 additions & 20 deletions

File tree

‎fs/sysfs/inode.c‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,14 @@ static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
6565
return attrs;
6666
}
6767

68-
int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
68+
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
6969
{
70-
struct inode * inode = dentry->d_inode;
71-
struct sysfs_dirent * sd = dentry->d_fsdata;
7270
struct sysfs_inode_attrs *sd_attrs;
7371
struct iattr *iattrs;
7472
unsigned int ia_valid = iattr->ia_valid;
75-
int error;
76-
77-
if (!sd)
78-
return -EINVAL;
7973

8074
sd_attrs = sd->s_iattr;
8175

82-
error = inode_change_ok(inode, iattr);
83-
if (error)
84-
return error;
85-
86-
iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
87-
88-
error = inode_setattr(inode, iattr);
89-
if (error)
90-
return error;
91-
9276
if (!sd_attrs) {
9377
/* setting attributes for the first time, allocate now */
9478
sd_attrs = sysfs_init_inode_attrs(sd);
@@ -111,12 +95,39 @@ int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
11195
iattrs->ia_ctime = iattr->ia_ctime;
11296
if (ia_valid & ATTR_MODE) {
11397
umode_t mode = iattr->ia_mode;
114-
115-
if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
116-
mode &= ~S_ISGID;
11798
iattrs->ia_mode = sd->s_mode = mode;
11899
}
119100
}
101+
return 0;
102+
}
103+
104+
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
105+
{
106+
struct inode *inode = dentry->d_inode;
107+
struct sysfs_dirent *sd = dentry->d_fsdata;
108+
int error;
109+
110+
if (!sd)
111+
return -EINVAL;
112+
113+
error = inode_change_ok(inode, iattr);
114+
if (error)
115+
return error;
116+
117+
iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
118+
if (iattr->ia_valid & ATTR_MODE) {
119+
if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
120+
iattr->ia_mode &= ~S_ISGID;
121+
}
122+
123+
error = inode_setattr(inode, iattr);
124+
if (error)
125+
return error;
126+
127+
mutex_lock(&sysfs_mutex);
128+
error = sysfs_sd_setattr(sd, iattr);
129+
mutex_unlock(&sysfs_mutex);
130+
120131
return error;
121132
}
122133

‎fs/sysfs/sysfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
155155
*/
156156
struct inode *sysfs_get_inode(struct sysfs_dirent *sd);
157157
void sysfs_delete_inode(struct inode *inode);
158+
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
158159
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
159160
int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
160161
size_t size, int flags);

0 commit comments

Comments
 (0)