symlink.c 4.0 KB

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