group.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, (*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, (*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, create an attribute group
  91. * @kobj: The kobject to create the group on
  92. * @grp: The attribute group to create
  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, 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. EXPORT_SYMBOL_GPL(sysfs_create_group);
  131. EXPORT_SYMBOL_GPL(sysfs_update_group);
  132. EXPORT_SYMBOL_GPL(sysfs_remove_group);