Skip to content

Commit 5d60418

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: introduce kernfs_setattr()
Introduce kernfs setattr interface - kernfs_setattr(). sysfs_sd_setattr() is renamed to __kernfs_setattr() and kernfs_setattr() is a simple wrapper around it with sysfs_mutex locking. sysfs_chmod_file() is updated to get an explicit ref on kobj->sd and then invoke kernfs_setattr() so that it doesn't have to use internal interface. This patch doesn't introduce any behavior differences. v2: Dummy implementation for !CONFIG_SYSFS updated to return -ENOSYS. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 890ece1 commit 5d60418

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -922,19 +922,16 @@ int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
922922
struct iattr newattrs;
923923
int rc;
924924

925-
mutex_lock(&sysfs_mutex);
926-
927-
rc = -ENOENT;
928-
sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
925+
sd = sysfs_get_dirent(kobj->sd, attr->name);
929926
if (!sd)
930-
goto out;
927+
return -ENOENT;
931928

932929
newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
933930
newattrs.ia_valid = ATTR_MODE;
934-
rc = sysfs_sd_setattr(sd, &newattrs);
935931

936-
out:
937-
mutex_unlock(&sysfs_mutex);
932+
rc = kernfs_setattr(sd, &newattrs);
933+
934+
sysfs_put(sd);
938935
return rc;
939936
}
940937
EXPORT_SYMBOL_GPL(sysfs_chmod_file);

‎fs/sysfs/inode.c‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
6767
return attrs;
6868
}
6969

70-
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr)
70+
static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr)
7171
{
7272
struct sysfs_inode_attrs *sd_attrs;
7373
struct iattr *iattrs;
@@ -102,6 +102,23 @@ int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr)
102102
return 0;
103103
}
104104

105+
/**
106+
* kernfs_setattr - set iattr on a node
107+
* @sd: target node
108+
* @iattr: iattr to set
109+
*
110+
* Returns 0 on success, -errno on failure.
111+
*/
112+
int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr)
113+
{
114+
int ret;
115+
116+
mutex_lock(&sysfs_mutex);
117+
ret = __kernfs_setattr(sd, iattr);
118+
mutex_unlock(&sysfs_mutex);
119+
return ret;
120+
}
121+
105122
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
106123
{
107124
struct inode *inode = dentry->d_inode;
@@ -116,7 +133,7 @@ int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
116133
if (error)
117134
goto out;
118135

119-
error = sysfs_sd_setattr(sd, iattr);
136+
error = __kernfs_setattr(sd, iattr);
120137
if (error)
121138
goto out;
122139

‎fs/sysfs/sysfs.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
207207
*/
208208
struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd);
209209
void sysfs_evict_inode(struct inode *inode);
210-
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
211210
int sysfs_permission(struct inode *inode, int mask);
212211
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
213212
int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,

‎include/linux/kernfs.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <linux/kernel.h>
1111
#include <linux/err.h>
1212

13+
struct file;
14+
struct iattr;
15+
1316
struct sysfs_dirent;
1417

1518
#ifdef CONFIG_SYSFS
@@ -22,6 +25,7 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
2225
const void *ns);
2326
int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
2427
const char *new_name, const void *new_ns);
28+
int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
2529

2630
#else /* CONFIG_SYSFS */
2731

@@ -41,6 +45,10 @@ static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
4145
const char *new_name, const void *new_ns)
4246
{ return -ENOSYS; }
4347

48+
static inline int kernfs_setattr(struct sysfs_dirent *sd,
49+
const struct iattr *iattr)
50+
{ return -ENOSYS; }
51+
4452
#endif /* CONFIG_SYSFS */
4553

4654
static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,

0 commit comments

Comments
 (0)