group.c 8.7 KB

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