group.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * Copyright (c) 2013 Greg Kroah-Hartman
  7. * Copyright (c) 2013 The Linux Foundation
  8. *
  9. * This file is released undert the GPL v2.
  10. *
  11. */
  12. #include <linux/kobject.h>
  13. #include <linux/module.h>
  14. #include <linux/dcache.h>
  15. #include <linux/namei.h>
  16. #include <linux/err.h>
  17. #include "sysfs.h"
  18. static void remove_files(struct kernfs_node *parent, struct kobject *kobj,
  19. const struct attribute_group *grp)
  20. {
  21. struct attribute *const *attr;
  22. struct bin_attribute *const *bin_attr;
  23. if (grp->attrs)
  24. for (attr = grp->attrs; *attr; attr++)
  25. kernfs_remove_by_name(parent, (*attr)->name);
  26. if (grp->bin_attrs)
  27. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++)
  28. sysfs_remove_bin_file(kobj, *bin_attr);
  29. }
  30. static int create_files(struct kernfs_node *parent, struct kobject *kobj,
  31. const struct attribute_group *grp, int update)
  32. {
  33. struct attribute *const *attr;
  34. struct bin_attribute *const *bin_attr;
  35. int error = 0, i;
  36. if (grp->attrs) {
  37. for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
  38. umode_t mode = 0;
  39. /*
  40. * In update mode, we're changing the permissions or
  41. * visibility. Do this by first removing then
  42. * re-adding (if required) the file.
  43. */
  44. if (update)
  45. kernfs_remove_by_name(parent, (*attr)->name);
  46. if (grp->is_visible) {
  47. mode = grp->is_visible(kobj, *attr, i);
  48. if (!mode)
  49. continue;
  50. }
  51. error = sysfs_add_file_mode_ns(parent, *attr, false,
  52. (*attr)->mode | mode,
  53. NULL);
  54. if (unlikely(error))
  55. break;
  56. }
  57. if (error) {
  58. remove_files(parent, kobj, grp);
  59. goto exit;
  60. }
  61. }
  62. if (grp->bin_attrs) {
  63. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) {
  64. if (update)
  65. sysfs_remove_bin_file(kobj, *bin_attr);
  66. error = sysfs_create_bin_file(kobj, *bin_attr);
  67. if (error)
  68. break;
  69. }
  70. if (error)
  71. remove_files(parent, kobj, grp);
  72. }
  73. exit:
  74. return error;
  75. }
  76. static int internal_create_group(struct kobject *kobj, int update,
  77. const struct attribute_group *grp)
  78. {
  79. struct kernfs_node *kn;
  80. int error;
  81. BUG_ON(!kobj || (!update && !kobj->sd));
  82. /* Updates may happen before the object has been instantiated */
  83. if (unlikely(update && !kobj->sd))
  84. return -EINVAL;
  85. if (!grp->attrs && !grp->bin_attrs) {
  86. WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n",
  87. kobj->name, grp->name ? "" : grp->name);
  88. return -EINVAL;
  89. }
  90. if (grp->name) {
  91. kn = kernfs_create_dir(kobj->sd, grp->name, kobj);
  92. if (IS_ERR(kn)) {
  93. if (PTR_ERR(kn) == -EEXIST)
  94. sysfs_warn_dup(kobj->sd, grp->name);
  95. return PTR_ERR(kn);
  96. }
  97. } else
  98. kn = kobj->sd;
  99. kernfs_get(kn);
  100. error = create_files(kn, kobj, grp, update);
  101. if (error) {
  102. if (grp->name)
  103. kernfs_remove(kn);
  104. }
  105. kernfs_put(kn);
  106. return error;
  107. }
  108. /**
  109. * sysfs_create_group - given a directory kobject, create an attribute group
  110. * @kobj: The kobject to create the group on
  111. * @grp: The attribute group to create
  112. *
  113. * This function creates a group for the first time. It will explicitly
  114. * warn and error if any of the attribute files being created already exist.
  115. *
  116. * Returns 0 on success or error.
  117. */
  118. int sysfs_create_group(struct kobject *kobj,
  119. const struct attribute_group *grp)
  120. {
  121. return internal_create_group(kobj, 0, grp);
  122. }
  123. EXPORT_SYMBOL_GPL(sysfs_create_group);
  124. /**
  125. * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
  126. * @kobj: The kobject to create the group on
  127. * @groups: The attribute groups to create, NULL terminated
  128. *
  129. * This function creates a bunch of attribute groups. If an error occurs when
  130. * creating a group, all previously created groups will be removed, unwinding
  131. * everything back to the original state when this function was called.
  132. * It will explicitly warn and error if any of the attribute files being
  133. * created already exist.
  134. *
  135. * Returns 0 on success or error code from sysfs_create_group on error.
  136. */
  137. int sysfs_create_groups(struct kobject *kobj,
  138. const struct attribute_group **groups)
  139. {
  140. int error = 0;
  141. int i;
  142. if (!groups)
  143. return 0;
  144. for (i = 0; groups[i]; i++) {
  145. error = sysfs_create_group(kobj, groups[i]);
  146. if (error) {
  147. while (--i >= 0)
  148. sysfs_remove_group(kobj, groups[i]);
  149. break;
  150. }
  151. }
  152. return error;
  153. }
  154. EXPORT_SYMBOL_GPL(sysfs_create_groups);
  155. /**
  156. * sysfs_update_group - given a directory kobject, update an attribute group
  157. * @kobj: The kobject to update the group on
  158. * @grp: The attribute group to update
  159. *
  160. * This function updates an attribute group. Unlike
  161. * sysfs_create_group(), it will explicitly not warn or error if any
  162. * of the attribute files being created already exist. Furthermore,
  163. * if the visibility of the files has changed through the is_visible()
  164. * callback, it will update the permissions and add or remove the
  165. * relevant files.
  166. *
  167. * The primary use for this function is to call it after making a change
  168. * that affects group visibility.
  169. *
  170. * Returns 0 on success or error.
  171. */
  172. int sysfs_update_group(struct kobject *kobj,
  173. const struct attribute_group *grp)
  174. {
  175. return internal_create_group(kobj, 1, grp);
  176. }
  177. EXPORT_SYMBOL_GPL(sysfs_update_group);
  178. /**
  179. * sysfs_remove_group: remove a group from a kobject
  180. * @kobj: kobject to remove the group from
  181. * @grp: group to remove
  182. *
  183. * This function removes a group of attributes from a kobject. The attributes
  184. * previously have to have been created for this group, otherwise it will fail.
  185. */
  186. void sysfs_remove_group(struct kobject *kobj,
  187. const struct attribute_group *grp)
  188. {
  189. struct kernfs_node *parent = kobj->sd;
  190. struct kernfs_node *kn;
  191. if (grp->name) {
  192. kn = kernfs_find_and_get(parent, grp->name);
  193. if (!kn) {
  194. WARN(!kn, KERN_WARNING
  195. "sysfs group %p not found for kobject '%s'\n",
  196. grp, kobject_name(kobj));
  197. return;
  198. }
  199. } else {
  200. kn = parent;
  201. kernfs_get(kn);
  202. }
  203. remove_files(kn, kobj, grp);
  204. if (grp->name)
  205. kernfs_remove(kn);
  206. kernfs_put(kn);
  207. }
  208. EXPORT_SYMBOL_GPL(sysfs_remove_group);
  209. /**
  210. * sysfs_remove_groups - remove a list of groups
  211. *
  212. * @kobj: The kobject for the groups to be removed from
  213. * @groups: NULL terminated list of groups to be removed
  214. *
  215. * If groups is not NULL, remove the specified groups from the kobject.
  216. */
  217. void sysfs_remove_groups(struct kobject *kobj,
  218. const struct attribute_group **groups)
  219. {
  220. int i;
  221. if (!groups)
  222. return;
  223. for (i = 0; groups[i]; i++)
  224. sysfs_remove_group(kobj, groups[i]);
  225. }
  226. EXPORT_SYMBOL_GPL(sysfs_remove_groups);
  227. /**
  228. * sysfs_merge_group - merge files into a pre-existing attribute group.
  229. * @kobj: The kobject containing the group.
  230. * @grp: The files to create and the attribute group they belong to.
  231. *
  232. * This function returns an error if the group doesn't exist or any of the
  233. * files already exist in that group, in which case none of the new files
  234. * are created.
  235. */
  236. int sysfs_merge_group(struct kobject *kobj,
  237. const struct attribute_group *grp)
  238. {
  239. struct kernfs_node *parent;
  240. int error = 0;
  241. struct attribute *const *attr;
  242. int i;
  243. parent = kernfs_find_and_get(kobj->sd, grp->name);
  244. if (!parent)
  245. return -ENOENT;
  246. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  247. error = sysfs_add_file(parent, *attr, false);
  248. if (error) {
  249. while (--i >= 0)
  250. kernfs_remove_by_name(parent, (*--attr)->name);
  251. }
  252. kernfs_put(parent);
  253. return error;
  254. }
  255. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  256. /**
  257. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  258. * @kobj: The kobject containing the group.
  259. * @grp: The files to remove and the attribute group they belong to.
  260. */
  261. void sysfs_unmerge_group(struct kobject *kobj,
  262. const struct attribute_group *grp)
  263. {
  264. struct kernfs_node *parent;
  265. struct attribute *const *attr;
  266. parent = kernfs_find_and_get(kobj->sd, grp->name);
  267. if (parent) {
  268. for (attr = grp->attrs; *attr; ++attr)
  269. kernfs_remove_by_name(parent, (*attr)->name);
  270. kernfs_put(parent);
  271. }
  272. }
  273. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  274. /**
  275. * sysfs_add_link_to_group - add a symlink to an attribute group.
  276. * @kobj: The kobject containing the group.
  277. * @group_name: The name of the group.
  278. * @target: The target kobject of the symlink to create.
  279. * @link_name: The name of the symlink to create.
  280. */
  281. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  282. struct kobject *target, const char *link_name)
  283. {
  284. struct kernfs_node *parent;
  285. int error = 0;
  286. parent = kernfs_find_and_get(kobj->sd, group_name);
  287. if (!parent)
  288. return -ENOENT;
  289. error = sysfs_create_link_sd(parent, target, link_name);
  290. kernfs_put(parent);
  291. return error;
  292. }
  293. EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
  294. /**
  295. * sysfs_remove_link_from_group - remove a symlink from an attribute group.
  296. * @kobj: The kobject containing the group.
  297. * @group_name: The name of the group.
  298. * @link_name: The name of the symlink to remove.
  299. */
  300. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  301. const char *link_name)
  302. {
  303. struct kernfs_node *parent;
  304. parent = kernfs_find_and_get(kobj->sd, group_name);
  305. if (parent) {
  306. kernfs_remove_by_name(parent, link_name);
  307. kernfs_put(parent);
  308. }
  309. }
  310. EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);