2 * fs/kernfs/symlink.c - kernfs symlink implementation
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>
8 * This file is released under the GPLv2.
12 #include <linux/gfp.h>
13 #include <linux/namei.h>
15 #include "kernfs-internal.h"
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
23 * Returns the created node on success, ERR_PTR() value on error.
24 * Ownership of the link matches ownership of the target.
26 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
28 struct kernfs_node *target)
30 struct kernfs_node *kn;
32 kuid_t uid = GLOBAL_ROOT_UID;
33 kgid_t gid = GLOBAL_ROOT_GID;
36 uid = target->iattr->ia_iattr.ia_uid;
37 gid = target->iattr->ia_iattr.ia_gid;
40 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
43 return ERR_PTR(-ENOMEM);
45 if (kernfs_ns_enabled(parent))
47 kn->symlink.target_kn = target;
48 kernfs_get(target); /* ref owned by symlink */
50 error = kernfs_add_one(kn);
55 return ERR_PTR(error);
58 static int kernfs_get_target_path(struct kernfs_node *parent,
59 struct kernfs_node *target, char *path)
61 struct kernfs_node *base, *kn;
65 /* go up to the root, stop at the base */
67 while (base->parent) {
69 while (kn->parent && base != kn)
75 if ((s - path) + 3 >= PATH_MAX)
83 /* determine end of target string for reverse fillup */
85 while (kn->parent && kn != base) {
86 len += strlen(kn->name) + 1;
94 if ((s - path) + len >= PATH_MAX)
97 /* reverse fillup of target string from target to base */
99 while (kn->parent && kn != base) {
100 int slen = strlen(kn->name);
103 memcpy(s + len, kn->name, slen);
113 static int kernfs_getlink(struct inode *inode, char *path)
115 struct kernfs_node *kn = inode->i_private;
116 struct kernfs_node *parent = kn->parent;
117 struct kernfs_node *target = kn->symlink.target_kn;
120 mutex_lock(&kernfs_mutex);
121 error = kernfs_get_target_path(parent, target, path);
122 mutex_unlock(&kernfs_mutex);
127 static const char *kernfs_iop_get_link(struct dentry *dentry,
129 struct delayed_call *done)
135 return ERR_PTR(-ECHILD);
136 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
138 return ERR_PTR(-ENOMEM);
139 error = kernfs_getlink(inode, body);
140 if (unlikely(error < 0)) {
142 return ERR_PTR(error);
144 set_delayed_call(done, kfree_link, body);
148 const struct inode_operations kernfs_symlink_iops = {
149 .listxattr = kernfs_iop_listxattr,
150 .get_link = kernfs_iop_get_link,
151 .setattr = kernfs_iop_setattr,
152 .getattr = kernfs_iop_getattr,
153 .permission = kernfs_iop_permission,