group.c 8.7 KB

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