group.c 11 KB

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