symlink.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <linux/mutex.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. /**
  37. * sysfs_create_link - create symlink between two objects.
  38. * @kobj: object whose directory we're creating the link in.
  39. * @target: object we're pointing to.
  40. * @name: name of the symlink.
  41. */
  42. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  43. {
  44. struct sysfs_dirent *parent_sd = NULL;
  45. struct sysfs_dirent *target_sd = NULL;
  46. struct sysfs_dirent *sd = NULL;
  47. struct sysfs_addrm_cxt acxt;
  48. int error;
  49. BUG_ON(!name);
  50. if (!kobj)
  51. parent_sd = &sysfs_root;
  52. else
  53. parent_sd = kobj->sd;
  54. error = -EFAULT;
  55. if (!parent_sd)
  56. goto out_put;
  57. /* target->sd can go away beneath us but is protected with
  58. * sysfs_assoc_lock. Fetch target_sd from it.
  59. */
  60. spin_lock(&sysfs_assoc_lock);
  61. if (target->sd)
  62. target_sd = sysfs_get(target->sd);
  63. spin_unlock(&sysfs_assoc_lock);
  64. error = -ENOENT;
  65. if (!target_sd)
  66. goto out_put;
  67. error = -ENOMEM;
  68. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  69. if (!sd)
  70. goto out_put;
  71. sd->s_symlink.target_sd = target_sd;
  72. target_sd = NULL; /* reference is now owned by the symlink */
  73. sysfs_addrm_start(&acxt, parent_sd);
  74. error = sysfs_add_one(&acxt, sd);
  75. sysfs_addrm_finish(&acxt);
  76. if (error)
  77. goto out_put;
  78. return 0;
  79. out_put:
  80. sysfs_put(target_sd);
  81. sysfs_put(sd);
  82. return error;
  83. }
  84. /**
  85. * sysfs_remove_link - remove symlink in object's directory.
  86. * @kobj: object we're acting for.
  87. * @name: name of the symlink to remove.
  88. */
  89. void sysfs_remove_link(struct kobject * kobj, const char * name)
  90. {
  91. sysfs_hash_and_remove(kobj->sd, name);
  92. }
  93. static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
  94. struct sysfs_dirent * target_sd, char *path)
  95. {
  96. char * s;
  97. int depth, size;
  98. depth = object_depth(parent_sd);
  99. size = object_path_length(target_sd) + depth * 3 - 1;
  100. if (size > PATH_MAX)
  101. return -ENAMETOOLONG;
  102. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  103. for (s = path; depth--; s += 3)
  104. strcpy(s,"../");
  105. fill_object_path(target_sd, path, size);
  106. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  107. return 0;
  108. }
  109. static int sysfs_getlink(struct dentry *dentry, char * path)
  110. {
  111. struct sysfs_dirent *sd = dentry->d_fsdata;
  112. struct sysfs_dirent *parent_sd = sd->s_parent;
  113. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  114. int error;
  115. mutex_lock(&sysfs_mutex);
  116. error = sysfs_get_target_path(parent_sd, target_sd, path);
  117. mutex_unlock(&sysfs_mutex);
  118. return error;
  119. }
  120. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  121. {
  122. int error = -ENOMEM;
  123. unsigned long page = get_zeroed_page(GFP_KERNEL);
  124. if (page)
  125. error = sysfs_getlink(dentry, (char *) page);
  126. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  127. return NULL;
  128. }
  129. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  130. {
  131. char *page = nd_get_link(nd);
  132. if (!IS_ERR(page))
  133. free_page((unsigned long)page);
  134. }
  135. const struct inode_operations sysfs_symlink_inode_operations = {
  136. .readlink = generic_readlink,
  137. .follow_link = sysfs_follow_link,
  138. .put_link = sysfs_put_link,
  139. };
  140. EXPORT_SYMBOL_GPL(sysfs_create_link);
  141. EXPORT_SYMBOL_GPL(sysfs_remove_link);