group.c 9.2 KB

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