group.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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,
  92. S_IRWXU | S_IRUGO | S_IXUGO, kobj);
  93. if (IS_ERR(kn)) {
  94. if (PTR_ERR(kn) == -EEXIST)
  95. sysfs_warn_dup(kobj->sd, grp->name);
  96. return PTR_ERR(kn);
  97. }
  98. } else
  99. kn = kobj->sd;
  100. kernfs_get(kn);
  101. error = create_files(kn, kobj, grp, update);
  102. if (error) {
  103. if (grp->name)
  104. kernfs_remove(kn);
  105. }
  106. kernfs_put(kn);
  107. return error;
  108. }
  109. /**
  110. * sysfs_create_group - given a directory kobject, create an attribute group
  111. * @kobj: The kobject to create the group on
  112. * @grp: The attribute group to create
  113. *
  114. * This function creates a group for the first time. It will explicitly
  115. * warn and error if any of the attribute files being created already exist.
  116. *
  117. * Returns 0 on success or error.
  118. */
  119. int sysfs_create_group(struct kobject *kobj,
  120. const struct attribute_group *grp)
  121. {
  122. return internal_create_group(kobj, 0, grp);
  123. }
  124. EXPORT_SYMBOL_GPL(sysfs_create_group);
  125. /**
  126. * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
  127. * @kobj: The kobject to create the group on
  128. * @groups: The attribute groups to create, NULL terminated
  129. *
  130. * This function creates a bunch of attribute groups. If an error occurs when
  131. * creating a group, all previously created groups will be removed, unwinding
  132. * everything back to the original state when this function was called.
  133. * It will explicitly warn and error if any of the attribute files being
  134. * created already exist.
  135. *
  136. * Returns 0 on success or error code from sysfs_create_group on error.
  137. */
  138. int sysfs_create_groups(struct kobject *kobj,
  139. const struct attribute_group **groups)
  140. {
  141. int error = 0;
  142. int i;
  143. if (!groups)
  144. return 0;
  145. for (i = 0; groups[i]; i++) {
  146. error = sysfs_create_group(kobj, groups[i]);
  147. if (error) {
  148. while (--i >= 0)
  149. sysfs_remove_group(kobj, groups[i]);
  150. break;
  151. }
  152. }
  153. return error;
  154. }
  155. EXPORT_SYMBOL_GPL(sysfs_create_groups);
  156. /**
  157. * sysfs_update_group - given a directory kobject, update an attribute group
  158. * @kobj: The kobject to update the group on
  159. * @grp: The attribute group to update
  160. *
  161. * This function updates an attribute group. Unlike
  162. * sysfs_create_group(), it will explicitly not warn or error if any
  163. * of the attribute files being created already exist. Furthermore,
  164. * if the visibility of the files has changed through the is_visible()
  165. * callback, it will update the permissions and add or remove the
  166. * relevant files.
  167. *
  168. * The primary use for this function is to call it after making a change
  169. * that affects group visibility.
  170. *
  171. * Returns 0 on success or error.
  172. */
  173. int sysfs_update_group(struct kobject *kobj,
  174. const struct attribute_group *grp)
  175. {
  176. return internal_create_group(kobj, 1, grp);
  177. }
  178. EXPORT_SYMBOL_GPL(sysfs_update_group);
  179. /**
  180. * sysfs_remove_group: remove a group from a kobject
  181. * @kobj: kobject to remove the group from
  182. * @grp: group to remove
  183. *
  184. * This function removes a group of attributes from a kobject. The attributes
  185. * previously have to have been created for this group, otherwise it will fail.
  186. */
  187. void sysfs_remove_group(struct kobject *kobj,
  188. const struct attribute_group *grp)
  189. {
  190. struct kernfs_node *parent = kobj->sd;
  191. struct kernfs_node *kn;
  192. if (grp->name) {
  193. kn = kernfs_find_and_get(parent, grp->name);
  194. if (!kn) {
  195. WARN(!kn, KERN_WARNING
  196. "sysfs group %p not found for kobject '%s'\n",
  197. grp, kobject_name(kobj));
  198. return;
  199. }
  200. } else {
  201. kn = parent;
  202. kernfs_get(kn);
  203. }
  204. remove_files(kn, kobj, grp);
  205. if (grp->name)
  206. kernfs_remove(kn);
  207. kernfs_put(kn);
  208. }
  209. EXPORT_SYMBOL_GPL(sysfs_remove_group);
  210. /**
  211. * sysfs_remove_groups - remove a list of groups
  212. *
  213. * @kobj: The kobject for the groups to be removed from
  214. * @groups: NULL terminated list of groups to be removed
  215. *
  216. * If groups is not NULL, remove the specified groups from the kobject.
  217. */
  218. void sysfs_remove_groups(struct kobject *kobj,
  219. const struct attribute_group **groups)
  220. {
  221. int i;
  222. if (!groups)
  223. return;
  224. for (i = 0; groups[i]; i++)
  225. sysfs_remove_group(kobj, groups[i]);
  226. }
  227. EXPORT_SYMBOL_GPL(sysfs_remove_groups);
  228. /**
  229. * sysfs_merge_group - merge files into a pre-existing attribute group.
  230. * @kobj: The kobject containing the group.
  231. * @grp: The files to create and the attribute group they belong to.
  232. *
  233. * This function returns an error if the group doesn't exist or any of the
  234. * files already exist in that group, in which case none of the new files
  235. * are created.
  236. */
  237. int sysfs_merge_group(struct kobject *kobj,
  238. const struct attribute_group *grp)
  239. {
  240. struct kernfs_node *parent;
  241. int error = 0;
  242. struct attribute *const *attr;
  243. int i;
  244. parent = kernfs_find_and_get(kobj->sd, grp->name);
  245. if (!parent)
  246. return -ENOENT;
  247. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  248. error = sysfs_add_file(parent, *attr, false);
  249. if (error) {
  250. while (--i >= 0)
  251. kernfs_remove_by_name(parent, (*--attr)->name);
  252. }
  253. kernfs_put(parent);
  254. return error;
  255. }
  256. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  257. /**
  258. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  259. * @kobj: The kobject containing the group.
  260. * @grp: The files to remove and the attribute group they belong to.
  261. */
  262. void sysfs_unmerge_group(struct kobject *kobj,
  263. const struct attribute_group *grp)
  264. {
  265. struct kernfs_node *parent;
  266. struct attribute *const *attr;
  267. parent = kernfs_find_and_get(kobj->sd, grp->name);
  268. if (parent) {
  269. for (attr = grp->attrs; *attr; ++attr)
  270. kernfs_remove_by_name(parent, (*attr)->name);
  271. kernfs_put(parent);
  272. }
  273. }
  274. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  275. /**
  276. * sysfs_add_link_to_group - add a symlink to an attribute group.
  277. * @kobj: The kobject containing the group.
  278. * @group_name: The name of the group.
  279. * @target: The target kobject of the symlink to create.
  280. * @link_name: The name of the symlink to create.
  281. */
  282. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  283. struct kobject *target, const char *link_name)
  284. {
  285. struct kernfs_node *parent;
  286. int error = 0;
  287. parent = kernfs_find_and_get(kobj->sd, group_name);
  288. if (!parent)
  289. return -ENOENT;
  290. error = sysfs_create_link_sd(parent, target, link_name);
  291. kernfs_put(parent);
  292. return error;
  293. }
  294. EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
  295. /**
  296. * sysfs_remove_link_from_group - remove a symlink from an attribute group.
  297. * @kobj: The kobject containing the group.
  298. * @group_name: The name of the group.
  299. * @link_name: The name of the symlink to remove.
  300. */
  301. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  302. const char *link_name)
  303. {
  304. struct kernfs_node *parent;
  305. parent = kernfs_find_and_get(kobj->sd, group_name);
  306. if (parent) {
  307. kernfs_remove_by_name(parent, link_name);
  308. kernfs_put(parent);
  309. }
  310. }
  311. EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);