group.c 9.0 KB

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