blob: 4d457055acb9dfeefeb94d5a3b6bdbea96e20624 [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;
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -080030 struct kernfs_addrm_cxt acxt;
Tejun Heo2072f1a2013-11-28 14:54:35 -050031 int error;
32
Tejun Heodb4aad22014-01-17 09:58:25 -050033 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
Tejun Heo324a56e2013-12-11 14:11:53 -050034 if (!kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050035 return ERR_PTR(-ENOMEM);
36
Tejun Heoac9bba02013-11-29 17:19:09 -050037 if (kernfs_ns_enabled(parent))
Tejun Heoadc5e8b2013-12-11 14:11:54 -050038 kn->ns = target->ns;
39 kn->symlink.target_kn = target;
Tejun Heo2072f1a2013-11-28 14:54:35 -050040 kernfs_get(target); /* ref owned by symlink */
41
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -080042 kernfs_addrm_start(&acxt);
Tejun Heodb4aad22014-01-17 09:58:25 -050043 error = kernfs_add_one(&acxt, kn);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -080044 kernfs_addrm_finish(&acxt);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -080045
Tejun Heo2072f1a2013-11-28 14:54:35 -050046 if (!error)
Tejun Heo324a56e2013-12-11 14:11:53 -050047 return kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050048
Tejun Heo324a56e2013-12-11 14:11:53 -050049 kernfs_put(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050050 return ERR_PTR(error);
51}
52
Tejun Heoc637b8a2013-12-11 14:11:58 -050053static int kernfs_get_target_path(struct kernfs_node *parent,
54 struct kernfs_node *target, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -050055{
Tejun Heo324a56e2013-12-11 14:11:53 -050056 struct kernfs_node *base, *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050057 char *s = path;
58 int len = 0;
59
60 /* go up to the root, stop at the base */
Tejun Heo324a56e2013-12-11 14:11:53 -050061 base = parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050062 while (base->parent) {
63 kn = target->parent;
64 while (kn->parent && base != kn)
65 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050066
Tejun Heo324a56e2013-12-11 14:11:53 -050067 if (base == kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050068 break;
69
70 strcpy(s, "../");
71 s += 3;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050072 base = base->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050073 }
74
75 /* determine end of target string for reverse fillup */
Tejun Heo324a56e2013-12-11 14:11:53 -050076 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050077 while (kn->parent && kn != base) {
78 len += strlen(kn->name) + 1;
79 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050080 }
81
82 /* check limits */
83 if (len < 2)
84 return -EINVAL;
85 len--;
86 if ((s - path) + len > PATH_MAX)
87 return -ENAMETOOLONG;
88
89 /* reverse fillup of target string from target to base */
Tejun Heo324a56e2013-12-11 14:11:53 -050090 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050091 while (kn->parent && kn != base) {
92 int slen = strlen(kn->name);
Tejun Heo2072f1a2013-11-28 14:54:35 -050093
94 len -= slen;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050095 strncpy(s + len, kn->name, slen);
Tejun Heo2072f1a2013-11-28 14:54:35 -050096 if (len)
97 s[--len] = '/';
98
Tejun Heoadc5e8b2013-12-11 14:11:54 -050099 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500100 }
101
102 return 0;
103}
104
Tejun Heoc637b8a2013-12-11 14:11:58 -0500105static int kernfs_getlink(struct dentry *dentry, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500106{
Tejun Heo324a56e2013-12-11 14:11:53 -0500107 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500108 struct kernfs_node *parent = kn->parent;
109 struct kernfs_node *target = kn->symlink.target_kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500110 int error;
111
Tejun Heoa797bfc2013-12-11 14:11:57 -0500112 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500113 error = kernfs_get_target_path(parent, target, path);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500114 mutex_unlock(&kernfs_mutex);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500115
116 return error;
117}
118
Tejun Heoc637b8a2013-12-11 14:11:58 -0500119static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500120{
121 int error = -ENOMEM;
122 unsigned long page = get_zeroed_page(GFP_KERNEL);
123 if (page) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500124 error = kernfs_getlink(dentry, (char *) page);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500125 if (error < 0)
126 free_page((unsigned long)page);
127 }
128 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
129 return NULL;
130}
131
Tejun Heoc637b8a2013-12-11 14:11:58 -0500132static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
133 void *cookie)
Tejun Heo2072f1a2013-11-28 14:54:35 -0500134{
135 char *page = nd_get_link(nd);
136 if (!IS_ERR(page))
137 free_page((unsigned long)page);
138}
139
Tejun Heoa797bfc2013-12-11 14:11:57 -0500140const struct inode_operations kernfs_symlink_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500141 .setxattr = kernfs_iop_setxattr,
142 .removexattr = kernfs_iop_removexattr,
143 .getxattr = kernfs_iop_getxattr,
144 .listxattr = kernfs_iop_listxattr,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500145 .readlink = generic_readlink,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500146 .follow_link = kernfs_iop_follow_link,
147 .put_link = kernfs_iop_put_link,
148 .setattr = kernfs_iop_setattr,
149 .getattr = kernfs_iop_getattr,
150 .permission = kernfs_iop_permission,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500151};