group.c 11 KB

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