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