symlink.c 7.4 KB

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