symlink.c 4.9 KB

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