group.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. *
  7. * This file is released undert the GPL v2.
  8. *
  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 sysfs_dirent *dir_sd, struct kobject *kobj,
  17. const struct attribute_group *grp)
  18. {
  19. struct attribute *const* attr;
  20. int i;
  21. for (i = 0, attr = grp->attrs; *attr; i++, attr++)
  22. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  23. }
  24. static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
  25. const struct attribute_group *grp, int update)
  26. {
  27. struct attribute *const* attr;
  28. int error = 0, i;
  29. for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
  30. umode_t mode = 0;
  31. /* in update mode, we're changing the permissions or
  32. * visibility. Do this by first removing then
  33. * re-adding (if required) the file */
  34. if (update)
  35. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  36. if (grp->is_visible) {
  37. mode = grp->is_visible(kobj, *attr, i);
  38. if (!mode)
  39. continue;
  40. }
  41. error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
  42. (*attr)->mode | mode);
  43. if (unlikely(error))
  44. break;
  45. }
  46. if (error)
  47. remove_files(dir_sd, kobj, grp);
  48. return error;
  49. }
  50. static int internal_create_group(struct kobject *kobj, int update,
  51. const struct attribute_group *grp)
  52. {
  53. struct sysfs_dirent *sd;
  54. int error;
  55. BUG_ON(!kobj || (!update && !kobj->sd));
  56. /* Updates may happen before the object has been instantiated */
  57. if (unlikely(update && !kobj->sd))
  58. return -EINVAL;
  59. if (!grp->attrs) {
  60. WARN(1, "sysfs: attrs not set by subsystem for group: %s/%s\n",
  61. kobj->name, grp->name ? "" : grp->name);
  62. return -EINVAL;
  63. }
  64. if (grp->name) {
  65. error = sysfs_create_subdir(kobj, grp->name, &sd);
  66. if (error)
  67. return error;
  68. } else
  69. sd = kobj->sd;
  70. sysfs_get(sd);
  71. error = create_files(sd, kobj, grp, update);
  72. if (error) {
  73. if (grp->name)
  74. sysfs_remove_subdir(sd);
  75. }
  76. sysfs_put(sd);
  77. return error;
  78. }
  79. /**
  80. * sysfs_create_group - given a directory kobject, create an attribute group
  81. * @kobj: The kobject to create the group on
  82. * @grp: The attribute group to create
  83. *
  84. * This function creates a group for the first time. It will explicitly
  85. * warn and error if any of the attribute files being created already exist.
  86. *
  87. * Returns 0 on success or error.
  88. */
  89. int sysfs_create_group(struct kobject *kobj,
  90. const struct attribute_group *grp)
  91. {
  92. return internal_create_group(kobj, 0, grp);
  93. }
  94. /**
  95. * sysfs_update_group - given a directory kobject, update an attribute group
  96. * @kobj: The kobject to update the group on
  97. * @grp: The attribute group to update
  98. *
  99. * This function updates an attribute group. Unlike
  100. * sysfs_create_group(), it will explicitly not warn or error if any
  101. * of the attribute files being created already exist. Furthermore,
  102. * if the visibility of the files has changed through the is_visible()
  103. * callback, it will update the permissions and add or remove the
  104. * relevant files.
  105. *
  106. * The primary use for this function is to call it after making a change
  107. * that affects group visibility.
  108. *
  109. * Returns 0 on success or error.
  110. */
  111. int sysfs_update_group(struct kobject *kobj,
  112. const struct attribute_group *grp)
  113. {
  114. return internal_create_group(kobj, 1, grp);
  115. }
  116. void sysfs_remove_group(struct kobject * kobj,
  117. const struct attribute_group * grp)
  118. {
  119. struct sysfs_dirent *dir_sd = kobj->sd;
  120. struct sysfs_dirent *sd;
  121. if (grp->name) {
  122. sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
  123. if (!sd) {
  124. WARN(!sd, KERN_WARNING "sysfs group %p not found for "
  125. "kobject '%s'\n", grp, kobject_name(kobj));
  126. return;
  127. }
  128. } else
  129. sd = sysfs_get(dir_sd);
  130. remove_files(sd, kobj, grp);
  131. if (grp->name)
  132. sysfs_remove_subdir(sd);
  133. sysfs_put(sd);
  134. }
  135. /**
  136. * sysfs_merge_group - merge files into a pre-existing attribute group.
  137. * @kobj: The kobject containing the group.
  138. * @grp: The files to create and the attribute group they belong to.
  139. *
  140. * This function returns an error if the group doesn't exist or any of the
  141. * files already exist in that group, in which case none of the new files
  142. * are created.
  143. */
  144. int sysfs_merge_group(struct kobject *kobj,
  145. const struct attribute_group *grp)
  146. {
  147. struct sysfs_dirent *dir_sd;
  148. int error = 0;
  149. struct attribute *const *attr;
  150. int i;
  151. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  152. if (!dir_sd)
  153. return -ENOENT;
  154. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  155. error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
  156. if (error) {
  157. while (--i >= 0)
  158. sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
  159. }
  160. sysfs_put(dir_sd);
  161. return error;
  162. }
  163. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  164. /**
  165. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  166. * @kobj: The kobject containing the group.
  167. * @grp: The files to remove and the attribute group they belong to.
  168. */
  169. void sysfs_unmerge_group(struct kobject *kobj,
  170. const struct attribute_group *grp)
  171. {
  172. struct sysfs_dirent *dir_sd;
  173. struct attribute *const *attr;
  174. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  175. if (dir_sd) {
  176. for (attr = grp->attrs; *attr; ++attr)
  177. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  178. sysfs_put(dir_sd);
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  182. EXPORT_SYMBOL_GPL(sysfs_create_group);
  183. EXPORT_SYMBOL_GPL(sysfs_update_group);
  184. EXPORT_SYMBOL_GPL(sysfs_remove_group);