symlink.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/mount.h>
  14. #include <linux/module.h>
  15. #include <linux/kobject.h>
  16. #include <linux/namei.h>
  17. #include <linux/mutex.h>
  18. #include "sysfs.h"
  19. static int object_depth(struct sysfs_dirent *sd)
  20. {
  21. int depth = 0;
  22. for (; sd->s_parent; sd = sd->s_parent)
  23. depth++;
  24. return depth;
  25. }
  26. static int object_path_length(struct sysfs_dirent * sd)
  27. {
  28. int length = 1;
  29. for (; sd->s_parent; sd = sd->s_parent)
  30. length += strlen(sd->s_name) + 1;
  31. return length;
  32. }
  33. static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
  34. {
  35. --length;
  36. for (; sd->s_parent; sd = sd->s_parent) {
  37. int cur = strlen(sd->s_name);
  38. /* back up enough to print this bus id with '/' */
  39. length -= cur;
  40. strncpy(buffer + length, sd->s_name, cur);
  41. *(buffer + --length) = '/';
  42. }
  43. }
  44. /**
  45. * sysfs_create_link - create symlink between two objects.
  46. * @kobj: object whose directory we're creating the link in.
  47. * @target: object we're pointing to.
  48. * @name: name of the symlink.
  49. */
  50. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  51. {
  52. struct sysfs_dirent *parent_sd = NULL;
  53. struct sysfs_dirent *target_sd = NULL;
  54. struct sysfs_dirent *sd = NULL;
  55. struct sysfs_addrm_cxt acxt;
  56. int error;
  57. BUG_ON(!name);
  58. if (!kobj)
  59. parent_sd = &sysfs_root;
  60. else
  61. parent_sd = kobj->sd;
  62. error = -EFAULT;
  63. if (!parent_sd)
  64. goto out_put;
  65. /* target->sd can go away beneath us but is protected with
  66. * sysfs_assoc_lock. Fetch target_sd from it.
  67. */
  68. spin_lock(&sysfs_assoc_lock);
  69. if (target->sd)
  70. target_sd = sysfs_get(target->sd);
  71. spin_unlock(&sysfs_assoc_lock);
  72. error = -ENOENT;
  73. if (!target_sd)
  74. goto out_put;
  75. error = -ENOMEM;
  76. sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  77. if (!sd)
  78. goto out_put;
  79. sd->s_symlink.target_sd = target_sd;
  80. target_sd = NULL; /* reference is now owned by the symlink */
  81. sysfs_addrm_start(&acxt, parent_sd);
  82. error = sysfs_add_one(&acxt, sd);
  83. sysfs_addrm_finish(&acxt);
  84. if (error)
  85. goto out_put;
  86. return 0;
  87. out_put:
  88. sysfs_put(target_sd);
  89. sysfs_put(sd);
  90. return error;
  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. sysfs_hash_and_remove(kobj->sd, name);
  100. }
  101. static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
  102. struct sysfs_dirent * target_sd, char *path)
  103. {
  104. char * s;
  105. int depth, size;
  106. depth = object_depth(parent_sd);
  107. size = object_path_length(target_sd) + depth * 3 - 1;
  108. if (size > PATH_MAX)
  109. return -ENAMETOOLONG;
  110. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  111. for (s = path; depth--; s += 3)
  112. strcpy(s,"../");
  113. fill_object_path(target_sd, path, size);
  114. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  115. return 0;
  116. }
  117. static int sysfs_getlink(struct dentry *dentry, char * path)
  118. {
  119. struct sysfs_dirent *sd = dentry->d_fsdata;
  120. struct sysfs_dirent *parent_sd = sd->s_parent;
  121. struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
  122. int error;
  123. mutex_lock(&sysfs_mutex);
  124. error = sysfs_get_target_path(parent_sd, target_sd, path);
  125. mutex_unlock(&sysfs_mutex);
  126. return error;
  127. }
  128. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  129. {
  130. int error = -ENOMEM;
  131. unsigned long page = get_zeroed_page(GFP_KERNEL);
  132. if (page)
  133. error = sysfs_getlink(dentry, (char *) page);
  134. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  135. return NULL;
  136. }
  137. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  138. {
  139. char *page = nd_get_link(nd);
  140. if (!IS_ERR(page))
  141. free_page((unsigned long)page);
  142. }
  143. const struct inode_operations sysfs_symlink_inode_operations = {
  144. .readlink = generic_readlink,
  145. .follow_link = sysfs_follow_link,
  146. .put_link = sysfs_put_link,
  147. };
  148. EXPORT_SYMBOL_GPL(sysfs_create_link);
  149. EXPORT_SYMBOL_GPL(sysfs_remove_link);