symlink.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/gfp.h>
  14. #include <linux/mount.h>
  15. #include <linux/module.h>
  16. #include <linux/kobject.h>
  17. #include <linux/namei.h>
  18. #include <linux/mutex.h>
  19. #include <linux/security.h>
  20. #include "sysfs.h"
  21. static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
  22. const char *name, int warn)
  23. {
  24. struct sysfs_dirent *parent_sd = NULL;
  25. struct sysfs_dirent *target_sd = NULL;
  26. struct sysfs_dirent *sd = NULL;
  27. struct sysfs_addrm_cxt acxt;
  28. int error;
  29. BUG_ON(!name);
  30. if (!kobj)
  31. parent_sd = &sysfs_root;
  32. else
  33. parent_sd = kobj->sd;
  34. error = -EFAULT;
  35. if (!parent_sd)
  36. goto out_put;
  37. /* target->sd can go away beneath us but is protected with
  38. * sysfs_assoc_lock. Fetch target_sd from it.
  39. */
  40. spin_lock(&sysfs_assoc_lock);
  41. if (target->sd)
  42. target_sd = sysfs_get(target->sd);
  43. spin_unlock(&sysfs_assoc_lock);
  44. error = -ENOENT;
  45. if (!target_sd)
  46. goto out_put;
  47. error = -ENOMEM;
  48. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  49. if (!sd)
  50. goto out_put;
  51. if (sysfs_ns_type(parent_sd))
  52. sd->s_ns = target->ktype->namespace(target);
  53. sd->s_symlink.target_sd = target_sd;
  54. target_sd = NULL; /* reference is now owned by the symlink */
  55. sysfs_addrm_start(&acxt, parent_sd);
  56. if (warn)
  57. error = sysfs_add_one(&acxt, sd);
  58. else
  59. error = __sysfs_add_one(&acxt, sd);
  60. sysfs_addrm_finish(&acxt);
  61. if (error)
  62. goto out_put;
  63. return 0;
  64. out_put:
  65. sysfs_put(target_sd);
  66. sysfs_put(sd);
  67. return error;
  68. }
  69. /**
  70. * sysfs_create_link - create symlink between two objects.
  71. * @kobj: object whose directory we're creating the link in.
  72. * @target: object we're pointing to.
  73. * @name: name of the symlink.
  74. */
  75. int sysfs_create_link(struct kobject *kobj, struct kobject *target,
  76. const char *name)
  77. {
  78. return sysfs_do_create_link(kobj, target, name, 1);
  79. }
  80. /**
  81. * sysfs_create_link_nowarn - create symlink between two objects.
  82. * @kobj: object whose directory we're creating the link in.
  83. * @target: object we're pointing to.
  84. * @name: name of the symlink.
  85. *
  86. * This function does the same as sysf_create_link(), but it
  87. * doesn't warn if the link already exists.
  88. */
  89. int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
  90. const char *name)
  91. {
  92. return sysfs_do_create_link(kobj, target, name, 0);
  93. }
  94. /**
  95. * sysfs_delete_link - remove symlink in object's directory.
  96. * @kobj: object we're acting for.
  97. * @targ: object we're pointing to.
  98. * @name: name of the symlink to remove.
  99. *
  100. * Unlike sysfs_remove_link sysfs_delete_link has enough information
  101. * to successfully delete symlinks in tagged directories.
  102. */
  103. void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
  104. const char *name)
  105. {
  106. const void *ns = NULL;
  107. spin_lock(&sysfs_assoc_lock);
  108. if (targ->sd)
  109. ns = targ->sd->s_ns;
  110. spin_unlock(&sysfs_assoc_lock);
  111. sysfs_hash_and_remove(kobj->sd, ns, name);
  112. }
  113. /**
  114. * sysfs_remove_link - remove symlink in object's directory.
  115. * @kobj: object we're acting for.
  116. * @name: name of the symlink to remove.
  117. */
  118. void sysfs_remove_link(struct kobject * kobj, const char * name)
  119. {
  120. struct sysfs_dirent *parent_sd = NULL;
  121. if (!kobj)
  122. parent_sd = &sysfs_root;
  123. else
  124. parent_sd = kobj->sd;
  125. sysfs_hash_and_remove(parent_sd, NULL, name);
  126. }
  127. /**
  128. * sysfs_rename_link - rename symlink in object's directory.
  129. * @kobj: object we're acting for.
  130. * @targ: object we're pointing to.
  131. * @old: previous name of the symlink.
  132. * @new: new name of the symlink.
  133. *
  134. * A helper function for the common rename symlink idiom.
  135. */
  136. int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
  137. const char *old, const char *new)
  138. {
  139. struct sysfs_dirent *parent_sd, *sd = NULL;
  140. const void *old_ns = NULL, *new_ns = NULL;
  141. int result;
  142. if (!kobj)
  143. parent_sd = &sysfs_root;
  144. else
  145. parent_sd = kobj->sd;
  146. if (targ->sd)
  147. old_ns = targ->sd->s_ns;
  148. result = -ENOENT;
  149. sd = sysfs_get_dirent(parent_sd, old_ns, old);
  150. if (!sd)
  151. goto out;
  152. result = -EINVAL;
  153. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  154. goto out;
  155. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  156. goto out;
  157. if (sysfs_ns_type(parent_sd))
  158. new_ns = targ->ktype->namespace(targ);
  159. result = sysfs_rename(sd, parent_sd, new_ns, new);
  160. out:
  161. sysfs_put(sd);
  162. return result;
  163. }
  164. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  165. struct sysfs_dirent *target_sd, char *path)
  166. {
  167. struct sysfs_dirent *base, *sd;
  168. char *s = path;
  169. int len = 0;
  170. /* go up to the root, stop at the base */
  171. base = parent_sd;
  172. while (base->s_parent) {
  173. sd = target_sd->s_parent;
  174. while (sd->s_parent && base != sd)
  175. sd = sd->s_parent;
  176. if (base == sd)
  177. break;
  178. strcpy(s, "../");
  179. s += 3;
  180. base = base->s_parent;
  181. }
  182. /* determine end of target string for reverse fillup */
  183. sd = target_sd;
  184. while (sd->s_parent && sd != base) {
  185. len += strlen(sd->s_name) + 1;
  186. sd = sd->s_parent;
  187. }
  188. /* check limits */
  189. if (len < 2)
  190. return -EINVAL;
  191. len--;
  192. if ((s - path) + len > PATH_MAX)
  193. return -ENAMETOOLONG;
  194. /* reverse fillup of target string from target to base */
  195. sd = target_sd;
  196. while (sd->s_parent && sd != base) {
  197. int slen = strlen(sd->s_name);
  198. len -= slen;
  199. strncpy(s + len, sd->s_name, slen);
  200. if (len)
  201. s[--len] = '/';
  202. sd = sd->s_parent;
  203. }
  204. return 0;
  205. }
  206. static int sysfs_getlink(struct dentry *dentry, char * path)
  207. {
  208. struct sysfs_dirent *sd = dentry->d_fsdata;
  209. struct sysfs_dirent *parent_sd = sd->s_parent;
  210. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  211. int error;
  212. mutex_lock(&sysfs_mutex);
  213. error = sysfs_get_target_path(parent_sd, target_sd, path);
  214. mutex_unlock(&sysfs_mutex);
  215. return error;
  216. }
  217. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  218. {
  219. int error = -ENOMEM;
  220. unsigned long page = get_zeroed_page(GFP_KERNEL);
  221. if (page) {
  222. error = sysfs_getlink(dentry, (char *) page);
  223. if (error < 0)
  224. free_page((unsigned long)page);
  225. }
  226. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  227. return NULL;
  228. }
  229. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  230. {
  231. char *page = nd_get_link(nd);
  232. if (!IS_ERR(page))
  233. free_page((unsigned long)page);
  234. }
  235. const struct inode_operations sysfs_symlink_inode_operations = {
  236. .setxattr = sysfs_setxattr,
  237. .readlink = generic_readlink,
  238. .follow_link = sysfs_follow_link,
  239. .put_link = sysfs_put_link,
  240. .setattr = sysfs_setattr,
  241. .getattr = sysfs_getattr,
  242. .permission = sysfs_permission,
  243. };
  244. EXPORT_SYMBOL_GPL(sysfs_create_link);
  245. EXPORT_SYMBOL_GPL(sysfs_remove_link);
  246. EXPORT_SYMBOL_GPL(sysfs_rename_link);