Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 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 | */ |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/gfp.h> |
| 13 | #include <linux/namei.h> |
| 14 | |
| 15 | #include "kernfs-internal.h" |
| 16 | |
| 17 | /** |
| 18 | * kernfs_create_link - create a symlink |
| 19 | * @parent: directory to create the symlink in |
| 20 | * @name: name of the symlink |
| 21 | * @target: target node for the symlink to point to |
| 22 | * |
| 23 | * Returns the created node on success, ERR_PTR() value on error. |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 24 | * Ownership of the link matches ownership of the target. |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 25 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 26 | struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, |
| 27 | const char *name, |
| 28 | struct kernfs_node *target) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 29 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 30 | struct kernfs_node *kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 31 | int error; |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 32 | kuid_t uid = GLOBAL_ROOT_UID; |
| 33 | kgid_t gid = GLOBAL_ROOT_GID; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 34 | |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 35 | if (target->iattr) { |
| 36 | uid = target->iattr->ia_iattr.ia_uid; |
| 37 | gid = target->iattr->ia_iattr.ia_gid; |
| 38 | } |
| 39 | |
| 40 | kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid, |
| 41 | KERNFS_LINK); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 42 | if (!kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 43 | return ERR_PTR(-ENOMEM); |
| 44 | |
Tejun Heo | ac9bba0 | 2013-11-29 17:19:09 -0500 | [diff] [blame] | 45 | if (kernfs_ns_enabled(parent)) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 46 | kn->ns = target->ns; |
| 47 | kn->symlink.target_kn = target; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 48 | kernfs_get(target); /* ref owned by symlink */ |
| 49 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 50 | error = kernfs_add_one(kn); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 51 | if (!error) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 52 | return kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 53 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 54 | kernfs_put(kn); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 55 | return ERR_PTR(error); |
| 56 | } |
| 57 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 58 | static int kernfs_get_target_path(struct kernfs_node *parent, |
| 59 | struct kernfs_node *target, char *path) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 60 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 61 | struct kernfs_node *base, *kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 62 | char *s = path; |
| 63 | int len = 0; |
| 64 | |
| 65 | /* go up to the root, stop at the base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 66 | base = parent; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 67 | while (base->parent) { |
| 68 | kn = target->parent; |
| 69 | while (kn->parent && base != kn) |
| 70 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 71 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 72 | if (base == kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 73 | break; |
| 74 | |
Bernd Edlinger | a75e78f | 2018-07-07 17:52:47 +0000 | [diff] [blame] | 75 | if ((s - path) + 3 >= PATH_MAX) |
| 76 | return -ENAMETOOLONG; |
| 77 | |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 78 | strcpy(s, "../"); |
| 79 | s += 3; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 80 | base = base->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* determine end of target string for reverse fillup */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 84 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 85 | while (kn->parent && kn != base) { |
| 86 | len += strlen(kn->name) + 1; |
| 87 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* check limits */ |
| 91 | if (len < 2) |
| 92 | return -EINVAL; |
| 93 | len--; |
Bernd Edlinger | a75e78f | 2018-07-07 17:52:47 +0000 | [diff] [blame] | 94 | if ((s - path) + len >= PATH_MAX) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 95 | return -ENAMETOOLONG; |
| 96 | |
| 97 | /* reverse fillup of target string from target to base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 98 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 99 | while (kn->parent && kn != base) { |
| 100 | int slen = strlen(kn->name); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 101 | |
| 102 | len -= slen; |
Guenter Roeck | 166126c | 2018-07-01 13:57:13 -0700 | [diff] [blame] | 103 | memcpy(s + len, kn->name, slen); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 104 | if (len) |
| 105 | s[--len] = '/'; |
| 106 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 107 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 113 | static int kernfs_getlink(struct inode *inode, char *path) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 114 | { |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 115 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 116 | struct kernfs_node *parent = kn->parent; |
| 117 | struct kernfs_node *target = kn->symlink.target_kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 118 | int error; |
| 119 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 120 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 121 | error = kernfs_get_target_path(parent, target, path); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 122 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 123 | |
| 124 | return error; |
| 125 | } |
| 126 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 127 | static const char *kernfs_iop_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 128 | struct inode *inode, |
| 129 | struct delayed_call *done) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 130 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 131 | char *body; |
| 132 | int error; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 133 | |
| 134 | if (!dentry) |
| 135 | return ERR_PTR(-ECHILD); |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 136 | body = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 137 | if (!body) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 138 | return ERR_PTR(-ENOMEM); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 139 | error = kernfs_getlink(inode, body); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 140 | if (unlikely(error < 0)) { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 141 | kfree(body); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 142 | return ERR_PTR(error); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 143 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 144 | set_delayed_call(done, kfree_link, body); |
| 145 | return body; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 148 | const struct inode_operations kernfs_symlink_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 149 | .listxattr = kernfs_iop_listxattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 150 | .get_link = kernfs_iop_get_link, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 151 | .setattr = kernfs_iop_setattr, |
| 152 | .getattr = kernfs_iop_getattr, |
| 153 | .permission = kernfs_iop_permission, |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 154 | }; |