symlink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. */
  22. struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
  23. const char *name,
  24. struct kernfs_node *target)
  25. {
  26. struct kernfs_node *kn;
  27. int error;
  28. kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
  29. if (!kn)
  30. return ERR_PTR(-ENOMEM);
  31. if (kernfs_ns_enabled(parent))
  32. kn->ns = target->ns;
  33. kn->symlink.target_kn = target;
  34. kernfs_get(target); /* ref owned by symlink */
  35. error = kernfs_add_one(kn);
  36. if (!error)
  37. return kn;
  38. kernfs_put(kn);
  39. return ERR_PTR(error);
  40. }
  41. static int kernfs_get_target_path(struct kernfs_node *parent,
  42. struct kernfs_node *target, char *path)
  43. {
  44. struct kernfs_node *base, *kn;
  45. char *s = path;
  46. int len = 0;
  47. /* go up to the root, stop at the base */
  48. base = parent;
  49. while (base->parent) {
  50. kn = target->parent;
  51. while (kn->parent && base != kn)
  52. kn = kn->parent;
  53. if (base == kn)
  54. break;
  55. strcpy(s, "../");
  56. s += 3;
  57. base = base->parent;
  58. }
  59. /* determine end of target string for reverse fillup */
  60. kn = target;
  61. while (kn->parent && kn != base) {
  62. len += strlen(kn->name) + 1;
  63. kn = kn->parent;
  64. }
  65. /* check limits */
  66. if (len < 2)
  67. return -EINVAL;
  68. len--;
  69. if ((s - path) + len > PATH_MAX)
  70. return -ENAMETOOLONG;
  71. /* reverse fillup of target string from target to base */
  72. kn = target;
  73. while (kn->parent && kn != base) {
  74. int slen = strlen(kn->name);
  75. len -= slen;
  76. strncpy(s + len, kn->name, slen);
  77. if (len)
  78. s[--len] = '/';
  79. kn = kn->parent;
  80. }
  81. return 0;
  82. }
  83. static int kernfs_getlink(struct dentry *dentry, char *path)
  84. {
  85. struct kernfs_node *kn = dentry->d_fsdata;
  86. struct kernfs_node *parent = kn->parent;
  87. struct kernfs_node *target = kn->symlink.target_kn;
  88. int error;
  89. mutex_lock(&kernfs_mutex);
  90. error = kernfs_get_target_path(parent, target, path);
  91. mutex_unlock(&kernfs_mutex);
  92. return error;
  93. }
  94. static const char *kernfs_iop_get_link(struct dentry *dentry,
  95. struct inode *inode,
  96. struct delayed_call *done)
  97. {
  98. char *body;
  99. int error;
  100. if (!dentry)
  101. return ERR_PTR(-ECHILD);
  102. body = kzalloc(PAGE_SIZE, GFP_KERNEL);
  103. if (!body)
  104. return ERR_PTR(-ENOMEM);
  105. error = kernfs_getlink(dentry, body);
  106. if (unlikely(error < 0)) {
  107. kfree(body);
  108. return ERR_PTR(error);
  109. }
  110. set_delayed_call(done, kfree_link, body);
  111. return body;
  112. }
  113. const struct inode_operations kernfs_symlink_iops = {
  114. .listxattr = kernfs_iop_listxattr,
  115. .readlink = generic_readlink,
  116. .get_link = kernfs_iop_get_link,
  117. .setattr = kernfs_iop_setattr,
  118. .getattr = kernfs_iop_getattr,
  119. .permission = kernfs_iop_permission,
  120. };