group.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. mode_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->name) {
  60. error = sysfs_create_subdir(kobj, grp->name, &sd);
  61. if (error)
  62. return error;
  63. } else
  64. sd = kobj->sd;
  65. sysfs_get(sd);
  66. error = create_files(sd, kobj, grp, update);
  67. if (error) {
  68. if (grp->name)
  69. sysfs_remove_subdir(sd);
  70. }
  71. sysfs_put(sd);
  72. return error;
  73. }
  74. /**
  75. * sysfs_create_group - given a directory kobject, create an attribute group
  76. * @kobj: The kobject to create the group on
  77. * @grp: The attribute group to create
  78. *
  79. * This function creates a group for the first time. It will explicitly
  80. * warn and error if any of the attribute files being created already exist.
  81. *
  82. * Returns 0 on success or error.
  83. */
  84. int sysfs_create_group(struct kobject *kobj,
  85. const struct attribute_group *grp)
  86. {
  87. return internal_create_group(kobj, 0, grp);
  88. }
  89. /**
  90. * sysfs_update_group - given a directory kobject, update an attribute group
  91. * @kobj: The kobject to update the group on
  92. * @grp: The attribute group to update
  93. *
  94. * This function updates an attribute group. Unlike
  95. * sysfs_create_group(), it will explicitly not warn or error if any
  96. * of the attribute files being created already exist. Furthermore,
  97. * if the visibility of the files has changed through the is_visible()
  98. * callback, it will update the permissions and add or remove the
  99. * relevant files.
  100. *
  101. * The primary use for this function is to call it after making a change
  102. * that affects group visibility.
  103. *
  104. * Returns 0 on success or error.
  105. */
  106. int sysfs_update_group(struct kobject *kobj,
  107. const struct attribute_group *grp)
  108. {
  109. return internal_create_group(kobj, 1, grp);
  110. }
  111. void sysfs_remove_group(struct kobject * kobj,
  112. const struct attribute_group * grp)
  113. {
  114. struct sysfs_dirent *dir_sd = kobj->sd;
  115. struct sysfs_dirent *sd;
  116. if (grp->name) {
  117. sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
  118. if (!sd) {
  119. WARN(!sd, KERN_WARNING "sysfs group %p not found for "
  120. "kobject '%s'\n", grp, kobject_name(kobj));
  121. return;
  122. }
  123. } else
  124. sd = sysfs_get(dir_sd);
  125. remove_files(sd, kobj, grp);
  126. if (grp->name)
  127. sysfs_remove_subdir(sd);
  128. sysfs_put(sd);
  129. }
  130. /**
  131. * sysfs_merge_group - merge files into a pre-existing attribute group.
  132. * @kobj: The kobject containing the group.
  133. * @grp: The files to create and the attribute group they belong to.
  134. *
  135. * This function returns an error if the group doesn't exist or any of the
  136. * files already exist in that group, in which case none of the new files
  137. * are created.
  138. */
  139. int sysfs_merge_group(struct kobject *kobj,
  140. const struct attribute_group *grp)
  141. {
  142. struct sysfs_dirent *dir_sd;
  143. int error = 0;
  144. struct attribute *const *attr;
  145. int i;
  146. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  147. if (!dir_sd)
  148. return -ENOENT;
  149. for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
  150. error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
  151. if (error) {
  152. while (--i >= 0)
  153. sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
  154. }
  155. sysfs_put(dir_sd);
  156. return error;
  157. }
  158. EXPORT_SYMBOL_GPL(sysfs_merge_group);
  159. /**
  160. * sysfs_unmerge_group - remove files from a pre-existing attribute group.
  161. * @kobj: The kobject containing the group.
  162. * @grp: The files to remove and the attribute group they belong to.
  163. */
  164. void sysfs_unmerge_group(struct kobject *kobj,
  165. const struct attribute_group *grp)
  166. {
  167. struct sysfs_dirent *dir_sd;
  168. struct attribute *const *attr;
  169. dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
  170. if (dir_sd) {
  171. for (attr = grp->attrs; *attr; ++attr)
  172. sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
  173. sysfs_put(dir_sd);
  174. }
  175. }
  176. EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
  177. EXPORT_SYMBOL_GPL(sysfs_create_group);
  178. EXPORT_SYMBOL_GPL(sysfs_update_group);
  179. EXPORT_SYMBOL_GPL(sysfs_remove_group);