group.c 15 KB

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