group.c 16 KB

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