group.c 9.3 KB

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