symlink.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * symlink.c - operations for sysfs symlinks.
  3. */
  4. #include <linux/fs.h>
  5. #include <linux/mount.h>
  6. #include <linux/module.h>
  7. #include <linux/kobject.h>
  8. #include <linux/namei.h>
  9. #include <asm/semaphore.h>
  10. #include "sysfs.h"
  11. static int object_depth(struct sysfs_dirent *sd)
  12. {
  13. int depth = 0;
  14. for (; sd->s_parent; sd = sd->s_parent)
  15. depth++;
  16. return depth;
  17. }
  18. static int object_path_length(struct sysfs_dirent * sd)
  19. {
  20. int length = 1;
  21. for (; sd->s_parent; sd = sd->s_parent)
  22. length += strlen(sd->s_name) + 1;
  23. return length;
  24. }
  25. static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
  26. {
  27. --length;
  28. for (; sd->s_parent; sd = sd->s_parent) {
  29. int cur = strlen(sd->s_name);
  30. /* back up enough to print this bus id with '/' */
  31. length -= cur;
  32. strncpy(buffer + length, sd->s_name, cur);
  33. *(buffer + --length) = '/';
  34. }
  35. }
  36. static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name,
  37. struct sysfs_dirent * target_sd)
  38. {
  39. struct sysfs_dirent * sd;
  40. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  41. if (!sd)
  42. return -ENOMEM;
  43. sd->s_elem.symlink.target_sd = target_sd;
  44. sysfs_attach_dirent(sd, parent_sd, NULL);
  45. return 0;
  46. }
  47. /**
  48. * sysfs_create_link - create symlink between two objects.
  49. * @kobj: object whose directory we're creating the link in.
  50. * @target: object we're pointing to.
  51. * @name: name of the symlink.
  52. */
  53. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  54. {
  55. struct sysfs_dirent *parent_sd = NULL;
  56. struct sysfs_dirent *target_sd = NULL;
  57. int error = -EEXIST;
  58. BUG_ON(!name);
  59. if (!kobj) {
  60. if (sysfs_mount && sysfs_mount->mnt_sb)
  61. parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
  62. } else
  63. parent_sd = kobj->sd;
  64. if (!parent_sd)
  65. return -EFAULT;
  66. /* target->sd can go away beneath us but is protected with
  67. * kobj_sysfs_assoc_lock. Fetch target_sd from it.
  68. */
  69. spin_lock(&kobj_sysfs_assoc_lock);
  70. if (target->sd)
  71. target_sd = sysfs_get(target->sd);
  72. spin_unlock(&kobj_sysfs_assoc_lock);
  73. if (!target_sd)
  74. return -ENOENT;
  75. mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
  76. if (!sysfs_find_dirent(parent_sd, name))
  77. error = sysfs_add_link(parent_sd, name, target_sd);
  78. mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
  79. if (error)
  80. sysfs_put(target_sd);
  81. return error;
  82. }
  83. /**
  84. * sysfs_remove_link - remove symlink in object's directory.
  85. * @kobj: object we're acting for.
  86. * @name: name of the symlink to remove.
  87. */
  88. void sysfs_remove_link(struct kobject * kobj, const char * name)
  89. {
  90. sysfs_hash_and_remove(kobj->sd, name);
  91. }
  92. static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
  93. struct sysfs_dirent * target_sd, char *path)
  94. {
  95. char * s;
  96. int depth, size;
  97. depth = object_depth(parent_sd);
  98. size = object_path_length(target_sd) + depth * 3 - 1;
  99. if (size > PATH_MAX)
  100. return -ENAMETOOLONG;
  101. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  102. for (s = path; depth--; s += 3)
  103. strcpy(s,"../");
  104. fill_object_path(target_sd, path, size);
  105. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  106. return 0;
  107. }
  108. static int sysfs_getlink(struct dentry *dentry, char * path)
  109. {
  110. struct sysfs_dirent *sd = dentry->d_fsdata;
  111. struct sysfs_dirent *parent_sd = sd->s_parent;
  112. struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
  113. int error;
  114. down_read(&sysfs_rename_sem);
  115. error = sysfs_get_target_path(parent_sd, target_sd, path);
  116. up_read(&sysfs_rename_sem);
  117. return error;
  118. }
  119. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  120. {
  121. int error = -ENOMEM;
  122. unsigned long page = get_zeroed_page(GFP_KERNEL);
  123. if (page)
  124. error = sysfs_getlink(dentry, (char *) page);
  125. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  126. return NULL;
  127. }
  128. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  129. {
  130. char *page = nd_get_link(nd);
  131. if (!IS_ERR(page))
  132. free_page((unsigned long)page);
  133. }
  134. const struct inode_operations sysfs_symlink_inode_operations = {
  135. .readlink = generic_readlink,
  136. .follow_link = sysfs_follow_link,
  137. .put_link = sysfs_put_link,
  138. };
  139. EXPORT_SYMBOL_GPL(sysfs_create_link);
  140. EXPORT_SYMBOL_GPL(sysfs_remove_link);