symlink.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /**
  111. * sysfs_create_link_nowarn - create symlink between two objects.
  112. * @kobj: object whose directory we're creating the link in.
  113. * @target: object we're pointing to.
  114. * @name: name of the symlink.
  115. *
  116. * This function does the same as sysf_create_link(), but it
  117. * doesn't warn if the link already exists.
  118. */
  119. int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
  120. const char *name)
  121. {
  122. return sysfs_do_create_link(kobj, target, name, 0);
  123. }
  124. /**
  125. * sysfs_delete_link - remove symlink in object's directory.
  126. * @kobj: object we're acting for.
  127. * @targ: object we're pointing to.
  128. * @name: name of the symlink to remove.
  129. *
  130. * Unlike sysfs_remove_link sysfs_delete_link has enough information
  131. * to successfully delete symlinks in tagged directories.
  132. */
  133. void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
  134. const char *name)
  135. {
  136. const void *ns = NULL;
  137. spin_lock(&sysfs_assoc_lock);
  138. if (targ->sd && sysfs_ns_type(kobj->sd))
  139. ns = targ->sd->s_ns;
  140. spin_unlock(&sysfs_assoc_lock);
  141. sysfs_hash_and_remove(kobj->sd, ns, name);
  142. }
  143. /**
  144. * sysfs_remove_link - remove symlink in object's directory.
  145. * @kobj: object we're acting for.
  146. * @name: name of the symlink to remove.
  147. */
  148. void sysfs_remove_link(struct kobject * kobj, const char * name)
  149. {
  150. struct sysfs_dirent *parent_sd = NULL;
  151. if (!kobj)
  152. parent_sd = &sysfs_root;
  153. else
  154. parent_sd = kobj->sd;
  155. sysfs_hash_and_remove(parent_sd, NULL, name);
  156. }
  157. /**
  158. * sysfs_rename_link - rename symlink in object's directory.
  159. * @kobj: object we're acting for.
  160. * @targ: object we're pointing to.
  161. * @old: previous name of the symlink.
  162. * @new: new name of the symlink.
  163. *
  164. * A helper function for the common rename symlink idiom.
  165. */
  166. int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
  167. const char *old, const char *new)
  168. {
  169. struct sysfs_dirent *parent_sd, *sd = NULL;
  170. const void *old_ns = NULL, *new_ns = NULL;
  171. int result;
  172. if (!kobj)
  173. parent_sd = &sysfs_root;
  174. else
  175. parent_sd = kobj->sd;
  176. if (targ->sd)
  177. old_ns = targ->sd->s_ns;
  178. result = -ENOENT;
  179. sd = sysfs_get_dirent(parent_sd, old_ns, old);
  180. if (!sd)
  181. goto out;
  182. result = -EINVAL;
  183. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  184. goto out;
  185. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  186. goto out;
  187. if (sysfs_ns_type(parent_sd))
  188. new_ns = targ->ktype->namespace(targ);
  189. result = sysfs_rename(sd, parent_sd, new_ns, new);
  190. out:
  191. sysfs_put(sd);
  192. return result;
  193. }
  194. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  195. struct sysfs_dirent *target_sd, char *path)
  196. {
  197. struct sysfs_dirent *base, *sd;
  198. char *s = path;
  199. int len = 0;
  200. /* go up to the root, stop at the base */
  201. base = parent_sd;
  202. while (base->s_parent) {
  203. sd = target_sd->s_parent;
  204. while (sd->s_parent && base != sd)
  205. sd = sd->s_parent;
  206. if (base == sd)
  207. break;
  208. strcpy(s, "../");
  209. s += 3;
  210. base = base->s_parent;
  211. }
  212. /* determine end of target string for reverse fillup */
  213. sd = target_sd;
  214. while (sd->s_parent && sd != base) {
  215. len += strlen(sd->s_name) + 1;
  216. sd = sd->s_parent;
  217. }
  218. /* check limits */
  219. if (len < 2)
  220. return -EINVAL;
  221. len--;
  222. if ((s - path) + len > PATH_MAX)
  223. return -ENAMETOOLONG;
  224. /* reverse fillup of target string from target to base */
  225. sd = target_sd;
  226. while (sd->s_parent && sd != base) {
  227. int slen = strlen(sd->s_name);
  228. len -= slen;
  229. strncpy(s + len, sd->s_name, slen);
  230. if (len)
  231. s[--len] = '/';
  232. sd = sd->s_parent;
  233. }
  234. return 0;
  235. }
  236. static int sysfs_getlink(struct dentry *dentry, char * path)
  237. {
  238. struct sysfs_dirent *sd = dentry->d_fsdata;
  239. struct sysfs_dirent *parent_sd = sd->s_parent;
  240. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  241. int error;
  242. mutex_lock(&sysfs_mutex);
  243. error = sysfs_get_target_path(parent_sd, target_sd, path);
  244. mutex_unlock(&sysfs_mutex);
  245. return error;
  246. }
  247. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  248. {
  249. int error = -ENOMEM;
  250. unsigned long page = get_zeroed_page(GFP_KERNEL);
  251. if (page) {
  252. error = sysfs_getlink(dentry, (char *) page);
  253. if (error < 0)
  254. free_page((unsigned long)page);
  255. }
  256. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  257. return NULL;
  258. }
  259. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  260. {
  261. char *page = nd_get_link(nd);
  262. if (!IS_ERR(page))
  263. free_page((unsigned long)page);
  264. }
  265. const struct inode_operations sysfs_symlink_inode_operations = {
  266. .setxattr = sysfs_setxattr,
  267. .readlink = generic_readlink,
  268. .follow_link = sysfs_follow_link,
  269. .put_link = sysfs_put_link,
  270. .setattr = sysfs_setattr,
  271. .getattr = sysfs_getattr,
  272. .permission = sysfs_permission,
  273. };
  274. EXPORT_SYMBOL_GPL(sysfs_create_link);
  275. EXPORT_SYMBOL_GPL(sysfs_remove_link);
  276. EXPORT_SYMBOL_GPL(sysfs_rename_link);