dir.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/sysfs/dir.c - sysfs core and dir operation implementation
  4. *
  5. * Copyright (c) 2001-3 Patrick Mochel
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  8. *
  9. * Please see Documentation/filesystems/sysfs.txt for more information.
  10. */
  11. #define pr_fmt(fmt) "sysfs: " fmt
  12. #include <linux/fs.h>
  13. #include <linux/kobject.h>
  14. #include <linux/slab.h>
  15. #include "sysfs.h"
  16. DEFINE_SPINLOCK(sysfs_symlink_target_lock);
  17. void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
  18. {
  19. char *buf;
  20. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  21. if (buf)
  22. kernfs_path(parent, buf, PATH_MAX);
  23. pr_warn("cannot create duplicate filename '%s/%s'\n", buf, name);
  24. dump_stack();
  25. kfree(buf);
  26. }
  27. /**
  28. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  29. * @kobj: object we're creating directory for
  30. * @ns: the namespace tag to use
  31. */
  32. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  33. {
  34. struct kernfs_node *parent, *kn;
  35. BUG_ON(!kobj);
  36. if (kobj->parent)
  37. parent = kobj->parent->sd;
  38. else
  39. parent = sysfs_root_kn;
  40. if (!parent)
  41. return -ENOENT;
  42. kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
  43. S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
  44. if (IS_ERR(kn)) {
  45. if (PTR_ERR(kn) == -EEXIST)
  46. sysfs_warn_dup(parent, kobject_name(kobj));
  47. return PTR_ERR(kn);
  48. }
  49. kobj->sd = kn;
  50. return 0;
  51. }
  52. /**
  53. * sysfs_remove_dir - remove an object's directory.
  54. * @kobj: object.
  55. *
  56. * The only thing special about this is that we remove any files in
  57. * the directory before we remove the directory, and we've inlined
  58. * what used to be sysfs_rmdir() below, instead of calling separately.
  59. */
  60. void sysfs_remove_dir(struct kobject *kobj)
  61. {
  62. struct kernfs_node *kn = kobj->sd;
  63. /*
  64. * In general, kboject owner is responsible for ensuring removal
  65. * doesn't race with other operations and sysfs doesn't provide any
  66. * protection; however, when @kobj is used as a symlink target, the
  67. * symlinking entity usually doesn't own @kobj and thus has no
  68. * control over removal. @kobj->sd may be removed anytime
  69. * and symlink code may end up dereferencing an already freed node.
  70. *
  71. * sysfs_symlink_target_lock synchronizes @kobj->sd
  72. * disassociation against symlink operations so that symlink code
  73. * can safely dereference @kobj->sd.
  74. */
  75. spin_lock(&sysfs_symlink_target_lock);
  76. kobj->sd = NULL;
  77. spin_unlock(&sysfs_symlink_target_lock);
  78. if (kn) {
  79. WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
  80. kernfs_remove(kn);
  81. }
  82. }
  83. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  84. const void *new_ns)
  85. {
  86. struct kernfs_node *parent;
  87. int ret;
  88. parent = kernfs_get_parent(kobj->sd);
  89. ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
  90. kernfs_put(parent);
  91. return ret;
  92. }
  93. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  94. const void *new_ns)
  95. {
  96. struct kernfs_node *kn = kobj->sd;
  97. struct kernfs_node *new_parent;
  98. new_parent = new_parent_kobj && new_parent_kobj->sd ?
  99. new_parent_kobj->sd : sysfs_root_kn;
  100. return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
  101. }
  102. /**
  103. * sysfs_create_mount_point - create an always empty directory
  104. * @parent_kobj: kobject that will contain this always empty directory
  105. * @name: The name of the always empty directory to add
  106. */
  107. int sysfs_create_mount_point(struct kobject *parent_kobj, const char *name)
  108. {
  109. struct kernfs_node *kn, *parent = parent_kobj->sd;
  110. kn = kernfs_create_empty_dir(parent, name);
  111. if (IS_ERR(kn)) {
  112. if (PTR_ERR(kn) == -EEXIST)
  113. sysfs_warn_dup(parent, name);
  114. return PTR_ERR(kn);
  115. }
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(sysfs_create_mount_point);
  119. /**
  120. * sysfs_remove_mount_point - remove an always empty directory.
  121. * @parent_kobj: kobject that will contain this always empty directory
  122. * @name: The name of the always empty directory to remove
  123. *
  124. */
  125. void sysfs_remove_mount_point(struct kobject *parent_kobj, const char *name)
  126. {
  127. struct kernfs_node *parent = parent_kobj->sd;
  128. kernfs_remove_by_name_ns(parent, name, NULL);
  129. }
  130. EXPORT_SYMBOL_GPL(sysfs_remove_mount_point);