xref: /kernel/linux/linux-5.10/fs/kernfs/symlink.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fs/kernfs/symlink.c - kernfs symlink implementation
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2001-3 Patrick Mochel
68c2ecf20Sopenharmony_ci * Copyright (c) 2007 SUSE Linux Products GmbH
78c2ecf20Sopenharmony_ci * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/fs.h>
118c2ecf20Sopenharmony_ci#include <linux/gfp.h>
128c2ecf20Sopenharmony_ci#include <linux/namei.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "kernfs-internal.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/**
178c2ecf20Sopenharmony_ci * kernfs_create_link - create a symlink
188c2ecf20Sopenharmony_ci * @parent: directory to create the symlink in
198c2ecf20Sopenharmony_ci * @name: name of the symlink
208c2ecf20Sopenharmony_ci * @target: target node for the symlink to point to
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Returns the created node on success, ERR_PTR() value on error.
238c2ecf20Sopenharmony_ci * Ownership of the link matches ownership of the target.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_cistruct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
268c2ecf20Sopenharmony_ci				       const char *name,
278c2ecf20Sopenharmony_ci				       struct kernfs_node *target)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct kernfs_node *kn;
308c2ecf20Sopenharmony_ci	int error;
318c2ecf20Sopenharmony_ci	kuid_t uid = GLOBAL_ROOT_UID;
328c2ecf20Sopenharmony_ci	kgid_t gid = GLOBAL_ROOT_GID;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if (target->iattr) {
358c2ecf20Sopenharmony_ci		uid = target->iattr->ia_uid;
368c2ecf20Sopenharmony_ci		gid = target->iattr->ia_gid;
378c2ecf20Sopenharmony_ci	}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
408c2ecf20Sopenharmony_ci			     KERNFS_LINK);
418c2ecf20Sopenharmony_ci	if (!kn)
428c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	if (kernfs_ns_enabled(parent))
458c2ecf20Sopenharmony_ci		kn->ns = target->ns;
468c2ecf20Sopenharmony_ci	kn->symlink.target_kn = target;
478c2ecf20Sopenharmony_ci	kernfs_get(target);	/* ref owned by symlink */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	error = kernfs_add_one(kn);
508c2ecf20Sopenharmony_ci	if (!error)
518c2ecf20Sopenharmony_ci		return kn;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	kernfs_put(kn);
548c2ecf20Sopenharmony_ci	return ERR_PTR(error);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int kernfs_get_target_path(struct kernfs_node *parent,
588c2ecf20Sopenharmony_ci				  struct kernfs_node *target, char *path)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	struct kernfs_node *base, *kn;
618c2ecf20Sopenharmony_ci	char *s = path;
628c2ecf20Sopenharmony_ci	int len = 0;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* go up to the root, stop at the base */
658c2ecf20Sopenharmony_ci	base = parent;
668c2ecf20Sopenharmony_ci	while (base->parent) {
678c2ecf20Sopenharmony_ci		kn = target->parent;
688c2ecf20Sopenharmony_ci		while (kn->parent && base != kn)
698c2ecf20Sopenharmony_ci			kn = kn->parent;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		if (base == kn)
728c2ecf20Sopenharmony_ci			break;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		if ((s - path) + 3 >= PATH_MAX)
758c2ecf20Sopenharmony_ci			return -ENAMETOOLONG;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci		strcpy(s, "../");
788c2ecf20Sopenharmony_ci		s += 3;
798c2ecf20Sopenharmony_ci		base = base->parent;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* determine end of target string for reverse fillup */
838c2ecf20Sopenharmony_ci	kn = target;
848c2ecf20Sopenharmony_ci	while (kn->parent && kn != base) {
858c2ecf20Sopenharmony_ci		len += strlen(kn->name) + 1;
868c2ecf20Sopenharmony_ci		kn = kn->parent;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* check limits */
908c2ecf20Sopenharmony_ci	if (len < 2)
918c2ecf20Sopenharmony_ci		return -EINVAL;
928c2ecf20Sopenharmony_ci	len--;
938c2ecf20Sopenharmony_ci	if ((s - path) + len >= PATH_MAX)
948c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* reverse fillup of target string from target to base */
978c2ecf20Sopenharmony_ci	kn = target;
988c2ecf20Sopenharmony_ci	while (kn->parent && kn != base) {
998c2ecf20Sopenharmony_ci		int slen = strlen(kn->name);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		len -= slen;
1028c2ecf20Sopenharmony_ci		memcpy(s + len, kn->name, slen);
1038c2ecf20Sopenharmony_ci		if (len)
1048c2ecf20Sopenharmony_ci			s[--len] = '/';
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		kn = kn->parent;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic int kernfs_getlink(struct inode *inode, char *path)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	struct kernfs_node *kn = inode->i_private;
1158c2ecf20Sopenharmony_ci	struct kernfs_node *parent = kn->parent;
1168c2ecf20Sopenharmony_ci	struct kernfs_node *target = kn->symlink.target_kn;
1178c2ecf20Sopenharmony_ci	int error;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	mutex_lock(&kernfs_mutex);
1208c2ecf20Sopenharmony_ci	error = kernfs_get_target_path(parent, target, path);
1218c2ecf20Sopenharmony_ci	mutex_unlock(&kernfs_mutex);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return error;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic const char *kernfs_iop_get_link(struct dentry *dentry,
1278c2ecf20Sopenharmony_ci				       struct inode *inode,
1288c2ecf20Sopenharmony_ci				       struct delayed_call *done)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	char *body;
1318c2ecf20Sopenharmony_ci	int error;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (!dentry)
1348c2ecf20Sopenharmony_ci		return ERR_PTR(-ECHILD);
1358c2ecf20Sopenharmony_ci	body = kzalloc(PAGE_SIZE, GFP_KERNEL);
1368c2ecf20Sopenharmony_ci	if (!body)
1378c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1388c2ecf20Sopenharmony_ci	error = kernfs_getlink(inode, body);
1398c2ecf20Sopenharmony_ci	if (unlikely(error < 0)) {
1408c2ecf20Sopenharmony_ci		kfree(body);
1418c2ecf20Sopenharmony_ci		return ERR_PTR(error);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci	set_delayed_call(done, kfree_link, body);
1448c2ecf20Sopenharmony_ci	return body;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciconst struct inode_operations kernfs_symlink_iops = {
1488c2ecf20Sopenharmony_ci	.listxattr	= kernfs_iop_listxattr,
1498c2ecf20Sopenharmony_ci	.get_link	= kernfs_iop_get_link,
1508c2ecf20Sopenharmony_ci	.setattr	= kernfs_iop_setattr,
1518c2ecf20Sopenharmony_ci	.getattr	= kernfs_iop_getattr,
1528c2ecf20Sopenharmony_ci	.permission	= kernfs_iop_permission,
1538c2ecf20Sopenharmony_ci};
154