symlink.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * fs/sysfs/symlink.c - sysfs symlink 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. #include <linux/fs.h>
  13. #include <linux/mount.h>
  14. #include <linux/module.h>
  15. #include <linux/kobject.h>
  16. #include <linux/namei.h>
  17. #include <linux/mutex.h>
  18. #include "sysfs.h"
  19. /**
  20. * sysfs_create_link - create symlink between two objects.
  21. * @kobj: object whose directory we're creating the link in.
  22. * @target: object we're pointing to.
  23. * @name: name of the symlink.
  24. */
  25. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  26. {
  27. struct sysfs_dirent *parent_sd = NULL;
  28. struct sysfs_dirent *target_sd = NULL;
  29. struct sysfs_dirent *sd = NULL;
  30. struct sysfs_addrm_cxt acxt;
  31. int error;
  32. BUG_ON(!name);
  33. if (!kobj)
  34. parent_sd = &sysfs_root;
  35. else
  36. parent_sd = kobj->sd;
  37. error = -EFAULT;
  38. if (!parent_sd)
  39. goto out_put;
  40. /* target->sd can go away beneath us but is protected with
  41. * sysfs_assoc_lock. Fetch target_sd from it.
  42. */
  43. spin_lock(&sysfs_assoc_lock);
  44. if (target->sd)
  45. target_sd = sysfs_get(target->sd);
  46. spin_unlock(&sysfs_assoc_lock);
  47. error = -ENOENT;
  48. if (!target_sd)
  49. goto out_put;
  50. error = -ENOMEM;
  51. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  52. if (!sd)
  53. goto out_put;
  54. sd->s_symlink.target_sd = target_sd;
  55. target_sd = NULL; /* reference is now owned by the symlink */
  56. sysfs_addrm_start(&acxt, parent_sd);
  57. error = sysfs_add_one(&acxt, sd);
  58. sysfs_addrm_finish(&acxt);
  59. if (error)
  60. goto out_put;
  61. return 0;
  62. out_put:
  63. sysfs_put(target_sd);
  64. sysfs_put(sd);
  65. return error;
  66. }
  67. /**
  68. * sysfs_remove_link - remove symlink in object's directory.
  69. * @kobj: object we're acting for.
  70. * @name: name of the symlink to remove.
  71. */
  72. void sysfs_remove_link(struct kobject * kobj, const char * name)
  73. {
  74. sysfs_hash_and_remove(kobj->sd, name);
  75. }
  76. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  77. struct sysfs_dirent *target_sd, char *path)
  78. {
  79. struct sysfs_dirent *base, *sd;
  80. char *s = path;
  81. int len = 0;
  82. /* go up to the root, stop at the base */
  83. base = parent_sd;
  84. while (base->s_parent) {
  85. sd = target_sd->s_parent;
  86. while (sd->s_parent && base != sd)
  87. sd = sd->s_parent;
  88. if (base == sd)
  89. break;
  90. strcpy(s, "../");
  91. s += 3;
  92. base = base->s_parent;
  93. }
  94. /* determine end of target string for reverse fillup */
  95. sd = target_sd;
  96. while (sd->s_parent && sd != base) {
  97. len += strlen(sd->s_name) + 1;
  98. sd = sd->s_parent;
  99. }
  100. /* check limits */
  101. if (len < 2)
  102. return -EINVAL;
  103. len--;
  104. if ((s - path) + len > PATH_MAX)
  105. return -ENAMETOOLONG;
  106. /* reverse fillup of target string from target to base */
  107. sd = target_sd;
  108. while (sd->s_parent && sd != base) {
  109. int slen = strlen(sd->s_name);
  110. len -= slen;
  111. strncpy(s + len, sd->s_name, slen);
  112. if (len)
  113. s[--len] = '/';
  114. sd = sd->s_parent;
  115. }
  116. return 0;
  117. }
  118. static int sysfs_getlink(struct dentry *dentry, char * path)
  119. {
  120. struct sysfs_dirent *sd = dentry->d_fsdata;
  121. struct sysfs_dirent *parent_sd = sd->s_parent;
  122. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  123. int error;
  124. mutex_lock(&sysfs_mutex);
  125. error = sysfs_get_target_path(parent_sd, target_sd, path);
  126. mutex_unlock(&sysfs_mutex);
  127. return error;
  128. }
  129. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  130. {
  131. int error = -ENOMEM;
  132. unsigned long page = get_zeroed_page(GFP_KERNEL);
  133. if (page)
  134. error = sysfs_getlink(dentry, (char *) page);
  135. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  136. return NULL;
  137. }
  138. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  139. {
  140. char *page = nd_get_link(nd);
  141. if (!IS_ERR(page))
  142. free_page((unsigned long)page);
  143. }
  144. const struct inode_operations sysfs_symlink_inode_operations = {
  145. .readlink = generic_readlink,
  146. .follow_link = sysfs_follow_link,
  147. .put_link = sysfs_put_link,
  148. };
  149. EXPORT_SYMBOL_GPL(sysfs_create_link);
  150. EXPORT_SYMBOL_GPL(sysfs_remove_link);