group.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 <linux/fs.h>
  16. #include "sysfs.h"
  17. static void remove_files(struct kernfs_node *parent,
  18. const struct attribute_group *grp)
  19. {
  20. struct attribute *const *attr;
  21. struct bin_attribute *const *bin_attr;
  22. if (grp->attrs)
  23. for (attr = grp->attrs; *attr; attr++)
  24. kernfs_remove_by_name(parent, (*attr)->name);
  25. if (grp->bin_attrs)
  26. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++)
  27. kernfs_remove_by_name(parent, (*bin_attr)->attr.name);
  28. }
  29. static int create_files(struct kernfs_node *parent, struct kobject *kobj,
  30. kuid_t uid, kgid_t gid,
  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, uid, gid, 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 (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
  67. umode_t mode = (*bin_attr)->attr.mode;
  68. if (update)
  69. kernfs_remove_by_name(parent,
  70. (*bin_attr)->attr.name);
  71. if (grp->is_bin_visible) {
  72. mode = grp->is_bin_visible(kobj, *bin_attr, i);
  73. if (!mode)
  74. continue;
  75. }
  76. WARN(mode & ~(SYSFS_PREALLOC | 0664),
  77. "Attribute %s: Invalid permissions 0%o\n",
  78. (*bin_attr)->attr.name, mode);
  79. mode &= SYSFS_PREALLOC | 0664;
  80. error = sysfs_add_file_mode_ns(parent,
  81. &(*bin_attr)->attr, true,
  82. mode,
  83. uid, gid, NULL);
  84. if (error)
  85. break;
  86. }
  87. if (error)
  88. remove_files(parent, grp);
  89. }
  90. exit:
  91. return error;
  92. }
  93. static int internal_create_group(struct kobject *kobj, int update,
  94. const struct attribute_group *grp)
  95. {
  96. struct kernfs_node *kn;
  97. kuid_t uid;
  98. kgid_t gid;
  99. int error;
  100. if (WARN_ON(!kobj || (!update && !kobj->sd)))
  101. return -EINVAL;
  102. /* Updates may happen before the object has been instantiated */
  103. if (unlikely(update && !kobj->sd))
  104. return -EINVAL;
  105. if (!grp->attrs && !grp->bin_attrs) {
  106. WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n",
  107. kobj->name, grp->name ?: "");
  108. return -EINVAL;
  109. }
  110. kobject_get_ownership(kobj, &uid, &gid);
  111. if (grp->name) {
  112. if (update) {
  113. kn = kernfs_find_and_get(kobj->sd, grp->name);
  114. if (!kn) {
  115. pr_warn("Can't update unknown attr grp name: %s/%s\n",
  116. kobj->name, grp->name);
  117. return -EINVAL;
  118. }
  119. } else {
  120. kn = kernfs_create_dir_ns(kobj->sd, grp->name,
  121. S_IRWXU | S_IRUGO | S_IXUGO,
  122. uid, gid, kobj, NULL);
  123. if (IS_ERR(kn)) {
  124. if (PTR_ERR(kn) == -EEXIST)
  125. sysfs_warn_dup(kobj->sd, grp->name);
  126. return PTR_ERR(kn);
  127. }
  128. }
  129. } else
  130. kn = kobj->sd;
  131. kernfs_get(kn);
  132. error = create_files(kn, kobj, uid, gid, grp, update);
  133. if (error) {
  134. if (grp->name)
  135. kernfs_remove(kn);
  136. }
  137. kernfs_put(kn);
  138. if (grp->name && update)
  139. kernfs_put(kn);
  140. return error;
  141. }
  142. /**
  143. * sysfs_create_group - given a directory kobject, create an attribute group
  144. * @kobj: The kobject to create the group on
  145. * @grp: The attribute group to create
  146. *
  147. * This function creates a group for the first time. It will explicitly
  148. * warn and error if any of the attribute files being created already exist.
  149. *
  150. * Returns 0 on success or error code on failure.
  151. */
  152. int sysfs_create_group(struct kobject *kobj,
  153. const struct attribute_group *grp)
  154. {
  155. return internal_create_group(kobj, 0, grp);
  156. }
  157. EXPORT_SYMBOL_GPL(sysfs_create_group);
  158. static int internal_create_groups(struct kobject *kobj, int update,
  159. const struct attribute_group **groups)
  160. {
  161. int error = 0;
  162. int i;
  163. if (!groups)
  164. return 0;
  165. for (i = 0; groups[i]; i++) {
  166. error = internal_create_group(kobj, update, groups[i]);
  167. if (error) {
  168. while (--i >= 0)
  169. sysfs_remove_group(kobj, groups[i]);
  170. break;
  171. }
  172. }
  173. return error;
  174. }
  175. /**
  176. * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
  177. * @kobj: The kobject to create the group on
  178. * @groups: The attribute groups to create, NULL terminated
  179. *
  180. * This function creates a bunch of attribute groups. If an error occurs when
  181. * creating a group, all previously created groups will be removed, unwinding
  182. * everything back to the original state when this function was called.
  183. * It will explicitly warn and error if any of the attribute files being
  184. * created already exist.
  185. *
  186. * Returns 0 on success or error code from sysfs_create_group on failure.
  187. */
  188. int sysfs_create_groups(struct kobject *kobj,
  189. const struct attribute_group **groups)
  190. {
  191. return internal_create_groups(kobj, 0, groups);
  192. }
  193. EXPORT_SYMBOL_GPL(sysfs_create_groups);
  194. /**
  195. * sysfs_update_groups - given a directory kobject, create a bunch of attribute groups
  196. * @kobj: The kobject to update the group on
  197. * @groups: The attribute groups to update, NULL terminated
  198. *
  199. * This function update a bunch of attribute groups. If an error occurs when
  200. * updating a group, all previously updated groups will be removed together
  201. * with already existing (not updated) attributes.
  202. *
  203. * Returns 0 on success or error code from sysfs_update_group on failure.
  204. */
  205. int sysfs_update_groups(struct kobject *kobj,
  206. const struct attribute_group **groups)
  207. {
  208. return internal_create_groups(kobj, 1, groups);
  209. }
  210. EXPORT_SYMBOL_GPL(sysfs_update_groups);
  211. /**
  212. * sysfs_update_group - given a directory kobject, update an attribute group
  213. * @kobj: The kobject to update the group on
  214. * @grp: The attribute group to update
  215. *
  216. * This function updates an attribute group. Unlike
  217. * sysfs_create_group(), it will explicitly not warn or error if any
  218. * of the attribute files being created already exist. Furthermore,
  219. * if the visibility of the files has changed through the is_visible()
  220. * callback, it will update the permissions and add or remove the
  221. * relevant files. Changing a group's name (subdirectory name under
  222. * kobj's directory in sysfs) is not allowed.
  223. *
  224. * The primary use for this function is to call it after making a change
  225. * that affects group visibility.
  226. *
  227. * Returns 0 on success or error code on failure.
  228. */
  229. int sysfs_update_group(struct kobject *kobj,
  230. const struct attribute_group *grp)
  231. {
  232. return internal_create_group(kobj, 1, grp);
  233. }
  234. EXPORT_SYMBOL_GPL(sysfs_update_group);
  235. /**
  236. * sysfs_remove_group: remove a group from a kobject
  237. * @kobj: kobject to remove the group from
  238. * @grp: group to remove
  239. *
  240. * This function removes a group of attributes from a kobject. The attributes
  241. * previously have to have been created for this group, otherwise it will fail.
  242. */
  243. void sysfs_remove_group(struct kobject *kobj,
  244. const struct attribute_group *grp)
  245. {
  246. struct kernfs_node *parent = kobj->sd;
  247. struct kernfs_node *kn;
  248. if (grp->name) {
  249. kn = kernfs_find_and_get(parent, grp->name);
  250. if (!kn) {
  251. WARN(!kn, KERN_WARNING
  252. "sysfs group '%s' not found for kobject '%s'\n",
  253. grp->name, kobject_name(kobj));
  254. return;
  255. }
  256. } else {
  257. kn = parent;
  258. kernfs_get(kn);
  259. }
  260. remove_files(kn, grp);
  261. if (grp->name)
  262. kernfs_remove(kn);
  263. kernfs_put(kn);
  264. }
  265. EXPORT_SYMBOL_GPL(sysfs_remove_group);
  266. /**
  267. * sysfs_remove_groups - remove a list of groups
  268. *
  269. * @kobj: The kobject for the groups to be removed from
  270. * @groups: NULL terminated list of groups to be removed
  271. *
  272. * If groups is not NULL, remove the specified groups from the kobject.
  273. */
  274. void sysfs_remove_groups(struct kobject *kobj,
  275. const struct attribute_group **groups)
  276. {
  277. int i;
  278. if (!groups)
  279. return;
  280. for (i = 0; groups[i]; i++)
  281. sysfs_remove_group(kobj, groups[i]);
  282. }
  283. EXPORT_SYMBOL_GPL(sysfs_remove_groups);
  284. /**
  285. * sysfs_merge_group - merge files into a pre-existing attribute group.
  286. * @kobj: The kobject containing the group.
  287. * @grp: The files to create and the attribute group they belong to.
  288. *
  289. * This function returns an error if the group doesn't exist or any of the
  290. * files already exist in that group, in which case none of the new files
  291. * are created.
  292. */
  293. int sysfs_merge_group(struct kobject *kobj,
  294. const struct attribute_group *grp)
  295. {
  296. struct kernfs_node *parent;
  297. kuid_t uid;
  298. kgid_t gid;
  299. int error = 0;
  300. struct attribute *const *attr;
  301. int i;
  302. parent = kernfs_find_and_get(kobj->sd, grp->name);
  303. if (!parent)
  304. return -ENOENT;
  305. kobject_get_ownership(kobj, &uid, &gid);
  306. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  307. error = sysfs_add_file_mode_ns(parent, *attr, false,
  308. (*attr)->mode, uid, gid, NULL);
  309. if (error) {
  310. while (--i >= 0)
  311. kernfs_remove_by_name(parent, (*--attr)->name);
  312. }
  313. kernfs_put(parent);
  314. return error;
  315. }
  316. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  317. /**
  318. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  319. * @kobj: The kobject containing the group.
  320. * @grp: The files to remove and the attribute group they belong to.
  321. */
  322. void sysfs_unmerge_group(struct kobject *kobj,
  323. const struct attribute_group *grp)
  324. {
  325. struct kernfs_node *parent;
  326. struct attribute *const *attr;
  327. parent = kernfs_find_and_get(kobj->sd, grp->name);
  328. if (parent) {
  329. for (attr = grp->attrs; *attr; ++attr)
  330. kernfs_remove_by_name(parent, (*attr)->name);
  331. kernfs_put(parent);
  332. }
  333. }
  334. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  335. /**
  336. * sysfs_add_link_to_group - add a symlink to an attribute group.
  337. * @kobj: The kobject containing the group.
  338. * @group_name: The name of the group.
  339. * @target: The target kobject of the symlink to create.
  340. * @link_name: The name of the symlink to create.
  341. */
  342. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  343. struct kobject *target, const char *link_name)
  344. {
  345. struct kernfs_node *parent;
  346. int error = 0;
  347. parent = kernfs_find_and_get(kobj->sd, group_name);
  348. if (!parent)
  349. return -ENOENT;
  350. error = sysfs_create_link_sd(parent, target, link_name);
  351. kernfs_put(parent);
  352. return error;
  353. }
  354. EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
  355. /**
  356. * sysfs_remove_link_from_group - remove a symlink from an attribute group.
  357. * @kobj: The kobject containing the group.
  358. * @group_name: The name of the group.
  359. * @link_name: The name of the symlink to remove.
  360. */
  361. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  362. const char *link_name)
  363. {
  364. struct kernfs_node *parent;
  365. parent = kernfs_find_and_get(kobj->sd, group_name);
  366. if (parent) {
  367. kernfs_remove_by_name(parent, link_name);
  368. kernfs_put(parent);
  369. }
  370. }
  371. EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
  372. /**
  373. * compat_only_sysfs_link_entry_to_kobj - add a symlink to a kobject pointing
  374. * to a group or an attribute
  375. * @kobj: The kobject containing the group.
  376. * @target_kobj: The target kobject.
  377. * @target_name: The name of the target group or attribute.
  378. * @symlink_name: The name of the symlink file (target_name will be
  379. * considered if symlink_name is NULL).
  380. */
  381. int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
  382. struct kobject *target_kobj,
  383. const char *target_name,
  384. const char *symlink_name)
  385. {
  386. struct kernfs_node *target;
  387. struct kernfs_node *entry;
  388. struct kernfs_node *link;
  389. /*
  390. * We don't own @target_kobj and it may be removed at any time.
  391. * Synchronize using sysfs_symlink_target_lock. See sysfs_remove_dir()
  392. * for details.
  393. */
  394. spin_lock(&sysfs_symlink_target_lock);
  395. target = target_kobj->sd;
  396. if (target)
  397. kernfs_get(target);
  398. spin_unlock(&sysfs_symlink_target_lock);
  399. if (!target)
  400. return -ENOENT;
  401. entry = kernfs_find_and_get(target_kobj->sd, target_name);
  402. if (!entry) {
  403. kernfs_put(target);
  404. return -ENOENT;
  405. }
  406. if (!symlink_name)
  407. symlink_name = target_name;
  408. link = kernfs_create_link(kobj->sd, symlink_name, entry);
  409. if (PTR_ERR(link) == -EEXIST)
  410. sysfs_warn_dup(kobj->sd, symlink_name);
  411. kernfs_put(entry);
  412. kernfs_put(target);
  413. return PTR_ERR_OR_ZERO(link);
  414. }
  415. EXPORT_SYMBOL_GPL(compat_only_sysfs_link_entry_to_kobj);
  416. static int sysfs_group_attrs_change_owner(struct kernfs_node *grp_kn,
  417. const struct attribute_group *grp,
  418. struct iattr *newattrs)
  419. {
  420. struct kernfs_node *kn;
  421. int error;
  422. if (grp->attrs) {
  423. struct attribute *const *attr;
  424. for (attr = grp->attrs; *attr; attr++) {
  425. kn = kernfs_find_and_get(grp_kn, (*attr)->name);
  426. if (!kn)
  427. return -ENOENT;
  428. error = kernfs_setattr(kn, newattrs);
  429. kernfs_put(kn);
  430. if (error)
  431. return error;
  432. }
  433. }
  434. if (grp->bin_attrs) {
  435. struct bin_attribute *const *bin_attr;
  436. for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) {
  437. kn = kernfs_find_and_get(grp_kn, (*bin_attr)->attr.name);
  438. if (!kn)
  439. return -ENOENT;
  440. error = kernfs_setattr(kn, newattrs);
  441. kernfs_put(kn);
  442. if (error)
  443. return error;
  444. }
  445. }
  446. return 0;
  447. }
  448. /**
  449. * sysfs_group_change_owner - change owner of an attribute group.
  450. * @kobj: The kobject containing the group.
  451. * @grp: The attribute group.
  452. * @kuid: new owner's kuid
  453. * @kgid: new owner's kgid
  454. *
  455. * Returns 0 on success or error code on failure.
  456. */
  457. int sysfs_group_change_owner(struct kobject *kobj,
  458. const struct attribute_group *grp, kuid_t kuid,
  459. kgid_t kgid)
  460. {
  461. struct kernfs_node *grp_kn;
  462. int error;
  463. struct iattr newattrs = {
  464. .ia_valid = ATTR_UID | ATTR_GID,
  465. .ia_uid = kuid,
  466. .ia_gid = kgid,
  467. };
  468. if (!kobj->state_in_sysfs)
  469. return -EINVAL;
  470. if (grp->name) {
  471. grp_kn = kernfs_find_and_get(kobj->sd, grp->name);
  472. } else {
  473. kernfs_get(kobj->sd);
  474. grp_kn = kobj->sd;
  475. }
  476. if (!grp_kn)
  477. return -ENOENT;
  478. error = kernfs_setattr(grp_kn, &newattrs);
  479. if (!error)
  480. error = sysfs_group_attrs_change_owner(grp_kn, grp, &newattrs);
  481. kernfs_put(grp_kn);
  482. return error;
  483. }
  484. EXPORT_SYMBOL_GPL(sysfs_group_change_owner);
  485. /**
  486. * sysfs_groups_change_owner - change owner of a set of attribute groups.
  487. * @kobj: The kobject containing the groups.
  488. * @groups: The attribute groups.
  489. * @kuid: new owner's kuid
  490. * @kgid: new owner's kgid
  491. *
  492. * Returns 0 on success or error code on failure.
  493. */
  494. int sysfs_groups_change_owner(struct kobject *kobj,
  495. const struct attribute_group **groups,
  496. kuid_t kuid, kgid_t kgid)
  497. {
  498. int error = 0, i;
  499. if (!kobj->state_in_sysfs)
  500. return -EINVAL;
  501. if (!groups)
  502. return 0;
  503. for (i = 0; groups[i]; i++) {
  504. error = sysfs_group_change_owner(kobj, groups[i], kuid, kgid);
  505. if (error)
  506. break;
  507. }
  508. return error;
  509. }
  510. EXPORT_SYMBOL_GPL(sysfs_groups_change_owner);