]>
Commit | Line | Data |
---|---|---|
55716d26 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b8441ed2 TH |
2 | /* |
3 | * fs/kernfs/symlink.c - kernfs symlink implementation | |
4 | * | |
5 | * Copyright (c) 2001-3 Patrick Mochel | |
6 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
7 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> | |
b8441ed2 | 8 | */ |
2072f1af TH |
9 | |
10 | #include <linux/fs.h> | |
11 | #include <linux/gfp.h> | |
12 | #include <linux/namei.h> | |
13 | ||
14 | #include "kernfs-internal.h" | |
15 | ||
16 | /** | |
17 | * kernfs_create_link - create a symlink | |
18 | * @parent: directory to create the symlink in | |
19 | * @name: name of the symlink | |
20 | * @target: target node for the symlink to point to | |
21 | * | |
22 | * Returns the created node on success, ERR_PTR() value on error. | |
488dee96 | 23 | * Ownership of the link matches ownership of the target. |
2072f1af | 24 | */ |
324a56e1 TH |
25 | struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, |
26 | const char *name, | |
27 | struct kernfs_node *target) | |
2072f1af | 28 | { |
324a56e1 | 29 | struct kernfs_node *kn; |
2072f1af | 30 | int error; |
488dee96 DT |
31 | kuid_t uid = GLOBAL_ROOT_UID; |
32 | kgid_t gid = GLOBAL_ROOT_GID; | |
2072f1af | 33 | |
488dee96 | 34 | if (target->iattr) { |
05895219 OM |
35 | uid = target->iattr->ia_uid; |
36 | gid = target->iattr->ia_gid; | |
488dee96 DT |
37 | } |
38 | ||
39 | kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid, | |
40 | KERNFS_LINK); | |
324a56e1 | 41 | if (!kn) |
2072f1af TH |
42 | return ERR_PTR(-ENOMEM); |
43 | ||
ac9bba03 | 44 | if (kernfs_ns_enabled(parent)) |
adc5e8b5 TH |
45 | kn->ns = target->ns; |
46 | kn->symlink.target_kn = target; | |
2072f1af TH |
47 | kernfs_get(target); /* ref owned by symlink */ |
48 | ||
988cd7af | 49 | error = kernfs_add_one(kn); |
2072f1af | 50 | if (!error) |
324a56e1 | 51 | return kn; |
2072f1af | 52 | |
324a56e1 | 53 | kernfs_put(kn); |
2072f1af TH |
54 | return ERR_PTR(error); |
55 | } | |
56 | ||
c637b8ac TH |
57 | static int kernfs_get_target_path(struct kernfs_node *parent, |
58 | struct kernfs_node *target, char *path) | |
2072f1af | 59 | { |
324a56e1 | 60 | struct kernfs_node *base, *kn; |
2072f1af TH |
61 | char *s = path; |
62 | int len = 0; | |
63 | ||
64 | /* go up to the root, stop at the base */ | |
324a56e1 | 65 | base = parent; |
adc5e8b5 TH |
66 | while (base->parent) { |
67 | kn = target->parent; | |
68 | while (kn->parent && base != kn) | |
69 | kn = kn->parent; | |
2072f1af | 70 | |
324a56e1 | 71 | if (base == kn) |
2072f1af TH |
72 | break; |
73 | ||
a75e78f2 BE |
74 | if ((s - path) + 3 >= PATH_MAX) |
75 | return -ENAMETOOLONG; | |
76 | ||
2072f1af TH |
77 | strcpy(s, "../"); |
78 | s += 3; | |
adc5e8b5 | 79 | base = base->parent; |
2072f1af TH |
80 | } |
81 | ||
82 | /* determine end of target string for reverse fillup */ | |
324a56e1 | 83 | kn = target; |
adc5e8b5 TH |
84 | while (kn->parent && kn != base) { |
85 | len += strlen(kn->name) + 1; | |
86 | kn = kn->parent; | |
2072f1af TH |
87 | } |
88 | ||
89 | /* check limits */ | |
90 | if (len < 2) | |
91 | return -EINVAL; | |
92 | len--; | |
a75e78f2 | 93 | if ((s - path) + len >= PATH_MAX) |
2072f1af TH |
94 | return -ENAMETOOLONG; |
95 | ||
96 | /* reverse fillup of target string from target to base */ | |
324a56e1 | 97 | kn = target; |
adc5e8b5 TH |
98 | while (kn->parent && kn != base) { |
99 | int slen = strlen(kn->name); | |
2072f1af TH |
100 | |
101 | len -= slen; | |
166126c1 | 102 | memcpy(s + len, kn->name, slen); |
2072f1af TH |
103 | if (len) |
104 | s[--len] = '/'; | |
105 | ||
adc5e8b5 | 106 | kn = kn->parent; |
2072f1af TH |
107 | } |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
319ba91d | 112 | static int kernfs_getlink(struct inode *inode, char *path) |
2072f1af | 113 | { |
319ba91d | 114 | struct kernfs_node *kn = inode->i_private; |
adc5e8b5 TH |
115 | struct kernfs_node *parent = kn->parent; |
116 | struct kernfs_node *target = kn->symlink.target_kn; | |
2072f1af TH |
117 | int error; |
118 | ||
7ba0273b | 119 | down_read(&kernfs_rwsem); |
c637b8ac | 120 | error = kernfs_get_target_path(parent, target, path); |
7ba0273b | 121 | up_read(&kernfs_rwsem); |
2072f1af TH |
122 | |
123 | return error; | |
124 | } | |
125 | ||
6b255391 | 126 | static const char *kernfs_iop_get_link(struct dentry *dentry, |
fceef393 AV |
127 | struct inode *inode, |
128 | struct delayed_call *done) | |
2072f1af | 129 | { |
fceef393 AV |
130 | char *body; |
131 | int error; | |
6b255391 AV |
132 | |
133 | if (!dentry) | |
134 | return ERR_PTR(-ECHILD); | |
fceef393 AV |
135 | body = kzalloc(PAGE_SIZE, GFP_KERNEL); |
136 | if (!body) | |
680baacb | 137 | return ERR_PTR(-ENOMEM); |
319ba91d | 138 | error = kernfs_getlink(inode, body); |
680baacb | 139 | if (unlikely(error < 0)) { |
fceef393 | 140 | kfree(body); |
680baacb | 141 | return ERR_PTR(error); |
2072f1af | 142 | } |
fceef393 AV |
143 | set_delayed_call(done, kfree_link, body); |
144 | return body; | |
2072f1af TH |
145 | } |
146 | ||
a797bfc3 | 147 | const struct inode_operations kernfs_symlink_iops = { |
c637b8ac | 148 | .listxattr = kernfs_iop_listxattr, |
6b255391 | 149 | .get_link = kernfs_iop_get_link, |
c637b8ac TH |
150 | .setattr = kernfs_iop_setattr, |
151 | .getattr = kernfs_iop_getattr, | |
152 | .permission = kernfs_iop_permission, | |
2072f1af | 153 | }; |