symlink.c 7.1 KB

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