symlink.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. target_sd = NULL; /* reference is now owned by the symlink */
  74. sysfs_addrm_start(&acxt, parent_sd);
  75. if (!sysfs_find_dirent(parent_sd, name)) {
  76. sysfs_add_one(&acxt, sd);
  77. sysfs_link_sibling(sd);
  78. }
  79. if (!sysfs_addrm_finish(&acxt)) {
  80. error = -EEXIST;
  81. goto out_put;
  82. }
  83. return 0;
  84. out_put:
  85. sysfs_put(target_sd);
  86. sysfs_put(sd);
  87. return error;
  88. }
  89. /**
  90. * sysfs_remove_link - remove symlink in object's directory.
  91. * @kobj: object we're acting for.
  92. * @name: name of the symlink to remove.
  93. */
  94. void sysfs_remove_link(struct kobject * kobj, const char * name)
  95. {
  96. sysfs_hash_and_remove(kobj->sd, name);
  97. }
  98. static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
  99. struct sysfs_dirent * target_sd, char *path)
  100. {
  101. char * s;
  102. int depth, size;
  103. depth = object_depth(parent_sd);
  104. size = object_path_length(target_sd) + depth * 3 - 1;
  105. if (size > PATH_MAX)
  106. return -ENAMETOOLONG;
  107. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  108. for (s = path; depth--; s += 3)
  109. strcpy(s,"../");
  110. fill_object_path(target_sd, path, size);
  111. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  112. return 0;
  113. }
  114. static int sysfs_getlink(struct dentry *dentry, char * path)
  115. {
  116. struct sysfs_dirent *sd = dentry->d_fsdata;
  117. struct sysfs_dirent *parent_sd = sd->s_parent;
  118. struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
  119. int error;
  120. mutex_lock(&sysfs_mutex);
  121. error = sysfs_get_target_path(parent_sd, target_sd, path);
  122. mutex_unlock(&sysfs_mutex);
  123. return error;
  124. }
  125. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  126. {
  127. int error = -ENOMEM;
  128. unsigned long page = get_zeroed_page(GFP_KERNEL);
  129. if (page)
  130. error = sysfs_getlink(dentry, (char *) page);
  131. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  132. return NULL;
  133. }
  134. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  135. {
  136. char *page = nd_get_link(nd);
  137. if (!IS_ERR(page))
  138. free_page((unsigned long)page);
  139. }
  140. const struct inode_operations sysfs_symlink_inode_operations = {
  141. .readlink = generic_readlink,
  142. .follow_link = sysfs_follow_link,
  143. .put_link = sysfs_put_link,
  144. };
  145. EXPORT_SYMBOL_GPL(sysfs_create_link);
  146. EXPORT_SYMBOL_GPL(sysfs_remove_link);