symlink.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. enum kobj_ns_type ns_type;
  29. int error;
  30. BUG_ON(!name || !parent_sd);
  31. /* target->sd can go away beneath us but is protected with
  32. * sysfs_assoc_lock. Fetch target_sd from it.
  33. */
  34. spin_lock(&sysfs_assoc_lock);
  35. if (target->sd)
  36. target_sd = sysfs_get(target->sd);
  37. spin_unlock(&sysfs_assoc_lock);
  38. error = -ENOENT;
  39. if (!target_sd)
  40. goto out_put;
  41. error = -ENOMEM;
  42. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  43. if (!sd)
  44. goto out_put;
  45. ns_type = sysfs_ns_type(parent_sd);
  46. if (ns_type)
  47. sd->s_ns = target_sd->s_ns;
  48. sd->s_symlink.target_sd = target_sd;
  49. target_sd = NULL; /* reference is now owned by the symlink */
  50. sysfs_addrm_start(&acxt, parent_sd);
  51. /* Symlinks must be between directories with the same ns_type */
  52. if (!ns_type ||
  53. (ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent))) {
  54. if (warn)
  55. error = sysfs_add_one(&acxt, sd);
  56. else
  57. error = __sysfs_add_one(&acxt, sd);
  58. } else {
  59. error = -EINVAL;
  60. WARN(1, KERN_WARNING
  61. "sysfs: symlink across ns_types %s/%s -> %s/%s\n",
  62. parent_sd->s_name,
  63. sd->s_name,
  64. sd->s_symlink.target_sd->s_parent->s_name,
  65. sd->s_symlink.target_sd->s_name);
  66. }
  67. sysfs_addrm_finish(&acxt);
  68. if (error)
  69. goto out_put;
  70. return 0;
  71. out_put:
  72. sysfs_put(target_sd);
  73. sysfs_put(sd);
  74. return error;
  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. spin_lock(&sysfs_assoc_lock);
  139. if (targ->sd && sysfs_ns_type(kobj->sd))
  140. ns = targ->sd->s_ns;
  141. spin_unlock(&sysfs_assoc_lock);
  142. sysfs_hash_and_remove(kobj->sd, ns, name);
  143. }
  144. /**
  145. * sysfs_remove_link - remove symlink in object's directory.
  146. * @kobj: object we're acting for.
  147. * @name: name of the symlink to remove.
  148. */
  149. void sysfs_remove_link(struct kobject *kobj, const char *name)
  150. {
  151. struct sysfs_dirent *parent_sd = NULL;
  152. if (!kobj)
  153. parent_sd = &sysfs_root;
  154. else
  155. parent_sd = kobj->sd;
  156. sysfs_hash_and_remove(parent_sd, NULL, name);
  157. }
  158. EXPORT_SYMBOL_GPL(sysfs_remove_link);
  159. /**
  160. * sysfs_rename_link_ns - rename symlink in object's directory.
  161. * @kobj: object we're acting for.
  162. * @targ: object we're pointing to.
  163. * @old: previous name of the symlink.
  164. * @new: new name of the symlink.
  165. * @new_ns: new namespace of the symlink.
  166. *
  167. * A helper function for the common rename symlink idiom.
  168. */
  169. int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
  170. const char *old, const char *new, const void *new_ns)
  171. {
  172. struct sysfs_dirent *parent_sd, *sd = NULL;
  173. const void *old_ns = NULL;
  174. int result;
  175. if (!kobj)
  176. parent_sd = &sysfs_root;
  177. else
  178. parent_sd = kobj->sd;
  179. if (targ->sd)
  180. old_ns = targ->sd->s_ns;
  181. result = -ENOENT;
  182. sd = sysfs_get_dirent(parent_sd, old_ns, old);
  183. if (!sd)
  184. goto out;
  185. result = -EINVAL;
  186. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  187. goto out;
  188. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  189. goto out;
  190. result = sysfs_rename(sd, parent_sd, new_ns, new);
  191. out:
  192. sysfs_put(sd);
  193. return result;
  194. }
  195. EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
  196. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  197. struct sysfs_dirent *target_sd, char *path)
  198. {
  199. struct sysfs_dirent *base, *sd;
  200. char *s = path;
  201. int len = 0;
  202. /* go up to the root, stop at the base */
  203. base = parent_sd;
  204. while (base->s_parent) {
  205. sd = target_sd->s_parent;
  206. while (sd->s_parent && base != sd)
  207. sd = sd->s_parent;
  208. if (base == sd)
  209. break;
  210. strcpy(s, "../");
  211. s += 3;
  212. base = base->s_parent;
  213. }
  214. /* determine end of target string for reverse fillup */
  215. sd = target_sd;
  216. while (sd->s_parent && sd != base) {
  217. len += strlen(sd->s_name) + 1;
  218. sd = sd->s_parent;
  219. }
  220. /* check limits */
  221. if (len < 2)
  222. return -EINVAL;
  223. len--;
  224. if ((s - path) + len > PATH_MAX)
  225. return -ENAMETOOLONG;
  226. /* reverse fillup of target string from target to base */
  227. sd = target_sd;
  228. while (sd->s_parent && sd != base) {
  229. int slen = strlen(sd->s_name);
  230. len -= slen;
  231. strncpy(s + len, sd->s_name, slen);
  232. if (len)
  233. s[--len] = '/';
  234. sd = sd->s_parent;
  235. }
  236. return 0;
  237. }
  238. static int sysfs_getlink(struct dentry *dentry, char *path)
  239. {
  240. struct sysfs_dirent *sd = dentry->d_fsdata;
  241. struct sysfs_dirent *parent_sd = sd->s_parent;
  242. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  243. int error;
  244. mutex_lock(&sysfs_mutex);
  245. error = sysfs_get_target_path(parent_sd, target_sd, path);
  246. mutex_unlock(&sysfs_mutex);
  247. return error;
  248. }
  249. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  250. {
  251. int error = -ENOMEM;
  252. unsigned long page = get_zeroed_page(GFP_KERNEL);
  253. if (page) {
  254. error = sysfs_getlink(dentry, (char *) page);
  255. if (error < 0)
  256. free_page((unsigned long)page);
  257. }
  258. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  259. return NULL;
  260. }
  261. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
  262. void *cookie)
  263. {
  264. char *page = nd_get_link(nd);
  265. if (!IS_ERR(page))
  266. free_page((unsigned long)page);
  267. }
  268. const struct inode_operations sysfs_symlink_inode_operations = {
  269. .setxattr = sysfs_setxattr,
  270. .readlink = generic_readlink,
  271. .follow_link = sysfs_follow_link,
  272. .put_link = sysfs_put_link,
  273. .setattr = sysfs_setattr,
  274. .getattr = sysfs_getattr,
  275. .permission = sysfs_permission,
  276. };