Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / fs / kernfs / symlink.c
CommitLineData
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 *
24b3e3dd 22 * Return: 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
25struct 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
8f5cfb3b 39 kn = kernfs_new_node(parent, name, S_IFLNK|0777, uid, gid, KERNFS_LINK);
324a56e1 40 if (!kn)
2072f1af
TH
41 return ERR_PTR(-ENOMEM);
42
ac9bba03 43 if (kernfs_ns_enabled(parent))
adc5e8b5
TH
44 kn->ns = target->ns;
45 kn->symlink.target_kn = target;
2072f1af
TH
46 kernfs_get(target); /* ref owned by symlink */
47
988cd7af 48 error = kernfs_add_one(kn);
2072f1af 49 if (!error)
324a56e1 50 return kn;
2072f1af 51
324a56e1 52 kernfs_put(kn);
2072f1af
TH
53 return ERR_PTR(error);
54}
55
c637b8ac
TH
56static int kernfs_get_target_path(struct kernfs_node *parent,
57 struct kernfs_node *target, char *path)
2072f1af 58{
324a56e1 59 struct kernfs_node *base, *kn;
2072f1af
TH
60 char *s = path;
61 int len = 0;
62
63 /* go up to the root, stop at the base */
324a56e1 64 base = parent;
63348894
SAS
65 while (kernfs_parent(base)) {
66 kn = kernfs_parent(target);
67 while (kernfs_parent(kn) && base != kn)
68 kn = kernfs_parent(kn);
2072f1af 69
324a56e1 70 if (base == kn)
2072f1af
TH
71 break;
72
a75e78f2
BE
73 if ((s - path) + 3 >= PATH_MAX)
74 return -ENAMETOOLONG;
75
2072f1af
TH
76 strcpy(s, "../");
77 s += 3;
63348894 78 base = kernfs_parent(base);
2072f1af
TH
79 }
80
81 /* determine end of target string for reverse fillup */
324a56e1 82 kn = target;
63348894 83 while (kernfs_parent(kn) && kn != base) {
741c10b0 84 len += strlen(kernfs_rcu_name(kn)) + 1;
63348894 85 kn = kernfs_parent(kn);
2072f1af
TH
86 }
87
88 /* check limits */
89 if (len < 2)
90 return -EINVAL;
91 len--;
a75e78f2 92 if ((s - path) + len >= PATH_MAX)
2072f1af
TH
93 return -ENAMETOOLONG;
94
95 /* reverse fillup of target string from target to base */
324a56e1 96 kn = target;
63348894 97 while (kernfs_parent(kn) && kn != base) {
741c10b0
SAS
98 const char *name = kernfs_rcu_name(kn);
99 int slen = strlen(name);
2072f1af
TH
100
101 len -= slen;
741c10b0 102 memcpy(s + len, name, slen);
2072f1af
TH
103 if (len)
104 s[--len] = '/';
105
63348894 106 kn = kernfs_parent(kn);
2072f1af
TH
107 }
108
109 return 0;
110}
111
319ba91d 112static int kernfs_getlink(struct inode *inode, char *path)
2072f1af 113{
319ba91d 114 struct kernfs_node *kn = inode->i_private;
63348894 115 struct kernfs_node *parent;
adc5e8b5 116 struct kernfs_node *target = kn->symlink.target_kn;
63348894 117 struct kernfs_root *root = kernfs_root(kn);
2072f1af
TH
118 int error;
119
393c3714 120 down_read(&root->kernfs_rwsem);
63348894 121 parent = kernfs_parent(kn);
c637b8ac 122 error = kernfs_get_target_path(parent, target, path);
393c3714 123 up_read(&root->kernfs_rwsem);
2072f1af
TH
124
125 return error;
126}
127
6b255391 128static const char *kernfs_iop_get_link(struct dentry *dentry,
fceef393
AV
129 struct inode *inode,
130 struct delayed_call *done)
2072f1af 131{
fceef393
AV
132 char *body;
133 int error;
6b255391
AV
134
135 if (!dentry)
136 return ERR_PTR(-ECHILD);
fceef393
AV
137 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
138 if (!body)
680baacb 139 return ERR_PTR(-ENOMEM);
319ba91d 140 error = kernfs_getlink(inode, body);
680baacb 141 if (unlikely(error < 0)) {
fceef393 142 kfree(body);
680baacb 143 return ERR_PTR(error);
2072f1af 144 }
fceef393
AV
145 set_delayed_call(done, kfree_link, body);
146 return body;
2072f1af
TH
147}
148
a797bfc3 149const struct inode_operations kernfs_symlink_iops = {
c637b8ac 150 .listxattr = kernfs_iop_listxattr,
6b255391 151 .get_link = kernfs_iop_get_link,
c637b8ac
TH
152 .setattr = kernfs_iop_setattr,
153 .getattr = kernfs_iop_getattr,
154 .permission = kernfs_iop_permission,
2072f1af 155};