Skip to content

Commit 0f42389

Browse files
committed
[SCSI] sysfs: make group is_valid return a mode_t
We have a problem in scsi_transport_spi in that we need to customise not only the visibility of the attributes, but also their mode. Fix this by making the is_visible() callback return a mode, with 0 indicating is not visible. Also add a sysfs_update_group() API to allow us to change either the visibility or mode of the files at any time on the fly. Acked-by: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
1 parent bbd1ae4 commit 0f42389

4 files changed

Lines changed: 85 additions & 18 deletions

File tree

‎fs/sysfs/file.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,10 @@ const struct file_operations sysfs_file_operations = {
477477
.poll = sysfs_poll,
478478
};
479479

480-
481-
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
482-
int type)
480+
int sysfs_add_file_mode(struct sysfs_dirent *dir_sd,
481+
const struct attribute *attr, int type, mode_t amode)
483482
{
484-
umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
483+
umode_t mode = (amode & S_IALLUGO) | S_IFREG;
485484
struct sysfs_addrm_cxt acxt;
486485
struct sysfs_dirent *sd;
487486
int rc;
@@ -502,6 +501,13 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
502501
}
503502

504503

504+
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
505+
int type)
506+
{
507+
return sysfs_add_file_mode(dir_sd, attr, type, attr->mode);
508+
}
509+
510+
505511
/**
506512
* sysfs_create_file - create an attribute file for an object.
507513
* @kobj: object we're creating for.

‎fs/sysfs/group.c‎

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,50 @@ static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
2323
int i;
2424

2525
for (i = 0, attr = grp->attrs; *attr; i++, attr++)
26-
if (!grp->is_visible ||
27-
grp->is_visible(kobj, *attr, i))
28-
sysfs_hash_and_remove(dir_sd, (*attr)->name);
26+
sysfs_hash_and_remove(dir_sd, (*attr)->name);
2927
}
3028

3129
static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
32-
const struct attribute_group *grp)
30+
const struct attribute_group *grp, int update)
3331
{
3432
struct attribute *const* attr;
3533
int error = 0, i;
3634

37-
for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++)
38-
if (!grp->is_visible ||
39-
grp->is_visible(kobj, *attr, i))
40-
error |=
41-
sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
35+
for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
36+
mode_t mode = 0;
37+
38+
/* in update mode, we're changing the permissions or
39+
* visibility. Do this by first removing then
40+
* re-adding (if required) the file */
41+
if (update)
42+
sysfs_hash_and_remove(dir_sd, (*attr)->name);
43+
if (grp->is_visible) {
44+
mode = grp->is_visible(kobj, *attr, i);
45+
if (!mode)
46+
continue;
47+
}
48+
error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
49+
(*attr)->mode | mode);
50+
if (unlikely(error))
51+
break;
52+
}
4253
if (error)
4354
remove_files(dir_sd, kobj, grp);
4455
return error;
4556
}
4657

4758

48-
int sysfs_create_group(struct kobject * kobj,
49-
const struct attribute_group * grp)
59+
static int internal_create_group(struct kobject *kobj, int update,
60+
const struct attribute_group *grp)
5061
{
5162
struct sysfs_dirent *sd;
5263
int error;
5364

54-
BUG_ON(!kobj || !kobj->sd);
65+
BUG_ON(!kobj || (!update && !kobj->sd));
66+
67+
/* Updates may happen before the object has been instantiated */
68+
if (unlikely(update && !kobj->sd))
69+
return -EINVAL;
5570

5671
if (grp->name) {
5772
error = sysfs_create_subdir(kobj, grp->name, &sd);
@@ -60,7 +75,7 @@ int sysfs_create_group(struct kobject * kobj,
6075
} else
6176
sd = kobj->sd;
6277
sysfs_get(sd);
63-
error = create_files(sd, kobj, grp);
78+
error = create_files(sd, kobj, grp, update);
6479
if (error) {
6580
if (grp->name)
6681
sysfs_remove_subdir(sd);
@@ -69,6 +84,47 @@ int sysfs_create_group(struct kobject * kobj,
6984
return error;
7085
}
7186

87+
/**
88+
* sysfs_create_group - given a directory kobject, create an attribute group
89+
* @kobj: The kobject to create the group on
90+
* @grp: The attribute group to create
91+
*
92+
* This function creates a group for the first time. It will explicitly
93+
* warn and error if any of the attribute files being created already exist.
94+
*
95+
* Returns 0 on success or error.
96+
*/
97+
int sysfs_create_group(struct kobject *kobj,
98+
const struct attribute_group *grp)
99+
{
100+
return internal_create_group(kobj, 0, grp);
101+
}
102+
103+
/**
104+
* sysfs_update_group - given a directory kobject, create an attribute group
105+
* @kobj: The kobject to create the group on
106+
* @grp: The attribute group to create
107+
*
108+
* This function updates an attribute group. Unlike
109+
* sysfs_create_group(), it will explicitly not warn or error if any
110+
* of the attribute files being created already exist. Furthermore,
111+
* if the visibility of the files has changed through the is_visible()
112+
* callback, it will update the permissions and add or remove the
113+
* relevant files.
114+
*
115+
* The primary use for this function is to call it after making a change
116+
* that affects group visibility.
117+
*
118+
* Returns 0 on success or error.
119+
*/
120+
int sysfs_update_group(struct kobject *kobj,
121+
const struct attribute_group *grp)
122+
{
123+
return internal_create_group(kobj, 1, grp);
124+
}
125+
126+
127+
72128
void sysfs_remove_group(struct kobject * kobj,
73129
const struct attribute_group * grp)
74130
{
@@ -95,4 +151,5 @@ void sysfs_remove_group(struct kobject * kobj,
95151

96152

97153
EXPORT_SYMBOL_GPL(sysfs_create_group);
154+
EXPORT_SYMBOL_GPL(sysfs_update_group);
98155
EXPORT_SYMBOL_GPL(sysfs_remove_group);

‎fs/sysfs/sysfs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ extern const struct file_operations sysfs_file_operations;
154154
int sysfs_add_file(struct sysfs_dirent *dir_sd,
155155
const struct attribute *attr, int type);
156156

157+
int sysfs_add_file_mode(struct sysfs_dirent *dir_sd,
158+
const struct attribute *attr, int type, mode_t amode);
157159
/*
158160
* bin.c
159161
*/

‎include/linux/sysfs.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct attribute {
3232

3333
struct attribute_group {
3434
const char *name;
35-
int (*is_visible)(struct kobject *,
35+
mode_t (*is_visible)(struct kobject *,
3636
struct attribute *, int);
3737
struct attribute **attrs;
3838
};
@@ -105,6 +105,8 @@ void sysfs_remove_link(struct kobject *kobj, const char *name);
105105

106106
int __must_check sysfs_create_group(struct kobject *kobj,
107107
const struct attribute_group *grp);
108+
int sysfs_update_group(struct kobject *kobj,
109+
const struct attribute_group *grp);
108110
void sysfs_remove_group(struct kobject *kobj,
109111
const struct attribute_group *grp);
110112
int sysfs_add_file_to_group(struct kobject *kobj,

0 commit comments

Comments
 (0)