group.c 9.0 KB

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