xref: /openbmc/linux/fs/sysfs/group.c (revision 643d1f7f)
1 /*
2  * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released undert the GPL v2.
8  *
9  */
10 
11 #include <linux/kobject.h>
12 #include <linux/module.h>
13 #include <linux/dcache.h>
14 #include <linux/namei.h>
15 #include <linux/err.h>
16 #include "sysfs.h"
17 
18 
19 static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
20 			 const struct attribute_group *grp)
21 {
22 	struct attribute *const* attr;
23 	int i;
24 
25 	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);
29 }
30 
31 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
32 			const struct attribute_group *grp)
33 {
34 	struct attribute *const* attr;
35 	int error = 0, i;
36 
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);
42 	if (error)
43 		remove_files(dir_sd, kobj, grp);
44 	return error;
45 }
46 
47 
48 int sysfs_create_group(struct kobject * kobj,
49 		       const struct attribute_group * grp)
50 {
51 	struct sysfs_dirent *sd;
52 	int error;
53 
54 	BUG_ON(!kobj || !kobj->sd);
55 
56 	if (grp->name) {
57 		error = sysfs_create_subdir(kobj, grp->name, &sd);
58 		if (error)
59 			return error;
60 	} else
61 		sd = kobj->sd;
62 	sysfs_get(sd);
63 	error = create_files(sd, kobj, grp);
64 	if (error) {
65 		if (grp->name)
66 			sysfs_remove_subdir(sd);
67 	}
68 	sysfs_put(sd);
69 	return error;
70 }
71 
72 void sysfs_remove_group(struct kobject * kobj,
73 			const struct attribute_group * grp)
74 {
75 	struct sysfs_dirent *dir_sd = kobj->sd;
76 	struct sysfs_dirent *sd;
77 
78 	if (grp->name) {
79 		sd = sysfs_get_dirent(dir_sd, grp->name);
80 		BUG_ON(!sd);
81 	} else
82 		sd = sysfs_get(dir_sd);
83 
84 	remove_files(sd, kobj, grp);
85 	if (grp->name)
86 		sysfs_remove_subdir(sd);
87 
88 	sysfs_put(sd);
89 }
90 
91 
92 EXPORT_SYMBOL_GPL(sysfs_create_group);
93 EXPORT_SYMBOL_GPL(sysfs_remove_group);
94