blob: 5ffed48f3d0e7cebb0f58cd7b9923ae75c8e7c92 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
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 Heo2072f1a2013-11-28 14:54:35 -050010
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 Torokhov488dee92018-07-20 21:56:47 +000024 * Ownership of the link matches ownership of the target.
Tejun Heo2072f1a2013-11-28 14:54:35 -050025 */
Tejun Heo324a56e2013-12-11 14:11:53 -050026struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
27 const char *name,
28 struct kernfs_node *target)
Tejun Heo2072f1a2013-11-28 14:54:35 -050029{
Tejun Heo324a56e2013-12-11 14:11:53 -050030 struct kernfs_node *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050031 int error;
Dmitry Torokhov488dee92018-07-20 21:56:47 +000032 kuid_t uid = GLOBAL_ROOT_UID;
33 kgid_t gid = GLOBAL_ROOT_GID;
Tejun Heo2072f1a2013-11-28 14:54:35 -050034
Dmitry Torokhov488dee92018-07-20 21:56:47 +000035 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 Heo324a56e2013-12-11 14:11:53 -050042 if (!kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050043 return ERR_PTR(-ENOMEM);
44
Tejun Heoac9bba02013-11-29 17:19:09 -050045 if (kernfs_ns_enabled(parent))
Tejun Heoadc5e8b2013-12-11 14:11:54 -050046 kn->ns = target->ns;
47 kn->symlink.target_kn = target;
Tejun Heo2072f1a2013-11-28 14:54:35 -050048 kernfs_get(target); /* ref owned by symlink */
49
Tejun Heo988cd7a2014-02-03 14:02:58 -050050 error = kernfs_add_one(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050051 if (!error)
Tejun Heo324a56e2013-12-11 14:11:53 -050052 return kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050053
Tejun Heo324a56e2013-12-11 14:11:53 -050054 kernfs_put(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050055 return ERR_PTR(error);
56}
57
Tejun Heoc637b8a2013-12-11 14:11:58 -050058static int kernfs_get_target_path(struct kernfs_node *parent,
59 struct kernfs_node *target, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -050060{
Tejun Heo324a56e2013-12-11 14:11:53 -050061 struct kernfs_node *base, *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050062 char *s = path;
63 int len = 0;
64
65 /* go up to the root, stop at the base */
Tejun Heo324a56e2013-12-11 14:11:53 -050066 base = parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050067 while (base->parent) {
68 kn = target->parent;
69 while (kn->parent && base != kn)
70 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050071
Tejun Heo324a56e2013-12-11 14:11:53 -050072 if (base == kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050073 break;
74
75 strcpy(s, "../");
76 s += 3;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050077 base = base->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050078 }
79
80 /* determine end of target string for reverse fillup */
Tejun Heo324a56e2013-12-11 14:11:53 -050081 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050082 while (kn->parent && kn != base) {
83 len += strlen(kn->name) + 1;
84 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050085 }
86
87 /* check limits */
88 if (len < 2)
89 return -EINVAL;
90 len--;
91 if ((s - path) + len > PATH_MAX)
92 return -ENAMETOOLONG;
93
94 /* reverse fillup of target string from target to base */
Tejun Heo324a56e2013-12-11 14:11:53 -050095 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050096 while (kn->parent && kn != base) {
97 int slen = strlen(kn->name);
Tejun Heo2072f1a2013-11-28 14:54:35 -050098
99 len -= slen;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500100 strncpy(s + len, kn->name, slen);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500101 if (len)
102 s[--len] = '/';
103
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500104 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500105 }
106
107 return 0;
108}
109
Shaohua Li319ba912017-07-12 11:49:49 -0700110static int kernfs_getlink(struct inode *inode, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500111{
Shaohua Li319ba912017-07-12 11:49:49 -0700112 struct kernfs_node *kn = inode->i_private;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500113 struct kernfs_node *parent = kn->parent;
114 struct kernfs_node *target = kn->symlink.target_kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500115 int error;
116
Tejun Heoa797bfc2013-12-11 14:11:57 -0500117 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500118 error = kernfs_get_target_path(parent, target, path);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500119 mutex_unlock(&kernfs_mutex);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500120
121 return error;
122}
123
Al Viro6b255392015-11-17 10:20:54 -0500124static const char *kernfs_iop_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500125 struct inode *inode,
126 struct delayed_call *done)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500127{
Al Virofceef392015-12-29 15:58:39 -0500128 char *body;
129 int error;
Al Viro6b255392015-11-17 10:20:54 -0500130
131 if (!dentry)
132 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -0500133 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
134 if (!body)
Al Viro680baac2015-05-02 13:32:22 -0400135 return ERR_PTR(-ENOMEM);
Shaohua Li319ba912017-07-12 11:49:49 -0700136 error = kernfs_getlink(inode, body);
Al Viro680baac2015-05-02 13:32:22 -0400137 if (unlikely(error < 0)) {
Al Virofceef392015-12-29 15:58:39 -0500138 kfree(body);
Al Viro680baac2015-05-02 13:32:22 -0400139 return ERR_PTR(error);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500140 }
Al Virofceef392015-12-29 15:58:39 -0500141 set_delayed_call(done, kfree_link, body);
142 return body;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500143}
144
Tejun Heoa797bfc2013-12-11 14:11:57 -0500145const struct inode_operations kernfs_symlink_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500146 .listxattr = kernfs_iop_listxattr,
Al Viro6b255392015-11-17 10:20:54 -0500147 .get_link = kernfs_iop_get_link,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500148 .setattr = kernfs_iop_setattr,
149 .getattr = kernfs_iop_getattr,
150 .permission = kernfs_iop_permission,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500151};