symlink.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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->ktype->namespace(target);
  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 - 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. *
  166. * A helper function for the common rename symlink idiom.
  167. */
  168. int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
  169. const char *old, const char *new)
  170. {
  171. struct sysfs_dirent *parent_sd, *sd = NULL;
  172. const void *old_ns = NULL, *new_ns = NULL;
  173. int result;
  174. if (!kobj)
  175. parent_sd = &sysfs_root;
  176. else
  177. parent_sd = kobj->sd;
  178. if (targ->sd)
  179. old_ns = targ->sd->s_ns;
  180. result = -ENOENT;
  181. sd = sysfs_get_dirent(parent_sd, old_ns, old);
  182. if (!sd)
  183. goto out;
  184. result = -EINVAL;
  185. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  186. goto out;
  187. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  188. goto out;
  189. if (sysfs_ns_type(parent_sd))
  190. new_ns = targ->ktype->namespace(targ);
  191. result = sysfs_rename(sd, parent_sd, new_ns, new);
  192. out:
  193. sysfs_put(sd);
  194. return result;
  195. }
  196. EXPORT_SYMBOL_GPL(sysfs_rename_link);
  197. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  198. struct sysfs_dirent *target_sd, char *path)
  199. {
  200. struct sysfs_dirent *base, *sd;
  201. char *s = path;
  202. int len = 0;
  203. /* go up to the root, stop at the base */
  204. base = parent_sd;
  205. while (base->s_parent) {
  206. sd = target_sd->s_parent;
  207. while (sd->s_parent && base != sd)
  208. sd = sd->s_parent;
  209. if (base == sd)
  210. break;
  211. strcpy(s, "../");
  212. s += 3;
  213. base = base->s_parent;
  214. }
  215. /* determine end of target string for reverse fillup */
  216. sd = target_sd;
  217. while (sd->s_parent && sd != base) {
  218. len += strlen(sd->s_name) + 1;
  219. sd = sd->s_parent;
  220. }
  221. /* check limits */
  222. if (len < 2)
  223. return -EINVAL;
  224. len--;
  225. if ((s - path) + len > PATH_MAX)
  226. return -ENAMETOOLONG;
  227. /* reverse fillup of target string from target to base */
  228. sd = target_sd;
  229. while (sd->s_parent && sd != base) {
  230. int slen = strlen(sd->s_name);
  231. len -= slen;
  232. strncpy(s + len, sd->s_name, slen);
  233. if (len)
  234. s[--len] = '/';
  235. sd = sd->s_parent;
  236. }
  237. return 0;
  238. }
  239. static int sysfs_getlink(struct dentry *dentry, char * path)
  240. {
  241. struct sysfs_dirent *sd = dentry->d_fsdata;
  242. struct sysfs_dirent *parent_sd = sd->s_parent;
  243. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  244. int error;
  245. mutex_lock(&sysfs_mutex);
  246. error = sysfs_get_target_path(parent_sd, target_sd, path);
  247. mutex_unlock(&sysfs_mutex);
  248. return error;
  249. }
  250. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  251. {
  252. int error = -ENOMEM;
  253. unsigned long page = get_zeroed_page(GFP_KERNEL);
  254. if (page) {
  255. error = sysfs_getlink(dentry, (char *) page);
  256. if (error < 0)
  257. free_page((unsigned long)page);
  258. }
  259. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  260. return NULL;
  261. }
  262. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, 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. };