symlink.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. struct sysfs_dirent *parent_sd = NULL;
  75. if (!kobj)
  76. parent_sd = &sysfs_root;
  77. else
  78. parent_sd = kobj->sd;
  79. sysfs_hash_and_remove(parent_sd, name);
  80. }
  81. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  82. struct sysfs_dirent *target_sd, char *path)
  83. {
  84. struct sysfs_dirent *base, *sd;
  85. char *s = path;
  86. int len = 0;
  87. /* go up to the root, stop at the base */
  88. base = parent_sd;
  89. while (base->s_parent) {
  90. sd = target_sd->s_parent;
  91. while (sd->s_parent && base != sd)
  92. sd = sd->s_parent;
  93. if (base == sd)
  94. break;
  95. strcpy(s, "../");
  96. s += 3;
  97. base = base->s_parent;
  98. }
  99. /* determine end of target string for reverse fillup */
  100. sd = target_sd;
  101. while (sd->s_parent && sd != base) {
  102. len += strlen(sd->s_name) + 1;
  103. sd = sd->s_parent;
  104. }
  105. /* check limits */
  106. if (len < 2)
  107. return -EINVAL;
  108. len--;
  109. if ((s - path) + len > PATH_MAX)
  110. return -ENAMETOOLONG;
  111. /* reverse fillup of target string from target to base */
  112. sd = target_sd;
  113. while (sd->s_parent && sd != base) {
  114. int slen = strlen(sd->s_name);
  115. len -= slen;
  116. strncpy(s + len, sd->s_name, slen);
  117. if (len)
  118. s[--len] = '/';
  119. sd = sd->s_parent;
  120. }
  121. return 0;
  122. }
  123. static int sysfs_getlink(struct dentry *dentry, char * path)
  124. {
  125. struct sysfs_dirent *sd = dentry->d_fsdata;
  126. struct sysfs_dirent *parent_sd = sd->s_parent;
  127. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  128. int error;
  129. mutex_lock(&sysfs_mutex);
  130. error = sysfs_get_target_path(parent_sd, target_sd, path);
  131. mutex_unlock(&sysfs_mutex);
  132. return error;
  133. }
  134. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  135. {
  136. int error = -ENOMEM;
  137. unsigned long page = get_zeroed_page(GFP_KERNEL);
  138. if (page)
  139. error = sysfs_getlink(dentry, (char *) page);
  140. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  141. return NULL;
  142. }
  143. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  144. {
  145. char *page = nd_get_link(nd);
  146. if (!IS_ERR(page))
  147. free_page((unsigned long)page);
  148. }
  149. const struct inode_operations sysfs_symlink_inode_operations = {
  150. .readlink = generic_readlink,
  151. .follow_link = sysfs_follow_link,
  152. .put_link = sysfs_put_link,
  153. };
  154. EXPORT_SYMBOL_GPL(sysfs_create_link);
  155. EXPORT_SYMBOL_GPL(sysfs_remove_link);