symlink.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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(struct kobject *kobj, struct kobject *target,
  22. const char *name, int warn)
  23. {
  24. struct sysfs_dirent *parent_sd = NULL;
  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);
  30. if (!kobj)
  31. parent_sd = &sysfs_root;
  32. else
  33. parent_sd = kobj->sd;
  34. error = -EFAULT;
  35. if (!parent_sd)
  36. goto out_put;
  37. /* target->sd can go away beneath us but is protected with
  38. * sysfs_assoc_lock. Fetch target_sd from it.
  39. */
  40. spin_lock(&sysfs_assoc_lock);
  41. if (target->sd)
  42. target_sd = sysfs_get(target->sd);
  43. spin_unlock(&sysfs_assoc_lock);
  44. error = -ENOENT;
  45. if (!target_sd)
  46. goto out_put;
  47. error = -ENOMEM;
  48. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  49. if (!sd)
  50. goto out_put;
  51. sd->s_symlink.target_sd = target_sd;
  52. target_sd = NULL; /* reference is now owned by the symlink */
  53. sysfs_addrm_start(&acxt, parent_sd);
  54. if (warn)
  55. error = sysfs_add_one(&acxt, sd);
  56. else
  57. error = __sysfs_add_one(&acxt, sd);
  58. sysfs_addrm_finish(&acxt);
  59. if (error)
  60. goto out_put;
  61. return 0;
  62. out_put:
  63. sysfs_put(target_sd);
  64. sysfs_put(sd);
  65. return error;
  66. }
  67. /**
  68. * sysfs_create_link - create symlink between two objects.
  69. * @kobj: object whose directory we're creating the link in.
  70. * @target: object we're pointing to.
  71. * @name: name of the symlink.
  72. */
  73. int sysfs_create_link(struct kobject *kobj, struct kobject *target,
  74. const char *name)
  75. {
  76. return sysfs_do_create_link(kobj, target, name, 1);
  77. }
  78. /**
  79. * sysfs_create_link_nowarn - create symlink between two objects.
  80. * @kobj: object whose directory we're creating the link in.
  81. * @target: object we're pointing to.
  82. * @name: name of the symlink.
  83. *
  84. * This function does the same as sysf_create_link(), but it
  85. * doesn't warn if the link already exists.
  86. */
  87. int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
  88. const char *name)
  89. {
  90. return sysfs_do_create_link(kobj, target, name, 0);
  91. }
  92. /**
  93. * sysfs_remove_link - remove symlink in object's directory.
  94. * @kobj: object we're acting for.
  95. * @name: name of the symlink to remove.
  96. */
  97. void sysfs_remove_link(struct kobject * kobj, const char * name)
  98. {
  99. struct sysfs_dirent *parent_sd = NULL;
  100. if (!kobj)
  101. parent_sd = &sysfs_root;
  102. else
  103. parent_sd = kobj->sd;
  104. sysfs_hash_and_remove(parent_sd, name);
  105. }
  106. /**
  107. * sysfs_rename_link - rename symlink in object's directory.
  108. * @kobj: object we're acting for.
  109. * @targ: object we're pointing to.
  110. * @old: previous name of the symlink.
  111. * @new: new name of the symlink.
  112. *
  113. * A helper function for the common rename symlink idiom.
  114. */
  115. int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
  116. const char *old, const char *new)
  117. {
  118. struct sysfs_dirent *parent_sd, *sd = NULL;
  119. int result;
  120. if (!kobj)
  121. parent_sd = &sysfs_root;
  122. else
  123. parent_sd = kobj->sd;
  124. result = -ENOENT;
  125. sd = sysfs_get_dirent(parent_sd, old);
  126. if (!sd)
  127. goto out;
  128. result = -EINVAL;
  129. if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
  130. goto out;
  131. if (sd->s_symlink.target_sd->s_dir.kobj != targ)
  132. goto out;
  133. result = sysfs_rename(sd, parent_sd, new);
  134. out:
  135. sysfs_put(sd);
  136. return result;
  137. }
  138. static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
  139. struct sysfs_dirent *target_sd, char *path)
  140. {
  141. struct sysfs_dirent *base, *sd;
  142. char *s = path;
  143. int len = 0;
  144. /* go up to the root, stop at the base */
  145. base = parent_sd;
  146. while (base->s_parent) {
  147. sd = target_sd->s_parent;
  148. while (sd->s_parent && base != sd)
  149. sd = sd->s_parent;
  150. if (base == sd)
  151. break;
  152. strcpy(s, "../");
  153. s += 3;
  154. base = base->s_parent;
  155. }
  156. /* determine end of target string for reverse fillup */
  157. sd = target_sd;
  158. while (sd->s_parent && sd != base) {
  159. len += strlen(sd->s_name) + 1;
  160. sd = sd->s_parent;
  161. }
  162. /* check limits */
  163. if (len < 2)
  164. return -EINVAL;
  165. len--;
  166. if ((s - path) + len > PATH_MAX)
  167. return -ENAMETOOLONG;
  168. /* reverse fillup of target string from target to base */
  169. sd = target_sd;
  170. while (sd->s_parent && sd != base) {
  171. int slen = strlen(sd->s_name);
  172. len -= slen;
  173. strncpy(s + len, sd->s_name, slen);
  174. if (len)
  175. s[--len] = '/';
  176. sd = sd->s_parent;
  177. }
  178. return 0;
  179. }
  180. static int sysfs_getlink(struct dentry *dentry, char * path)
  181. {
  182. struct sysfs_dirent *sd = dentry->d_fsdata;
  183. struct sysfs_dirent *parent_sd = sd->s_parent;
  184. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  185. int error;
  186. mutex_lock(&sysfs_mutex);
  187. error = sysfs_get_target_path(parent_sd, target_sd, path);
  188. mutex_unlock(&sysfs_mutex);
  189. return error;
  190. }
  191. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  192. {
  193. int error = -ENOMEM;
  194. unsigned long page = get_zeroed_page(GFP_KERNEL);
  195. if (page) {
  196. error = sysfs_getlink(dentry, (char *) page);
  197. if (error < 0)
  198. free_page((unsigned long)page);
  199. }
  200. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  201. return NULL;
  202. }
  203. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  204. {
  205. char *page = nd_get_link(nd);
  206. if (!IS_ERR(page))
  207. free_page((unsigned long)page);
  208. }
  209. const struct inode_operations sysfs_symlink_inode_operations = {
  210. .setxattr = sysfs_setxattr,
  211. .readlink = generic_readlink,
  212. .follow_link = sysfs_follow_link,
  213. .put_link = sysfs_put_link,
  214. .setattr = sysfs_setattr,
  215. .getattr = sysfs_getattr,
  216. .permission = sysfs_permission,
  217. };
  218. EXPORT_SYMBOL_GPL(sysfs_create_link);
  219. EXPORT_SYMBOL_GPL(sysfs_remove_link);