dir.c 4.0 KB

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