Blame view

fs/sysfs/group.c 12.3 KB
619daeeeb   Greg Kroah-Hartman   sysfs: use SPDX i...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
   *
   * Copyright (c) 2003 Patrick Mochel
   * Copyright (c) 2003 Open Source Development Lab
9e2a47ed6   Greg Kroah-Hartman   sysfs: group: upd...
7
8
   * Copyright (c) 2013 Greg Kroah-Hartman
   * Copyright (c) 2013 The Linux Foundation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
   */
  
  #include <linux/kobject.h>
  #include <linux/module.h>
  #include <linux/dcache.h>
5f45f1a78   Christoph Hellwig   [PATCH] remove du...
14
  #include <linux/namei.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
  #include <linux/err.h>
  #include "sysfs.h"
9f70a4012   Robert ABEL   sysfs: fix attrib...
17
  static void remove_files(struct kernfs_node *parent,
608e266a2   Tejun Heo   sysfs: make kobj ...
18
  			 const struct attribute_group *grp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  {
995d8ed94   Greg Kroah-Hartman   sysfs: group.c: f...
20
21
  	struct attribute *const *attr;
  	struct bin_attribute *const *bin_attr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22

6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
23
24
  	if (grp->attrs)
  		for (attr = grp->attrs; *attr; attr++)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
25
  			kernfs_remove_by_name(parent, (*attr)->name);
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
26
27
  	if (grp->bin_attrs)
  		for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++)
9f70a4012   Robert ABEL   sysfs: fix attrib...
28
  			kernfs_remove_by_name(parent, (*bin_attr)->attr.name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  }
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
30
  static int create_files(struct kernfs_node *parent, struct kobject *kobj,
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
31
  			kuid_t uid, kgid_t gid,
0f4238958   James Bottomley   [SCSI] sysfs: mak...
32
  			const struct attribute_group *grp, int update)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  {
995d8ed94   Greg Kroah-Hartman   sysfs: group.c: f...
34
35
  	struct attribute *const *attr;
  	struct bin_attribute *const *bin_attr;
d4acd722b   James Bottomley   [SCSI] sysfs: add...
36
  	int error = 0, i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37

6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
38
39
  	if (grp->attrs) {
  		for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
da4759c73   Guenter Roeck   sysfs: Use only r...
40
  			umode_t mode = (*attr)->mode;
0f4238958   James Bottomley   [SCSI] sysfs: mak...
41

6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
42
43
44
45
46
47
  			/*
  			 * In update mode, we're changing the permissions or
  			 * visibility.  Do this by first removing then
  			 * re-adding (if required) the file.
  			 */
  			if (update)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
48
  				kernfs_remove_by_name(parent, (*attr)->name);
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
49
50
51
52
53
  			if (grp->is_visible) {
  				mode = grp->is_visible(kobj, *attr, i);
  				if (!mode)
  					continue;
  			}
d8bf8c92e   Vivien Didelot   sysfs: Only accep...
54
55
56
57
58
59
60
  
  			WARN(mode & ~(SYSFS_PREALLOC | 0664),
  			     "Attribute %s: Invalid permissions 0%o
  ",
  			     (*attr)->name, mode);
  
  			mode &= SYSFS_PREALLOC | 0664;
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
61
  			error = sysfs_add_file_mode_ns(parent, *attr, false,
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
62
  						       mode, uid, gid, NULL);
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
63
64
65
66
  			if (unlikely(error))
  				break;
  		}
  		if (error) {
9f70a4012   Robert ABEL   sysfs: fix attrib...
67
  			remove_files(parent, grp);
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
68
69
70
71
72
  			goto exit;
  		}
  	}
  
  	if (grp->bin_attrs) {
7f5028cf6   Emilio López   sysfs: Support is...
73
74
  		for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
  			umode_t mode = (*bin_attr)->attr.mode;
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
75
  			if (update)
aabaf4c20   Cody P Schafer   sysfs: create bin...
76
77
  				kernfs_remove_by_name(parent,
  						(*bin_attr)->attr.name);
7f5028cf6   Emilio López   sysfs: Support is...
78
79
80
81
82
83
84
85
86
87
88
89
  			if (grp->is_bin_visible) {
  				mode = grp->is_bin_visible(kobj, *bin_attr, i);
  				if (!mode)
  					continue;
  			}
  
  			WARN(mode & ~(SYSFS_PREALLOC | 0664),
  			     "Attribute %s: Invalid permissions 0%o
  ",
  			     (*bin_attr)->attr.name, mode);
  
  			mode &= SYSFS_PREALLOC | 0664;
aabaf4c20   Cody P Schafer   sysfs: create bin...
90
91
  			error = sysfs_add_file_mode_ns(parent,
  					&(*bin_attr)->attr, true,
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
92
93
  					mode,
  					uid, gid, NULL);
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
94
95
  			if (error)
  				break;
0f4238958   James Bottomley   [SCSI] sysfs: mak...
96
  		}
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
97
  		if (error)
9f70a4012   Robert ABEL   sysfs: fix attrib...
98
  			remove_files(parent, grp);
0f4238958   James Bottomley   [SCSI] sysfs: mak...
99
  	}
6ab9cea16   Greg Kroah-Hartman   sysfs: add suppor...
100
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  	return error;
  }
0f4238958   James Bottomley   [SCSI] sysfs: mak...
103
104
  static int internal_create_group(struct kobject *kobj, int update,
  				 const struct attribute_group *grp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
106
  	struct kernfs_node *kn;
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
107
108
  	kuid_t uid;
  	kgid_t gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  	int error;
de96e9fea   Greg Kroah-Hartman   sysfs: convert BU...
110
111
  	if (WARN_ON(!kobj || (!update && !kobj->sd)))
  		return -EINVAL;
0f4238958   James Bottomley   [SCSI] sysfs: mak...
112
113
114
115
  
  	/* Updates may happen before the object has been instantiated */
  	if (unlikely(update && !kobj->sd))
  		return -EINVAL;
388a8c353   Oliver Schinagl   sysfs: prevent wa...
116
117
118
  	if (!grp->attrs && !grp->bin_attrs) {
  		WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s
  ",
adf305f77   Javi Merino   sysfs: fix warnin...
119
  			kobj->name, grp->name ?: "");
5631f2c18   Bruno Prémont   sysfs: Prevent cr...
120
121
  		return -EINVAL;
  	}
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
122
  	kobject_get_ownership(kobj, &uid, &gid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  	if (grp->name) {
c855cf275   Rajat Jain   sysfs: Fix intern...
124
125
126
127
128
129
130
131
132
  		if (update) {
  			kn = kernfs_find_and_get(kobj->sd, grp->name);
  			if (!kn) {
  				pr_warn("Can't update unknown attr grp name: %s/%s
  ",
  					kobj->name, grp->name);
  				return -EINVAL;
  			}
  		} else {
a18d783fe   Linus Torvalds   Merge tag 'driver...
133
134
135
  			kn = kernfs_create_dir_ns(kobj->sd, grp->name,
  						  S_IRWXU | S_IRUGO | S_IXUGO,
  						  uid, gid, kobj, NULL);
c855cf275   Rajat Jain   sysfs: Fix intern...
136
137
138
139
140
  			if (IS_ERR(kn)) {
  				if (PTR_ERR(kn) == -EEXIST)
  					sysfs_warn_dup(kobj->sd, grp->name);
  				return PTR_ERR(kn);
  			}
93b2b8e4a   Tejun Heo   sysfs, kernfs: in...
141
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  	} else
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
143
144
  		kn = kobj->sd;
  	kernfs_get(kn);
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
145
  	error = create_files(kn, kobj, uid, gid, grp, update);
608e266a2   Tejun Heo   sysfs: make kobj ...
146
  	if (error) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
  		if (grp->name)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
148
  			kernfs_remove(kn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  	}
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
150
  	kernfs_put(kn);
c855cf275   Rajat Jain   sysfs: Fix intern...
151
152
153
  
  	if (grp->name && update)
  		kernfs_put(kn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
  	return error;
  }
0f4238958   James Bottomley   [SCSI] sysfs: mak...
156
157
158
159
160
161
162
163
  /**
   * sysfs_create_group - given a directory kobject, create an attribute group
   * @kobj:	The kobject to create the group on
   * @grp:	The attribute group to create
   *
   * This function creates a group for the first time.  It will explicitly
   * warn and error if any of the attribute files being created already exist.
   *
ed1dc8a89   Antonio Ospite   sysfs: disambigua...
164
   * Returns 0 on success or error code on failure.
0f4238958   James Bottomley   [SCSI] sysfs: mak...
165
166
167
168
169
170
   */
  int sysfs_create_group(struct kobject *kobj,
  		       const struct attribute_group *grp)
  {
  	return internal_create_group(kobj, 0, grp);
  }
d363bc53e   Greg Kroah-Hartman   sysfs: group.c: m...
171
  EXPORT_SYMBOL_GPL(sysfs_create_group);
0f4238958   James Bottomley   [SCSI] sysfs: mak...
172

aac1f7f95   Jiri Olsa   sysfs: Add sysfs_...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  static int internal_create_groups(struct kobject *kobj, int update,
  				  const struct attribute_group **groups)
  {
  	int error = 0;
  	int i;
  
  	if (!groups)
  		return 0;
  
  	for (i = 0; groups[i]; i++) {
  		error = internal_create_group(kobj, update, groups[i]);
  		if (error) {
  			while (--i >= 0)
  				sysfs_remove_group(kobj, groups[i]);
  			break;
  		}
  	}
  	return error;
  }
0f4238958   James Bottomley   [SCSI] sysfs: mak...
192
  /**
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
193
194
195
196
197
198
199
200
201
202
   * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
   * @kobj:	The kobject to create the group on
   * @groups:	The attribute groups to create, NULL terminated
   *
   * This function creates a bunch of attribute groups.  If an error occurs when
   * creating a group, all previously created groups will be removed, unwinding
   * everything back to the original state when this function was called.
   * It will explicitly warn and error if any of the attribute files being
   * created already exist.
   *
ed1dc8a89   Antonio Ospite   sysfs: disambigua...
203
   * Returns 0 on success or error code from sysfs_create_group on failure.
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
204
205
206
207
   */
  int sysfs_create_groups(struct kobject *kobj,
  			const struct attribute_group **groups)
  {
aac1f7f95   Jiri Olsa   sysfs: Add sysfs_...
208
  	return internal_create_groups(kobj, 0, groups);
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
209
210
211
212
  }
  EXPORT_SYMBOL_GPL(sysfs_create_groups);
  
  /**
aac1f7f95   Jiri Olsa   sysfs: Add sysfs_...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
   * sysfs_update_groups - given a directory kobject, create a bunch of attribute groups
   * @kobj:	The kobject to update the group on
   * @groups:	The attribute groups to update, NULL terminated
   *
   * This function update a bunch of attribute groups.  If an error occurs when
   * updating a group, all previously updated groups will be removed together
   * with already existing (not updated) attributes.
   *
   * Returns 0 on success or error code from sysfs_update_group on failure.
   */
  int sysfs_update_groups(struct kobject *kobj,
  			const struct attribute_group **groups)
  {
  	return internal_create_groups(kobj, 1, groups);
  }
  EXPORT_SYMBOL_GPL(sysfs_update_groups);
  
  /**
1f8e1cdac   Robert P. J. Day   SYSFS: Fix errone...
231
232
233
   * sysfs_update_group - given a directory kobject, update an attribute group
   * @kobj:	The kobject to update the group on
   * @grp:	The attribute group to update
0f4238958   James Bottomley   [SCSI] sysfs: mak...
234
235
236
237
238
239
   *
   * This function updates an attribute group.  Unlike
   * sysfs_create_group(), it will explicitly not warn or error if any
   * of the attribute files being created already exist.  Furthermore,
   * if the visibility of the files has changed through the is_visible()
   * callback, it will update the permissions and add or remove the
c855cf275   Rajat Jain   sysfs: Fix intern...
240
241
   * relevant files. Changing a group's name (subdirectory name under
   * kobj's directory in sysfs) is not allowed.
0f4238958   James Bottomley   [SCSI] sysfs: mak...
242
243
244
245
   *
   * The primary use for this function is to call it after making a change
   * that affects group visibility.
   *
ed1dc8a89   Antonio Ospite   sysfs: disambigua...
246
   * Returns 0 on success or error code on failure.
0f4238958   James Bottomley   [SCSI] sysfs: mak...
247
248
249
250
251
252
   */
  int sysfs_update_group(struct kobject *kobj,
  		       const struct attribute_group *grp)
  {
  	return internal_create_group(kobj, 1, grp);
  }
d363bc53e   Greg Kroah-Hartman   sysfs: group.c: m...
253
  EXPORT_SYMBOL_GPL(sysfs_update_group);
0f4238958   James Bottomley   [SCSI] sysfs: mak...
254

f9ae443b5   Greg Kroah-Hartman   sysfs: group.c: a...
255
256
257
258
259
260
261
262
  /**
   * sysfs_remove_group: remove a group from a kobject
   * @kobj:	kobject to remove the group from
   * @grp:	group to remove
   *
   * This function removes a group of attributes from a kobject.  The attributes
   * previously have to have been created for this group, otherwise it will fail.
   */
995d8ed94   Greg Kroah-Hartman   sysfs: group.c: f...
263
264
  void sysfs_remove_group(struct kobject *kobj,
  			const struct attribute_group *grp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
266
267
  	struct kernfs_node *parent = kobj->sd;
  	struct kernfs_node *kn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268

057f6c019   James Morris   security: prevent...
269
  	if (grp->name) {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
270
271
272
  		kn = kernfs_find_and_get(parent, grp->name);
  		if (!kn) {
  			WARN(!kn, KERN_WARNING
78618d395   Johannes Thumshirn   sysfs print name ...
273
274
275
  			     "sysfs group '%s' not found for kobject '%s'
  ",
  			     grp->name, kobject_name(kobj));
969affd27   Greg Kroah-Hartman   sysfs: remove BUG...
276
277
  			return;
  		}
ccf73cf33   Tejun Heo   sysfs, kernfs: in...
278
  	} else {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
279
280
  		kn = parent;
  		kernfs_get(kn);
ccf73cf33   Tejun Heo   sysfs, kernfs: in...
281
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

9f70a4012   Robert ABEL   sysfs: fix attrib...
283
  	remove_files(kn, grp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  	if (grp->name)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
285
  		kernfs_remove(kn);
608e266a2   Tejun Heo   sysfs: make kobj ...
286

324a56e16   Tejun Heo   kernfs: s/sysfs_d...
287
  	kernfs_put(kn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  }
d363bc53e   Greg Kroah-Hartman   sysfs: group.c: m...
289
  EXPORT_SYMBOL_GPL(sysfs_remove_group);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290

69d44ffbd   Alan Stern   sysfs: Add sysfs_...
291
  /**
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
292
293
   * sysfs_remove_groups - remove a list of groups
   *
f9ae443b5   Greg Kroah-Hartman   sysfs: group.c: a...
294
295
   * @kobj:	The kobject for the groups to be removed from
   * @groups:	NULL terminated list of groups to be removed
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
296
   *
09239ed4a   Greg Kroah-Hartman   sysfs: group.c: f...
297
   * If groups is not NULL, remove the specified groups from the kobject.
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
   */
  void sysfs_remove_groups(struct kobject *kobj,
  			 const struct attribute_group **groups)
  {
  	int i;
  
  	if (!groups)
  		return;
  	for (i = 0; groups[i]; i++)
  		sysfs_remove_group(kobj, groups[i]);
  }
  EXPORT_SYMBOL_GPL(sysfs_remove_groups);
  
  /**
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
312
313
314
315
316
317
318
319
320
321
322
   * sysfs_merge_group - merge files into a pre-existing attribute group.
   * @kobj:	The kobject containing the group.
   * @grp:	The files to create and the attribute group they belong to.
   *
   * This function returns an error if the group doesn't exist or any of the
   * files already exist in that group, in which case none of the new files
   * are created.
   */
  int sysfs_merge_group(struct kobject *kobj,
  		       const struct attribute_group *grp)
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
323
  	struct kernfs_node *parent;
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
324
325
  	kuid_t uid;
  	kgid_t gid;
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
326
327
328
  	int error = 0;
  	struct attribute *const *attr;
  	int i;
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
329
330
  	parent = kernfs_find_and_get(kobj->sd, grp->name);
  	if (!parent)
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
331
  		return -ENOENT;
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
332
  	kobject_get_ownership(kobj, &uid, &gid);
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
333
  	for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
5f81880d5   Dmitry Torokhov   sysfs, kobject: a...
334
335
  		error = sysfs_add_file_mode_ns(parent, *attr, false,
  					       (*attr)->mode, uid, gid, NULL);
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
336
337
  	if (error) {
  		while (--i >= 0)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
338
  			kernfs_remove_by_name(parent, (*--attr)->name);
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
339
  	}
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
340
  	kernfs_put(parent);
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
341
342
343
344
345
346
347
348
349
350
351
352
353
  
  	return error;
  }
  EXPORT_SYMBOL_GPL(sysfs_merge_group);
  
  /**
   * sysfs_unmerge_group - remove files from a pre-existing attribute group.
   * @kobj:	The kobject containing the group.
   * @grp:	The files to remove and the attribute group they belong to.
   */
  void sysfs_unmerge_group(struct kobject *kobj,
  		       const struct attribute_group *grp)
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
354
  	struct kernfs_node *parent;
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
355
  	struct attribute *const *attr;
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
356
357
  	parent = kernfs_find_and_get(kobj->sd, grp->name);
  	if (parent) {
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
358
  		for (attr = grp->attrs; *attr; ++attr)
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
359
360
  			kernfs_remove_by_name(parent, (*attr)->name);
  		kernfs_put(parent);
69d44ffbd   Alan Stern   sysfs: Add sysfs_...
361
362
363
  	}
  }
  EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
364
365
366
367
368
369
370
371
372
373
  /**
   * sysfs_add_link_to_group - add a symlink to an attribute group.
   * @kobj:	The kobject containing the group.
   * @group_name:	The name of the group.
   * @target:	The target kobject of the symlink to create.
   * @link_name:	The name of the symlink to create.
   */
  int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  			    struct kobject *target, const char *link_name)
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
374
  	struct kernfs_node *parent;
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
375
  	int error = 0;
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
376
377
  	parent = kernfs_find_and_get(kobj->sd, group_name);
  	if (!parent)
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
378
  		return -ENOENT;
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
379
380
  	error = sysfs_create_link_sd(parent, target, link_name);
  	kernfs_put(parent);
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  
  	return error;
  }
  EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
  
  /**
   * sysfs_remove_link_from_group - remove a symlink from an attribute group.
   * @kobj:	The kobject containing the group.
   * @group_name:	The name of the group.
   * @link_name:	The name of the symlink to remove.
   */
  void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  				  const char *link_name)
  {
324a56e16   Tejun Heo   kernfs: s/sysfs_d...
395
  	struct kernfs_node *parent;
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
396

324a56e16   Tejun Heo   kernfs: s/sysfs_d...
397
398
399
400
  	parent = kernfs_find_and_get(kobj->sd, group_name);
  	if (parent) {
  		kernfs_remove_by_name(parent, link_name);
  		kernfs_put(parent);
0bb8f3d6a   Rafael J. Wysocki   sysfs: Functions ...
401
402
403
  	}
  }
  EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
37c1c04cc   Jarkko Sakkinen   sysfs: added __co...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  
  /**
   * __compat_only_sysfs_link_entry_to_kobj - add a symlink to a kobject pointing
   * to a group or an attribute
   * @kobj:		The kobject containing the group.
   * @target_kobj:	The target kobject.
   * @target_name:	The name of the target group or attribute.
   */
  int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
  				      struct kobject *target_kobj,
  				      const char *target_name)
  {
  	struct kernfs_node *target;
  	struct kernfs_node *entry;
  	struct kernfs_node *link;
  
  	/*
  	 * We don't own @target_kobj and it may be removed at any time.
  	 * Synchronize using sysfs_symlink_target_lock. See sysfs_remove_dir()
  	 * for details.
  	 */
  	spin_lock(&sysfs_symlink_target_lock);
  	target = target_kobj->sd;
  	if (target)
  		kernfs_get(target);
  	spin_unlock(&sysfs_symlink_target_lock);
  	if (!target)
  		return -ENOENT;
  
  	entry = kernfs_find_and_get(target_kobj->sd, target_name);
  	if (!entry) {
  		kernfs_put(target);
  		return -ENOENT;
  	}
  
  	link = kernfs_create_link(kobj->sd, target_name, entry);
  	if (IS_ERR(link) && PTR_ERR(link) == -EEXIST)
  		sysfs_warn_dup(kobj->sd, target_name);
  
  	kernfs_put(entry);
  	kernfs_put(target);
90b3d2f6c   Vasyl Gomonovych   sysfs: Use PTR_ER...
445
  	return PTR_ERR_OR_ZERO(link);
37c1c04cc   Jarkko Sakkinen   sysfs: added __co...
446
447
  }
  EXPORT_SYMBOL_GPL(__compat_only_sysfs_link_entry_to_kobj);