symlink.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * fs/kernfs/symlink.c - kernfs symlink implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/gfp.h>
  12. #include <linux/namei.h>
  13. #include "kernfs-internal.h"
  14. /**
  15. * kernfs_create_link - create a symlink
  16. * @parent: directory to create the symlink in
  17. * @name: name of the symlink
  18. * @target: target node for the symlink to point to
  19. *
  20. * Returns the created node on success, ERR_PTR() value on error.
  21. * Ownership of the link matches ownership of the target.
  22. */
  23. struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
  24. const char *name,
  25. struct kernfs_node *target)
  26. {
  27. struct kernfs_node *kn;
  28. int error;
  29. kuid_t uid = GLOBAL_ROOT_UID;
  30. kgid_t gid = GLOBAL_ROOT_GID;
  31. if (target->iattr) {
  32. uid = target->iattr->ia_iattr.ia_uid;
  33. gid = target->iattr->ia_iattr.ia_gid;
  34. }
  35. kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
  36. KERNFS_LINK);
  37. if (!kn)
  38. return ERR_PTR(-ENOMEM);
  39. if (kernfs_ns_enabled(parent))
  40. kn->ns = target->ns;
  41. kn->symlink.target_kn = target;
  42. kernfs_get(target); /* ref owned by symlink */
  43. error = kernfs_add_one(kn);
  44. if (!error)
  45. return kn;
  46. kernfs_put(kn);
  47. return ERR_PTR(error);
  48. }
  49. static int kernfs_get_target_path(struct kernfs_node *parent,
  50. struct kernfs_node *target, char *path)
  51. {
  52. struct kernfs_node *base, *kn;
  53. char *s = path;
  54. int len = 0;
  55. /* go up to the root, stop at the base */
  56. base = parent;
  57. while (base->parent) {
  58. kn = target->parent;
  59. while (kn->parent && base != kn)
  60. kn = kn->parent;
  61. if (base == kn)
  62. break;
  63. if ((s - path) + 3 >= PATH_MAX)
  64. return -ENAMETOOLONG;
  65. strcpy(s, "../");
  66. s += 3;
  67. base = base->parent;
  68. }
  69. /* determine end of target string for reverse fillup */
  70. kn = target;
  71. while (kn->parent && kn != base) {
  72. len += strlen(kn->name) + 1;
  73. kn = kn->parent;
  74. }
  75. /* check limits */
  76. if (len < 2)
  77. return -EINVAL;
  78. len--;
  79. if ((s - path) + len >= PATH_MAX)
  80. return -ENAMETOOLONG;
  81. /* reverse fillup of target string from target to base */
  82. kn = target;
  83. while (kn->parent && kn != base) {
  84. int slen = strlen(kn->name);
  85. len -= slen;
  86. memcpy(s + len, kn->name, slen);
  87. if (len)
  88. s[--len] = '/';
  89. kn = kn->parent;
  90. }
  91. return 0;
  92. }
  93. static int kernfs_getlink(struct inode *inode, char *path)
  94. {
  95. struct kernfs_node *kn = inode->i_private;
  96. struct kernfs_node *parent = kn->parent;
  97. struct kernfs_node *target = kn->symlink.target_kn;
  98. int error;
  99. mutex_lock(&kernfs_mutex);
  100. error = kernfs_get_target_path(parent, target, path);
  101. mutex_unlock(&kernfs_mutex);
  102. return error;
  103. }
  104. static const char *kernfs_iop_get_link(struct dentry *dentry,
  105. struct inode *inode,
  106. struct delayed_call *done)
  107. {
  108. char *body;
  109. int error;
  110. if (!dentry)
  111. return ERR_PTR(-ECHILD);
  112. body = kzalloc(PAGE_SIZE, GFP_KERNEL);
  113. if (!body)
  114. return ERR_PTR(-ENOMEM);
  115. error = kernfs_getlink(inode, body);
  116. if (unlikely(error < 0)) {
  117. kfree(body);
  118. return ERR_PTR(error);
  119. }
  120. set_delayed_call(done, kfree_link, body);
  121. return body;
  122. }
  123. const struct inode_operations kernfs_symlink_iops = {
  124. .listxattr = kernfs_iop_listxattr,
  125. .get_link = kernfs_iop_get_link,
  126. .setattr = kernfs_iop_setattr,
  127. .getattr = kernfs_iop_getattr,
  128. .permission = kernfs_iop_permission,
  129. };