Skip to content

Commit 0bb8f3d

Browse files
committed
sysfs: Functions for adding/removing symlinks to/from attribute groups
The most convenient way to expose ACPI power resources lists of a device is to put symbolic links to sysfs directories representing those resources into special attribute groups in the device's sysfs directory. For this purpose, it is necessary to be able to add symbolic links to attribute groups. For this reason, add sysfs helper functions for adding/removing symbolic links to/from attribute groups, sysfs_add_link_to_group() and sysfs_remove_link_from_group(), respectively. This change set includes a build fix from David Rientjes. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b1c0f99 commit 0bb8f3d

4 files changed

Lines changed: 92 additions & 13 deletions

File tree

‎fs/sysfs/group.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,48 @@ void sysfs_unmerge_group(struct kobject *kobj,
205205
}
206206
EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
207207

208+
/**
209+
* sysfs_add_link_to_group - add a symlink to an attribute group.
210+
* @kobj: The kobject containing the group.
211+
* @group_name: The name of the group.
212+
* @target: The target kobject of the symlink to create.
213+
* @link_name: The name of the symlink to create.
214+
*/
215+
int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
216+
struct kobject *target, const char *link_name)
217+
{
218+
struct sysfs_dirent *dir_sd;
219+
int error = 0;
220+
221+
dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
222+
if (!dir_sd)
223+
return -ENOENT;
224+
225+
error = sysfs_create_link_sd(dir_sd, target, link_name);
226+
sysfs_put(dir_sd);
227+
228+
return error;
229+
}
230+
EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
231+
232+
/**
233+
* sysfs_remove_link_from_group - remove a symlink from an attribute group.
234+
* @kobj: The kobject containing the group.
235+
* @group_name: The name of the group.
236+
* @link_name: The name of the symlink to remove.
237+
*/
238+
void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
239+
const char *link_name)
240+
{
241+
struct sysfs_dirent *dir_sd;
242+
243+
dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
244+
if (dir_sd) {
245+
sysfs_hash_and_remove(dir_sd, NULL, link_name);
246+
sysfs_put(dir_sd);
247+
}
248+
}
249+
EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
208250

209251
EXPORT_SYMBOL_GPL(sysfs_create_group);
210252
EXPORT_SYMBOL_GPL(sysfs_update_group);

‎fs/sysfs/symlink.c‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,17 @@
2121

2222
#include "sysfs.h"
2323

24-
static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
25-
const char *name, int warn)
24+
static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
25+
struct kobject *target,
26+
const char *name, int warn)
2627
{
27-
struct sysfs_dirent *parent_sd = NULL;
2828
struct sysfs_dirent *target_sd = NULL;
2929
struct sysfs_dirent *sd = NULL;
3030
struct sysfs_addrm_cxt acxt;
3131
enum kobj_ns_type ns_type;
3232
int error;
3333

34-
BUG_ON(!name);
35-
36-
if (!kobj)
37-
parent_sd = &sysfs_root;
38-
else
39-
parent_sd = kobj->sd;
40-
41-
error = -EFAULT;
42-
if (!parent_sd)
43-
goto out_put;
34+
BUG_ON(!name || !parent_sd);
4435

4536
/* target->sd can go away beneath us but is protected with
4637
* sysfs_assoc_lock. Fetch target_sd from it.
@@ -95,6 +86,34 @@ static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
9586
return error;
9687
}
9788

89+
/**
90+
* sysfs_create_link_sd - create symlink to a given object.
91+
* @sd: directory we're creating the link in.
92+
* @target: object we're pointing to.
93+
* @name: name of the symlink.
94+
*/
95+
int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
96+
const char *name)
97+
{
98+
return sysfs_do_create_link_sd(sd, target, name, 1);
99+
}
100+
101+
static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
102+
const char *name, int warn)
103+
{
104+
struct sysfs_dirent *parent_sd = NULL;
105+
106+
if (!kobj)
107+
parent_sd = &sysfs_root;
108+
else
109+
parent_sd = kobj->sd;
110+
111+
if (!parent_sd)
112+
return -EFAULT;
113+
114+
return sysfs_do_create_link_sd(parent_sd, target, name, warn);
115+
}
116+
98117
/**
99118
* sysfs_create_link - create symlink between two objects.
100119
* @kobj: object whose directory we're creating the link in.

‎fs/sysfs/sysfs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,5 @@ void unmap_bin_file(struct sysfs_dirent *attr_sd);
240240
* symlink.c
241241
*/
242242
extern const struct inode_operations sysfs_symlink_inode_operations;
243+
int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
244+
const char *name);

‎include/linux/sysfs.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ int sysfs_merge_group(struct kobject *kobj,
181181
const struct attribute_group *grp);
182182
void sysfs_unmerge_group(struct kobject *kobj,
183183
const struct attribute_group *grp);
184+
int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
185+
struct kobject *target, const char *link_name);
186+
void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
187+
const char *link_name);
184188

185189
void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
186190
void sysfs_notify_dirent(struct sysfs_dirent *sd);
@@ -326,6 +330,18 @@ static inline void sysfs_unmerge_group(struct kobject *kobj,
326330
{
327331
}
328332

333+
static inline int sysfs_add_link_to_group(struct kobject *kobj,
334+
const char *group_name, struct kobject *target,
335+
const char *link_name)
336+
{
337+
return 0;
338+
}
339+
340+
static inline void sysfs_remove_link_from_group(struct kobject *kobj,
341+
const char *group_name, const char *link_name)
342+
{
343+
}
344+
329345
static inline void sysfs_notify(struct kobject *kobj, const char *dir,
330346
const char *attr)
331347
{

0 commit comments

Comments
 (0)