symlink.c 7.9 KB

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