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.
25 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
27 struct kernfs_node *target)
29 struct kernfs_node *kn;
30 struct kernfs_addrm_cxt acxt;
33 kn = kernfs_new_node(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO,
36 return ERR_PTR(-ENOMEM);
38 if (kernfs_ns_enabled(parent))
40 kn->symlink.target_kn = target;
41 kernfs_get(target); /* ref owned by symlink */
44 if (kernfs_get_active(parent)) {
45 kernfs_addrm_start(&acxt);
46 error = kernfs_add_one(&acxt, kn, parent);
47 kernfs_addrm_finish(&acxt);
48 kernfs_put_active(parent);
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)
80 /* determine end of target string for reverse fillup */
82 while (kn->parent && kn != base) {
83 len += strlen(kn->name) + 1;
91 if ((s - path) + len > PATH_MAX)
94 /* reverse fillup of target string from target to base */
96 while (kn->parent && kn != base) {
97 int slen = strlen(kn->name);
100 strncpy(s + len, kn->name, slen);
110 static int kernfs_getlink(struct dentry *dentry, char *path)
112 struct kernfs_node *kn = dentry->d_fsdata;
113 struct kernfs_node *parent = kn->parent;
114 struct kernfs_node *target = kn->symlink.target_kn;
117 mutex_lock(&kernfs_mutex);
118 error = kernfs_get_target_path(parent, target, path);
119 mutex_unlock(&kernfs_mutex);
124 static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
127 unsigned long page = get_zeroed_page(GFP_KERNEL);
129 error = kernfs_getlink(dentry, (char *) page);
131 free_page((unsigned long)page);
133 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
137 static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
140 char *page = nd_get_link(nd);
142 free_page((unsigned long)page);
145 const struct inode_operations kernfs_symlink_iops = {
146 .setxattr = kernfs_iop_setxattr,
147 .removexattr = kernfs_iop_removexattr,
148 .getxattr = kernfs_iop_getxattr,
149 .listxattr = kernfs_iop_listxattr,
150 .readlink = generic_readlink,
151 .follow_link = kernfs_iop_follow_link,
152 .put_link = kernfs_iop_put_link,
153 .setattr = kernfs_iop_setattr,
154 .getattr = kernfs_iop_getattr,
155 .permission = kernfs_iop_permission,