symlink.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  106. * sysfs_rename_link - rename symlink in object's directory.
  107. * @kobj: object we're acting for.
  108. * @targ: object we're pointing to.
  109. * @old: previous name of the symlink.
  110. * @new: new name of the symlink.
  111. *
  112. * A helper function for the common rename symlink idiom.
  113. */
  114. int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
  115. const char *old, const char *new)
  116. {
  117. struct sysfs_dirent *parent_sd, *sd = NULL;
  118. int result;
  119. if (!kobj)
  120. parent_sd = &sysfs_root;
  121. else
  122. parent_sd = kobj->sd;
  123. result = -ENOENT;
  124. sd = sysfs_get_dirent(parent_sd, old);
  125. if (!sd)
  126. goto out;
  127. result = -EINVAL;
  128. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  129. goto out;
  130. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  131. goto out;
  132. result = sysfs_rename(sd, parent_sd, new);
  133. out:
  134. sysfs_put(sd);
  135. return result;
  136. }
  137. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  138. struct sysfs_dirent *target_sd, char *path)
  139. {
  140. struct sysfs_dirent *base, *sd;
  141. char *s = path;
  142. int len = 0;
  143. /* go up to the root, stop at the base */
  144. base = parent_sd;
  145. while (base->s_parent) {
  146. sd = target_sd->s_parent;
  147. while (sd->s_parent && base != sd)
  148. sd = sd->s_parent;
  149. if (base == sd)
  150. break;
  151. strcpy(s, "../");
  152. s += 3;
  153. base = base->s_parent;
  154. }
  155. /* determine end of target string for reverse fillup */
  156. sd = target_sd;
  157. while (sd->s_parent && sd != base) {
  158. len += strlen(sd->s_name) + 1;
  159. sd = sd->s_parent;
  160. }
  161. /* check limits */
  162. if (len < 2)
  163. return -EINVAL;
  164. len--;
  165. if ((s - path) + len > PATH_MAX)
  166. return -ENAMETOOLONG;
  167. /* reverse fillup of target string from target to base */
  168. sd = target_sd;
  169. while (sd->s_parent && sd != base) {
  170. int slen = strlen(sd->s_name);
  171. len -= slen;
  172. strncpy(s + len, sd->s_name, slen);
  173. if (len)
  174. s[--len] = '/';
  175. sd = sd->s_parent;
  176. }
  177. return 0;
  178. }
  179. static int sysfs_getlink(struct dentry *dentry, char * path)
  180. {
  181. struct sysfs_dirent *sd = dentry->d_fsdata;
  182. struct sysfs_dirent *parent_sd = sd->s_parent;
  183. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  184. int error;
  185. mutex_lock(&sysfs_mutex);
  186. error = sysfs_get_target_path(parent_sd, target_sd, path);
  187. mutex_unlock(&sysfs_mutex);
  188. return error;
  189. }
  190. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  191. {
  192. int error = -ENOMEM;
  193. unsigned long page = get_zeroed_page(GFP_KERNEL);
  194. if (page) {
  195. error = sysfs_getlink(dentry, (char *) page);
  196. if (error < 0)
  197. free_page((unsigned long)page);
  198. }
  199. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  200. return NULL;
  201. }
  202. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  203. {
  204. char *page = nd_get_link(nd);
  205. if (!IS_ERR(page))
  206. free_page((unsigned long)page);
  207. }
  208. const struct inode_operations sysfs_symlink_inode_operations = {
  209. .setxattr = sysfs_setxattr,
  210. .readlink = generic_readlink,
  211. .follow_link = sysfs_follow_link,
  212. .put_link = sysfs_put_link,
  213. .setattr = sysfs_setattr,
  214. .getattr = sysfs_getattr,
  215. .permission = sysfs_permission,
  216. };
  217. EXPORT_SYMBOL_GPL(sysfs_create_link);
  218. EXPORT_SYMBOL_GPL(sysfs_remove_link);