blob: 5432883d819f2ec69c0436a419ef863b0c98f368 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Tejun Heob8441ed2013-11-24 09:54:58 -05002/*
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>
Tejun Heob8441ed2013-11-24 09:54:58 -05008 */
Tejun Heo2072f1a2013-11-28 14:54:35 -05009
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.
Dmitry Torokhov488dee92018-07-20 21:56:47 +000023 * Ownership of the link matches ownership of the target.
Tejun Heo2072f1a2013-11-28 14:54:35 -050024 */
Tejun Heo324a56e2013-12-11 14:11:53 -050025struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
26 const char *name,
27 struct kernfs_node *target)
Tejun Heo2072f1a2013-11-28 14:54:35 -050028{
Tejun Heo324a56e2013-12-11 14:11:53 -050029 struct kernfs_node *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050030 int error;
Dmitry Torokhov488dee92018-07-20 21:56:47 +000031 kuid_t uid = GLOBAL_ROOT_UID;
32 kgid_t gid = GLOBAL_ROOT_GID;
Tejun Heo2072f1a2013-11-28 14:54:35 -050033
Dmitry Torokhov488dee92018-07-20 21:56:47 +000034 if (target->iattr) {
Ondrej Mosnacek05895212019-02-22 15:57:12 +010035 uid = target->iattr->ia_uid;
36 gid = target->iattr->ia_gid;
Dmitry Torokhov488dee92018-07-20 21:56:47 +000037 }
38
39 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
40 KERNFS_LINK);
Tejun Heo324a56e2013-12-11 14:11:53 -050041 if (!kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050042 return ERR_PTR(-ENOMEM);
43
Tejun Heoac9bba02013-11-29 17:19:09 -050044 if (kernfs_ns_enabled(parent))
Tejun Heoadc5e8b2013-12-11 14:11:54 -050045 kn->ns = target->ns;
46 kn->symlink.target_kn = target;
Tejun Heo2072f1a2013-11-28 14:54:35 -050047 kernfs_get(target); /* ref owned by symlink */
48
Tejun Heo988cd7a2014-02-03 14:02:58 -050049 error = kernfs_add_one(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050050 if (!error)
Tejun Heo324a56e2013-12-11 14:11:53 -050051 return kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050052
Tejun Heo324a56e2013-12-11 14:11:53 -050053 kernfs_put(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050054 return ERR_PTR(error);
55}
56
Tejun Heoc637b8a2013-12-11 14:11:58 -050057static int kernfs_get_target_path(struct kernfs_node *parent,
58 struct kernfs_node *target, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -050059{
Tejun Heo324a56e2013-12-11 14:11:53 -050060 struct kernfs_node *base, *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050061 char *s = path;
62 int len = 0;
63
64 /* go up to the root, stop at the base */
Tejun Heo324a56e2013-12-11 14:11:53 -050065 base = parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050066 while (base->parent) {
67 kn = target->parent;
68 while (kn->parent && base != kn)
69 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050070
Tejun Heo324a56e2013-12-11 14:11:53 -050071 if (base == kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050072 break;
73
Bernd Edlingera75e78f2018-07-07 17:52:47 +000074 if ((s - path) + 3 >= PATH_MAX)
75 return -ENAMETOOLONG;
76
Tejun Heo2072f1a2013-11-28 14:54:35 -050077 strcpy(s, "../");
78 s += 3;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050079 base = base->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050080 }
81
82 /* determine end of target string for reverse fillup */
Tejun Heo324a56e2013-12-11 14:11:53 -050083 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050084 while (kn->parent && kn != base) {
85 len += strlen(kn->name) + 1;
86 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050087 }
88
89 /* check limits */
90 if (len < 2)
91 return -EINVAL;
92 len--;
Bernd Edlingera75e78f2018-07-07 17:52:47 +000093 if ((s - path) + len >= PATH_MAX)
Tejun Heo2072f1a2013-11-28 14:54:35 -050094 return -ENAMETOOLONG;
95
96 /* reverse fillup of target string from target to base */
Tejun Heo324a56e2013-12-11 14:11:53 -050097 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050098 while (kn->parent && kn != base) {
99 int slen = strlen(kn->name);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500100
101 len -= slen;
Guenter Roeck166126c2018-07-01 13:57:13 -0700102 memcpy(s + len, kn->name, slen);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500103 if (len)
104 s[--len] = '/';
105
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500106 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500107 }
108
109 return 0;
110}
111
Shaohua Li319ba912017-07-12 11:49:49 -0700112static int kernfs_getlink(struct inode *inode, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500113{
Shaohua Li319ba912017-07-12 11:49:49 -0700114 struct kernfs_node *kn = inode->i_private;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500115 struct kernfs_node *parent = kn->parent;
116 struct kernfs_node *target = kn->symlink.target_kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500117 int error;
118
Tejun Heoa797bfc2013-12-11 14:11:57 -0500119 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500120 error = kernfs_get_target_path(parent, target, path);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500121 mutex_unlock(&kernfs_mutex);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500122
123 return error;
124}
125
Al Viro6b255392015-11-17 10:20:54 -0500126static const char *kernfs_iop_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500127 struct inode *inode,
128 struct delayed_call *done)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500129{
Al Virofceef392015-12-29 15:58:39 -0500130 char *body;
131 int error;
Al Viro6b255392015-11-17 10:20:54 -0500132
133 if (!dentry)
134 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -0500135 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
136 if (!body)
Al Viro680baac2015-05-02 13:32:22 -0400137 return ERR_PTR(-ENOMEM);
Shaohua Li319ba912017-07-12 11:49:49 -0700138 error = kernfs_getlink(inode, body);
Al Viro680baac2015-05-02 13:32:22 -0400139 if (unlikely(error < 0)) {
Al Virofceef392015-12-29 15:58:39 -0500140 kfree(body);
Al Viro680baac2015-05-02 13:32:22 -0400141 return ERR_PTR(error);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500142 }
Al Virofceef392015-12-29 15:58:39 -0500143 set_delayed_call(done, kfree_link, body);
144 return body;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500145}
146
Tejun Heoa797bfc2013-12-11 14:11:57 -0500147const struct inode_operations kernfs_symlink_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500148 .listxattr = kernfs_iop_listxattr,
Al Viro6b255392015-11-17 10:20:54 -0500149 .get_link = kernfs_iop_get_link,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500150 .setattr = kernfs_iop_setattr,
151 .getattr = kernfs_iop_getattr,
152 .permission = kernfs_iop_permission,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500153};