blob: e431a850f2f2b8acef9f35a514d3f5b603ae8865 [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.
24 */
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;
31
Tejun Heodb4aad22014-01-17 09:58:25 -050032 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
Tejun Heo324a56e2013-12-11 14:11:53 -050033 if (!kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050034 return ERR_PTR(-ENOMEM);
35
Tejun Heoac9bba02013-11-29 17:19:09 -050036 if (kernfs_ns_enabled(parent))
Tejun Heoadc5e8b2013-12-11 14:11:54 -050037 kn->ns = target->ns;
38 kn->symlink.target_kn = target;
Tejun Heo2072f1a2013-11-28 14:54:35 -050039 kernfs_get(target); /* ref owned by symlink */
40
Tejun Heo988cd7a2014-02-03 14:02:58 -050041 error = kernfs_add_one(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050042 if (!error)
Tejun Heo324a56e2013-12-11 14:11:53 -050043 return kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050044
Tejun Heo324a56e2013-12-11 14:11:53 -050045 kernfs_put(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050046 return ERR_PTR(error);
47}
48
Tejun Heoc637b8a2013-12-11 14:11:58 -050049static int kernfs_get_target_path(struct kernfs_node *parent,
50 struct kernfs_node *target, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -050051{
Tejun Heo324a56e2013-12-11 14:11:53 -050052 struct kernfs_node *base, *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050053 char *s = path;
54 int len = 0;
55
56 /* go up to the root, stop at the base */
Tejun Heo324a56e2013-12-11 14:11:53 -050057 base = parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050058 while (base->parent) {
59 kn = target->parent;
60 while (kn->parent && base != kn)
61 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050062
Tejun Heo324a56e2013-12-11 14:11:53 -050063 if (base == kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050064 break;
65
Bernd Edlinger90b3fec2018-07-07 17:52:47 +000066 if ((s - path) + 3 >= PATH_MAX)
67 return -ENAMETOOLONG;
68
Tejun Heo2072f1a2013-11-28 14:54:35 -050069 strcpy(s, "../");
70 s += 3;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050071 base = base->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050072 }
73
74 /* determine end of target string for reverse fillup */
Tejun Heo324a56e2013-12-11 14:11:53 -050075 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050076 while (kn->parent && kn != base) {
77 len += strlen(kn->name) + 1;
78 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050079 }
80
81 /* check limits */
82 if (len < 2)
83 return -EINVAL;
84 len--;
Bernd Edlinger90b3fec2018-07-07 17:52:47 +000085 if ((s - path) + len >= PATH_MAX)
Tejun Heo2072f1a2013-11-28 14:54:35 -050086 return -ENAMETOOLONG;
87
88 /* reverse fillup of target string from target to base */
Tejun Heo324a56e2013-12-11 14:11:53 -050089 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050090 while (kn->parent && kn != base) {
91 int slen = strlen(kn->name);
Tejun Heo2072f1a2013-11-28 14:54:35 -050092
93 len -= slen;
Guenter Roeck52fc8c72018-07-01 13:57:13 -070094 memcpy(s + len, kn->name, slen);
Tejun Heo2072f1a2013-11-28 14:54:35 -050095 if (len)
96 s[--len] = '/';
97
Tejun Heoadc5e8b2013-12-11 14:11:54 -050098 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050099 }
100
101 return 0;
102}
103
Tejun Heoc637b8a2013-12-11 14:11:58 -0500104static int kernfs_getlink(struct dentry *dentry, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500105{
Tejun Heo324a56e2013-12-11 14:11:53 -0500106 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500107 struct kernfs_node *parent = kn->parent;
108 struct kernfs_node *target = kn->symlink.target_kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500109 int error;
110
Tejun Heoa797bfc2013-12-11 14:11:57 -0500111 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500112 error = kernfs_get_target_path(parent, target, path);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500113 mutex_unlock(&kernfs_mutex);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500114
115 return error;
116}
117
Al Viro6b255392015-11-17 10:20:54 -0500118static const char *kernfs_iop_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500119 struct inode *inode,
120 struct delayed_call *done)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500121{
Al Virofceef392015-12-29 15:58:39 -0500122 char *body;
123 int error;
Al Viro6b255392015-11-17 10:20:54 -0500124
125 if (!dentry)
126 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -0500127 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
128 if (!body)
Al Viro680baac2015-05-02 13:32:22 -0400129 return ERR_PTR(-ENOMEM);
Al Virofceef392015-12-29 15:58:39 -0500130 error = kernfs_getlink(dentry, body);
Al Viro680baac2015-05-02 13:32:22 -0400131 if (unlikely(error < 0)) {
Al Virofceef392015-12-29 15:58:39 -0500132 kfree(body);
Al Viro680baac2015-05-02 13:32:22 -0400133 return ERR_PTR(error);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500134 }
Al Virofceef392015-12-29 15:58:39 -0500135 set_delayed_call(done, kfree_link, body);
136 return body;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500137}
138
Tejun Heoa797bfc2013-12-11 14:11:57 -0500139const struct inode_operations kernfs_symlink_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500140 .listxattr = kernfs_iop_listxattr,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500141 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500142 .get_link = kernfs_iop_get_link,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500143 .setattr = kernfs_iop_setattr,
144 .getattr = kernfs_iop_getattr,
145 .permission = kernfs_iop_permission,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500146};