group.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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,
  17. const struct attribute_group *grp)
  18. {
  19. struct attribute *const* attr;
  20. for (attr = grp->attrs; *attr; attr++)
  21. sysfs_hash_and_remove(dir_sd, (*attr)->name);
  22. }
  23. static int create_files(struct sysfs_dirent *dir_sd,
  24. const struct attribute_group *grp)
  25. {
  26. struct attribute *const* attr;
  27. int error = 0;
  28. for (attr = grp->attrs; *attr && !error; attr++)
  29. error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
  30. if (error)
  31. remove_files(dir_sd, grp);
  32. return error;
  33. }
  34. int sysfs_create_group(struct kobject * kobj,
  35. const struct attribute_group * grp)
  36. {
  37. struct sysfs_dirent *sd;
  38. int error;
  39. BUG_ON(!kobj || !kobj->sd);
  40. if (grp->name) {
  41. error = sysfs_create_subdir(kobj, grp->name, &sd);
  42. if (error)
  43. return error;
  44. } else
  45. sd = kobj->sd;
  46. sysfs_get(sd);
  47. error = create_files(sd, grp);
  48. if (error) {
  49. if (grp->name)
  50. sysfs_remove_subdir(sd);
  51. }
  52. sysfs_put(sd);
  53. return error;
  54. }
  55. void sysfs_remove_group(struct kobject * kobj,
  56. const struct attribute_group * grp)
  57. {
  58. struct sysfs_dirent *dir_sd = kobj->sd;
  59. struct sysfs_dirent *sd;
  60. if (grp->name) {
  61. sd = sysfs_get_dirent(dir_sd, grp->name);
  62. BUG_ON(!sd);
  63. } else
  64. sd = sysfs_get(dir_sd);
  65. remove_files(sd, grp);
  66. if (grp->name)
  67. sysfs_remove_subdir(sd);
  68. sysfs_put(sd);
  69. }
  70. EXPORT_SYMBOL_GPL(sysfs_create_group);
  71. EXPORT_SYMBOL_GPL(sysfs_remove_group);