group.c 8.1 KB

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