dir.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. kuid_t uid;
  36. kgid_t gid;
  37. BUG_ON(!kobj);
  38. if (kobj->parent)
  39. parent = kobj->parent->sd;
  40. else
  41. parent = sysfs_root_kn;
  42. if (!parent)
  43. return -ENOENT;
  44. kobject_get_ownership(kobj, &uid, &gid);
  45. kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
  46. S_IRWXU | S_IRUGO | S_IXUGO, uid, gid,
  47. kobj, ns);
  48. if (IS_ERR(kn)) {
  49. if (PTR_ERR(kn) == -EEXIST)
  50. sysfs_warn_dup(parent, kobject_name(kobj));
  51. return PTR_ERR(kn);
  52. }
  53. kobj->sd = kn;
  54. return 0;
  55. }
  56. /**
  57. * sysfs_remove_dir - remove an object's directory.
  58. * @kobj: object.
  59. *
  60. * The only thing special about this is that we remove any files in
  61. * the directory before we remove the directory, and we've inlined
  62. * what used to be sysfs_rmdir() below, instead of calling separately.
  63. */
  64. void sysfs_remove_dir(struct kobject *kobj)
  65. {
  66. struct kernfs_node *kn = kobj->sd;
  67. /*
  68. * In general, kboject owner is responsible for ensuring removal
  69. * doesn't race with other operations and sysfs doesn't provide any
  70. * protection; however, when @kobj is used as a symlink target, the
  71. * symlinking entity usually doesn't own @kobj and thus has no
  72. * control over removal. @kobj->sd may be removed anytime
  73. * and symlink code may end up dereferencing an already freed node.
  74. *
  75. * sysfs_symlink_target_lock synchronizes @kobj->sd
  76. * disassociation against symlink operations so that symlink code
  77. * can safely dereference @kobj->sd.
  78. */
  79. spin_lock(&sysfs_symlink_target_lock);
  80. kobj->sd = NULL;
  81. spin_unlock(&sysfs_symlink_target_lock);
  82. if (kn) {
  83. WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
  84. kernfs_remove(kn);
  85. }
  86. }
  87. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  88. const void *new_ns)
  89. {
  90. struct kernfs_node *parent;
  91. int ret;
  92. parent = kernfs_get_parent(kobj->sd);
  93. ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
  94. kernfs_put(parent);
  95. return ret;
  96. }
  97. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  98. const void *new_ns)
  99. {
  100. struct kernfs_node *kn = kobj->sd;
  101. struct kernfs_node *new_parent;
  102. new_parent = new_parent_kobj && new_parent_kobj->sd ?
  103. new_parent_kobj->sd : sysfs_root_kn;
  104. return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
  105. }
  106. /**
  107. * sysfs_create_mount_point - create an always empty directory
  108. * @parent_kobj: kobject that will contain this always empty directory
  109. * @name: The name of the always empty directory to add
  110. */
  111. int sysfs_create_mount_point(struct kobject *parent_kobj, const char *name)
  112. {
  113. struct kernfs_node *kn, *parent = parent_kobj->sd;
  114. kn = kernfs_create_empty_dir(parent, name);
  115. if (IS_ERR(kn)) {
  116. if (PTR_ERR(kn) == -EEXIST)
  117. sysfs_warn_dup(parent, name);
  118. return PTR_ERR(kn);
  119. }
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(sysfs_create_mount_point);
  123. /**
  124. * sysfs_remove_mount_point - remove an always empty directory.
  125. * @parent_kobj: kobject that will contain this always empty directory
  126. * @name: The name of the always empty directory to remove
  127. *
  128. */
  129. void sysfs_remove_mount_point(struct kobject *parent_kobj, const char *name)
  130. {
  131. struct kernfs_node *parent = parent_kobj->sd;
  132. kernfs_remove_by_name_ns(parent, name, NULL);
  133. }
  134. EXPORT_SYMBOL_GPL(sysfs_remove_mount_point);