dir.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  19. * sysfs_pathname - return full path to sysfs dirent
  20. * @kn: kernfs_node whose path we want
  21. * @path: caller allocated buffer of size PATH_MAX
  22. *
  23. * Gives the name "/" to the sysfs_root entry; any path returned
  24. * is relative to wherever sysfs is mounted.
  25. */
  26. static char *sysfs_pathname(struct kernfs_node *kn, char *path)
  27. {
  28. if (kn->parent) {
  29. sysfs_pathname(kn->parent, path);
  30. strlcat(path, "/", PATH_MAX);
  31. }
  32. strlcat(path, kn->name, PATH_MAX);
  33. return path;
  34. }
  35. void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
  36. {
  37. char *path;
  38. path = kzalloc(PATH_MAX, GFP_KERNEL);
  39. if (path) {
  40. sysfs_pathname(parent, path);
  41. strlcat(path, "/", PATH_MAX);
  42. strlcat(path, name, PATH_MAX);
  43. }
  44. WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
  45. path ? path : name);
  46. kfree(path);
  47. }
  48. /**
  49. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  50. * @kobj: object we're creating directory for
  51. * @ns: the namespace tag to use
  52. */
  53. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  54. {
  55. struct kernfs_node *parent, *kn;
  56. BUG_ON(!kobj);
  57. if (kobj->parent)
  58. parent = kobj->parent->sd;
  59. else
  60. parent = sysfs_root_kn;
  61. if (!parent)
  62. return -ENOENT;
  63. kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
  64. S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
  65. if (IS_ERR(kn)) {
  66. if (PTR_ERR(kn) == -EEXIST)
  67. sysfs_warn_dup(parent, kobject_name(kobj));
  68. return PTR_ERR(kn);
  69. }
  70. kobj->sd = kn;
  71. return 0;
  72. }
  73. /**
  74. * sysfs_remove_dir - remove an object's directory.
  75. * @kobj: object.
  76. *
  77. * The only thing special about this is that we remove any files in
  78. * the directory before we remove the directory, and we've inlined
  79. * what used to be sysfs_rmdir() below, instead of calling separately.
  80. */
  81. void sysfs_remove_dir(struct kobject *kobj)
  82. {
  83. struct kernfs_node *kn = kobj->sd;
  84. /*
  85. * In general, kboject owner is responsible for ensuring removal
  86. * doesn't race with other operations and sysfs doesn't provide any
  87. * protection; however, when @kobj is used as a symlink target, the
  88. * symlinking entity usually doesn't own @kobj and thus has no
  89. * control over removal. @kobj->sd may be removed anytime
  90. * and symlink code may end up dereferencing an already freed node.
  91. *
  92. * sysfs_symlink_target_lock synchronizes @kobj->sd
  93. * disassociation against symlink operations so that symlink code
  94. * can safely dereference @kobj->sd.
  95. */
  96. spin_lock(&sysfs_symlink_target_lock);
  97. kobj->sd = NULL;
  98. spin_unlock(&sysfs_symlink_target_lock);
  99. if (kn) {
  100. WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
  101. kernfs_remove(kn);
  102. }
  103. }
  104. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  105. const void *new_ns)
  106. {
  107. struct kernfs_node *parent = kobj->sd->parent;
  108. return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
  109. }
  110. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  111. const void *new_ns)
  112. {
  113. struct kernfs_node *kn = kobj->sd;
  114. struct kernfs_node *new_parent;
  115. BUG_ON(!kn->parent);
  116. new_parent = new_parent_kobj && new_parent_kobj->sd ?
  117. new_parent_kobj->sd : sysfs_root_kn;
  118. return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
  119. }