symlink.c 7.9 KB

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