symlink.c 4.9 KB

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