2019-06-01 16:08:42 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-11-24 22:54:58 +08:00
|
|
|
/*
|
|
|
|
* fs/kernfs/symlink.c - kernfs symlink implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2007 SUSE Linux Products GmbH
|
|
|
|
* Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
|
|
|
|
*/
|
2013-11-29 03:54:35 +08:00
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
|
|
|
|
#include "kernfs-internal.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kernfs_create_link - create a symlink
|
|
|
|
* @parent: directory to create the symlink in
|
|
|
|
* @name: name of the symlink
|
|
|
|
* @target: target node for the symlink to point to
|
|
|
|
*
|
|
|
|
* Returns the created node on success, ERR_PTR() value on error.
|
2018-07-21 05:56:47 +08:00
|
|
|
* Ownership of the link matches ownership of the target.
|
2013-11-29 03:54:35 +08:00
|
|
|
*/
|
2013-12-12 03:11:53 +08:00
|
|
|
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
|
|
|
|
const char *name,
|
|
|
|
struct kernfs_node *target)
|
2013-11-29 03:54:35 +08:00
|
|
|
{
|
2013-12-12 03:11:53 +08:00
|
|
|
struct kernfs_node *kn;
|
2013-11-29 03:54:35 +08:00
|
|
|
int error;
|
2018-07-21 05:56:47 +08:00
|
|
|
kuid_t uid = GLOBAL_ROOT_UID;
|
|
|
|
kgid_t gid = GLOBAL_ROOT_GID;
|
2013-11-29 03:54:35 +08:00
|
|
|
|
2018-07-21 05:56:47 +08:00
|
|
|
if (target->iattr) {
|
2019-02-22 22:57:12 +08:00
|
|
|
uid = target->iattr->ia_uid;
|
|
|
|
gid = target->iattr->ia_gid;
|
2018-07-21 05:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
|
|
|
|
KERNFS_LINK);
|
2013-12-12 03:11:53 +08:00
|
|
|
if (!kn)
|
2013-11-29 03:54:35 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2013-11-30 06:19:09 +08:00
|
|
|
if (kernfs_ns_enabled(parent))
|
2013-12-12 03:11:54 +08:00
|
|
|
kn->ns = target->ns;
|
|
|
|
kn->symlink.target_kn = target;
|
2013-11-29 03:54:35 +08:00
|
|
|
kernfs_get(target); /* ref owned by symlink */
|
|
|
|
|
2014-02-04 03:02:58 +08:00
|
|
|
error = kernfs_add_one(kn);
|
2013-11-29 03:54:35 +08:00
|
|
|
if (!error)
|
2013-12-12 03:11:53 +08:00
|
|
|
return kn;
|
2013-11-29 03:54:35 +08:00
|
|
|
|
2013-12-12 03:11:53 +08:00
|
|
|
kernfs_put(kn);
|
2013-11-29 03:54:35 +08:00
|
|
|
return ERR_PTR(error);
|
|
|
|
}
|
|
|
|
|
2013-12-12 03:11:58 +08:00
|
|
|
static int kernfs_get_target_path(struct kernfs_node *parent,
|
|
|
|
struct kernfs_node *target, char *path)
|
2013-11-29 03:54:35 +08:00
|
|
|
{
|
2013-12-12 03:11:53 +08:00
|
|
|
struct kernfs_node *base, *kn;
|
2013-11-29 03:54:35 +08:00
|
|
|
char *s = path;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
/* go up to the root, stop at the base */
|
2013-12-12 03:11:53 +08:00
|
|
|
base = parent;
|
2013-12-12 03:11:54 +08:00
|
|
|
while (base->parent) {
|
|
|
|
kn = target->parent;
|
|
|
|
while (kn->parent && base != kn)
|
|
|
|
kn = kn->parent;
|
2013-11-29 03:54:35 +08:00
|
|
|
|
2013-12-12 03:11:53 +08:00
|
|
|
if (base == kn)
|
2013-11-29 03:54:35 +08:00
|
|
|
break;
|
|
|
|
|
2018-07-08 01:52:47 +08:00
|
|
|
if ((s - path) + 3 >= PATH_MAX)
|
|
|
|
return -ENAMETOOLONG;
|
|
|
|
|
2013-11-29 03:54:35 +08:00
|
|
|
strcpy(s, "../");
|
|
|
|
s += 3;
|
2013-12-12 03:11:54 +08:00
|
|
|
base = base->parent;
|
2013-11-29 03:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* determine end of target string for reverse fillup */
|
2013-12-12 03:11:53 +08:00
|
|
|
kn = target;
|
2013-12-12 03:11:54 +08:00
|
|
|
while (kn->parent && kn != base) {
|
|
|
|
len += strlen(kn->name) + 1;
|
|
|
|
kn = kn->parent;
|
2013-11-29 03:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check limits */
|
|
|
|
if (len < 2)
|
|
|
|
return -EINVAL;
|
|
|
|
len--;
|
2018-07-08 01:52:47 +08:00
|
|
|
if ((s - path) + len >= PATH_MAX)
|
2013-11-29 03:54:35 +08:00
|
|
|
return -ENAMETOOLONG;
|
|
|
|
|
|
|
|
/* reverse fillup of target string from target to base */
|
2013-12-12 03:11:53 +08:00
|
|
|
kn = target;
|
2013-12-12 03:11:54 +08:00
|
|
|
while (kn->parent && kn != base) {
|
|
|
|
int slen = strlen(kn->name);
|
2013-11-29 03:54:35 +08:00
|
|
|
|
|
|
|
len -= slen;
|
2018-07-02 04:57:13 +08:00
|
|
|
memcpy(s + len, kn->name, slen);
|
2013-11-29 03:54:35 +08:00
|
|
|
if (len)
|
|
|
|
s[--len] = '/';
|
|
|
|
|
2013-12-12 03:11:54 +08:00
|
|
|
kn = kn->parent;
|
2013-11-29 03:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:49:49 +08:00
|
|
|
static int kernfs_getlink(struct inode *inode, char *path)
|
2013-11-29 03:54:35 +08:00
|
|
|
{
|
2017-07-13 02:49:49 +08:00
|
|
|
struct kernfs_node *kn = inode->i_private;
|
2013-12-12 03:11:54 +08:00
|
|
|
struct kernfs_node *parent = kn->parent;
|
|
|
|
struct kernfs_node *target = kn->symlink.target_kn;
|
2013-11-29 03:54:35 +08:00
|
|
|
int error;
|
|
|
|
|
2013-12-12 03:11:57 +08:00
|
|
|
mutex_lock(&kernfs_mutex);
|
2013-12-12 03:11:58 +08:00
|
|
|
error = kernfs_get_target_path(parent, target, path);
|
2013-12-12 03:11:57 +08:00
|
|
|
mutex_unlock(&kernfs_mutex);
|
2013-11-29 03:54:35 +08:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2015-11-17 23:20:54 +08:00
|
|
|
static const char *kernfs_iop_get_link(struct dentry *dentry,
|
2015-12-30 04:58:39 +08:00
|
|
|
struct inode *inode,
|
|
|
|
struct delayed_call *done)
|
2013-11-29 03:54:35 +08:00
|
|
|
{
|
2015-12-30 04:58:39 +08:00
|
|
|
char *body;
|
|
|
|
int error;
|
2015-11-17 23:20:54 +08:00
|
|
|
|
|
|
|
if (!dentry)
|
|
|
|
return ERR_PTR(-ECHILD);
|
2015-12-30 04:58:39 +08:00
|
|
|
body = kzalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
if (!body)
|
2015-05-03 01:32:22 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2017-07-13 02:49:49 +08:00
|
|
|
error = kernfs_getlink(inode, body);
|
2015-05-03 01:32:22 +08:00
|
|
|
if (unlikely(error < 0)) {
|
2015-12-30 04:58:39 +08:00
|
|
|
kfree(body);
|
2015-05-03 01:32:22 +08:00
|
|
|
return ERR_PTR(error);
|
2013-11-29 03:54:35 +08:00
|
|
|
}
|
2015-12-30 04:58:39 +08:00
|
|
|
set_delayed_call(done, kfree_link, body);
|
|
|
|
return body;
|
2013-11-29 03:54:35 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 03:11:57 +08:00
|
|
|
const struct inode_operations kernfs_symlink_iops = {
|
2013-12-12 03:11:58 +08:00
|
|
|
.listxattr = kernfs_iop_listxattr,
|
2015-11-17 23:20:54 +08:00
|
|
|
.get_link = kernfs_iop_get_link,
|
2013-12-12 03:11:58 +08:00
|
|
|
.setattr = kernfs_iop_setattr,
|
|
|
|
.getattr = kernfs_iop_getattr,
|
|
|
|
.permission = kernfs_iop_permission,
|
2013-11-29 03:54:35 +08:00
|
|
|
};
|