group.c 11 KB

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