group.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include <linux/kobject.h>
  11. #include <linux/module.h>
  12. #include <linux/dcache.h>
  13. #include <linux/namei.h>
  14. #include <linux/err.h>
  15. #include "sysfs.h"
  16. static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
  17. const struct attribute_group *grp)
  18. {
  19. struct attribute *const* attr;
  20. struct bin_attribute *const* bin_attr;
  21. if (grp->attrs)
  22. for (attr = grp->attrs; *attr; attr++)
  23. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  24. if (grp->bin_attrs)
  25. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++)
  26. sysfs_remove_bin_file(kobj, *bin_attr);
  27. }
  28. static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
  29. const struct attribute_group *grp, int update)
  30. {
  31. struct attribute *const* attr;
  32. struct bin_attribute *const* bin_attr;
  33. int error = 0, i;
  34. if (grp->attrs) {
  35. for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
  36. umode_t mode = 0;
  37. /*
  38. * In update mode, we're changing the permissions or
  39. * visibility. Do this by first removing then
  40. * re-adding (if required) the file.
  41. */
  42. if (update)
  43. sysfs_hash_and_remove(dir_sd, NULL,
  44. (*attr)->name);
  45. if (grp->is_visible) {
  46. mode = grp->is_visible(kobj, *attr, i);
  47. if (!mode)
  48. continue;
  49. }
  50. error = sysfs_add_file_mode(dir_sd, *attr,
  51. SYSFS_KOBJ_ATTR,
  52. (*attr)->mode | mode);
  53. if (unlikely(error))
  54. break;
  55. }
  56. if (error) {
  57. remove_files(dir_sd, kobj, grp);
  58. goto exit;
  59. }
  60. }
  61. if (grp->bin_attrs) {
  62. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) {
  63. if (update)
  64. sysfs_remove_bin_file(kobj, *bin_attr);
  65. error = sysfs_create_bin_file(kobj, *bin_attr);
  66. if (error)
  67. break;
  68. }
  69. if (error)
  70. remove_files(dir_sd, kobj, grp);
  71. }
  72. exit:
  73. return error;
  74. }
  75. static int internal_create_group(struct kobject *kobj, int update,
  76. const struct attribute_group *grp)
  77. {
  78. struct sysfs_dirent *sd;
  79. int error;
  80. BUG_ON(!kobj || (!update && !kobj->sd));
  81. /* Updates may happen before the object has been instantiated */
  82. if (unlikely(update && !kobj->sd))
  83. return -EINVAL;
  84. if (!grp->attrs && !grp->bin_attrs) {
  85. WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n",
  86. kobj->name, grp->name ? "" : grp->name);
  87. return -EINVAL;
  88. }
  89. if (grp->name) {
  90. error = sysfs_create_subdir(kobj, grp->name, &sd);
  91. if (error)
  92. return error;
  93. } else
  94. sd = kobj->sd;
  95. sysfs_get(sd);
  96. error = create_files(sd, kobj, grp, update);
  97. if (error) {
  98. if (grp->name)
  99. sysfs_remove_subdir(sd);
  100. }
  101. sysfs_put(sd);
  102. return error;
  103. }
  104. /**
  105. * sysfs_create_group - given a directory kobject, create an attribute group
  106. * @kobj: The kobject to create the group on
  107. * @grp: The attribute group to create
  108. *
  109. * This function creates a group for the first time. It will explicitly
  110. * warn and error if any of the attribute files being created already exist.
  111. *
  112. * Returns 0 on success or error.
  113. */
  114. int sysfs_create_group(struct kobject *kobj,
  115. const struct attribute_group *grp)
  116. {
  117. return internal_create_group(kobj, 0, grp);
  118. }
  119. /**
  120. * sysfs_update_group - given a directory kobject, update an attribute group
  121. * @kobj: The kobject to update the group on
  122. * @grp: The attribute group to update
  123. *
  124. * This function updates an attribute group. Unlike
  125. * sysfs_create_group(), it will explicitly not warn or error if any
  126. * of the attribute files being created already exist. Furthermore,
  127. * if the visibility of the files has changed through the is_visible()
  128. * callback, it will update the permissions and add or remove the
  129. * relevant files.
  130. *
  131. * The primary use for this function is to call it after making a change
  132. * that affects group visibility.
  133. *
  134. * Returns 0 on success or error.
  135. */
  136. int sysfs_update_group(struct kobject *kobj,
  137. const struct attribute_group *grp)
  138. {
  139. return internal_create_group(kobj, 1, grp);
  140. }
  141. void sysfs_remove_group(struct kobject * kobj,
  142. const struct attribute_group * grp)
  143. {
  144. struct sysfs_dirent *dir_sd = kobj->sd;
  145. struct sysfs_dirent *sd;
  146. if (grp->name) {
  147. sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
  148. if (!sd) {
  149. WARN(!sd, KERN_WARNING "sysfs group %p not found for "
  150. "kobject '%s'\n", grp, kobject_name(kobj));
  151. return;
  152. }
  153. } else
  154. sd = sysfs_get(dir_sd);
  155. remove_files(sd, kobj, grp);
  156. if (grp->name)
  157. sysfs_remove_subdir(sd);
  158. sysfs_put(sd);
  159. }
  160. /**
  161. * sysfs_merge_group - merge files into a pre-existing attribute group.
  162. * @kobj: The kobject containing the group.
  163. * @grp: The files to create and the attribute group they belong to.
  164. *
  165. * This function returns an error if the group doesn't exist or any of the
  166. * files already exist in that group, in which case none of the new files
  167. * are created.
  168. */
  169. int sysfs_merge_group(struct kobject *kobj,
  170. const struct attribute_group *grp)
  171. {
  172. struct sysfs_dirent *dir_sd;
  173. int error = 0;
  174. struct attribute *const *attr;
  175. int i;
  176. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  177. if (!dir_sd)
  178. return -ENOENT;
  179. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  180. error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
  181. if (error) {
  182. while (--i >= 0)
  183. sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
  184. }
  185. sysfs_put(dir_sd);
  186. return error;
  187. }
  188. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  189. /**
  190. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  191. * @kobj: The kobject containing the group.
  192. * @grp: The files to remove and the attribute group they belong to.
  193. */
  194. void sysfs_unmerge_group(struct kobject *kobj,
  195. const struct attribute_group *grp)
  196. {
  197. struct sysfs_dirent *dir_sd;
  198. struct attribute *const *attr;
  199. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  200. if (dir_sd) {
  201. for (attr = grp->attrs; *attr; ++attr)
  202. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  203. sysfs_put(dir_sd);
  204. }
  205. }
  206. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  207. /**
  208. * sysfs_add_link_to_group - add a symlink to an attribute group.
  209. * @kobj: The kobject containing the group.
  210. * @group_name: The name of the group.
  211. * @target: The target kobject of the symlink to create.
  212. * @link_name: The name of the symlink to create.
  213. */
  214. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  215. struct kobject *target, const char *link_name)
  216. {
  217. struct sysfs_dirent *dir_sd;
  218. int error = 0;
  219. dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
  220. if (!dir_sd)
  221. return -ENOENT;
  222. error = sysfs_create_link_sd(dir_sd, target, link_name);
  223. sysfs_put(dir_sd);
  224. return error;
  225. }
  226. EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
  227. /**
  228. * sysfs_remove_link_from_group - remove a symlink from an attribute group.
  229. * @kobj: The kobject containing the group.
  230. * @group_name: The name of the group.
  231. * @link_name: The name of the symlink to remove.
  232. */
  233. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  234. const char *link_name)
  235. {
  236. struct sysfs_dirent *dir_sd;
  237. dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
  238. if (dir_sd) {
  239. sysfs_hash_and_remove(dir_sd, NULL, link_name);
  240. sysfs_put(dir_sd);
  241. }
  242. }
  243. EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
  244. EXPORT_SYMBOL_GPL(sysfs_create_group);
  245. EXPORT_SYMBOL_GPL(sysfs_update_group);
  246. EXPORT_SYMBOL_GPL(sysfs_remove_group);