blob: a92284d3c73daedbfccc1a20f9133c00ab5144e6 [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;
Tejun Heoc525aad2013-12-11 14:11:55 -050030 struct kernfs_addrm_cxt acxt;
Tejun Heo2072f1a2013-11-28 14:54:35 -050031 int error;
32
Tejun Heo324a56e2013-12-11 14:11:53 -050033 kn = sysfs_new_dirent(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO,
Tejun Heodf23fc32013-12-11 14:11:56 -050034 KERNFS_LINK);
Tejun Heo324a56e2013-12-11 14:11:53 -050035 if (!kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050036 return ERR_PTR(-ENOMEM);
37
Tejun Heoac9bba02013-11-29 17:19:09 -050038 if (kernfs_ns_enabled(parent))
Tejun Heoadc5e8b2013-12-11 14:11:54 -050039 kn->ns = target->ns;
40 kn->symlink.target_kn = target;
Tejun Heo2072f1a2013-11-28 14:54:35 -050041 kernfs_get(target); /* ref owned by symlink */
42
43 sysfs_addrm_start(&acxt);
Tejun Heo324a56e2013-12-11 14:11:53 -050044 error = sysfs_add_one(&acxt, kn, parent);
Tejun Heo2072f1a2013-11-28 14:54:35 -050045 sysfs_addrm_finish(&acxt);
46
47 if (!error)
Tejun Heo324a56e2013-12-11 14:11:53 -050048 return kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050049
Tejun Heo324a56e2013-12-11 14:11:53 -050050 kernfs_put(kn);
Tejun Heo2072f1a2013-11-28 14:54:35 -050051 return ERR_PTR(error);
52}
53
Tejun Heo324a56e2013-12-11 14:11:53 -050054static int sysfs_get_target_path(struct kernfs_node *parent,
55 struct kernfs_node *target, char *path)
Tejun Heo2072f1a2013-11-28 14:54:35 -050056{
Tejun Heo324a56e2013-12-11 14:11:53 -050057 struct kernfs_node *base, *kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -050058 char *s = path;
59 int len = 0;
60
61 /* go up to the root, stop at the base */
Tejun Heo324a56e2013-12-11 14:11:53 -050062 base = parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050063 while (base->parent) {
64 kn = target->parent;
65 while (kn->parent && base != kn)
66 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050067
Tejun Heo324a56e2013-12-11 14:11:53 -050068 if (base == kn)
Tejun Heo2072f1a2013-11-28 14:54:35 -050069 break;
70
71 strcpy(s, "../");
72 s += 3;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050073 base = base->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050074 }
75
76 /* determine end of target string for reverse fillup */
Tejun Heo324a56e2013-12-11 14:11:53 -050077 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050078 while (kn->parent && kn != base) {
79 len += strlen(kn->name) + 1;
80 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -050081 }
82
83 /* check limits */
84 if (len < 2)
85 return -EINVAL;
86 len--;
87 if ((s - path) + len > PATH_MAX)
88 return -ENAMETOOLONG;
89
90 /* reverse fillup of target string from target to base */
Tejun Heo324a56e2013-12-11 14:11:53 -050091 kn = target;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050092 while (kn->parent && kn != base) {
93 int slen = strlen(kn->name);
Tejun Heo2072f1a2013-11-28 14:54:35 -050094
95 len -= slen;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050096 strncpy(s + len, kn->name, slen);
Tejun Heo2072f1a2013-11-28 14:54:35 -050097 if (len)
98 s[--len] = '/';
99
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500100 kn = kn->parent;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500101 }
102
103 return 0;
104}
105
106static int sysfs_getlink(struct dentry *dentry, char *path)
107{
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500109 struct kernfs_node *parent = kn->parent;
110 struct kernfs_node *target = kn->symlink.target_kn;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500111 int error;
112
113 mutex_lock(&sysfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500114 error = sysfs_get_target_path(parent, target, path);
Tejun Heo2072f1a2013-11-28 14:54:35 -0500115 mutex_unlock(&sysfs_mutex);
116
117 return error;
118}
119
120static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
121{
122 int error = -ENOMEM;
123 unsigned long page = get_zeroed_page(GFP_KERNEL);
124 if (page) {
125 error = sysfs_getlink(dentry, (char *) page);
126 if (error < 0)
127 free_page((unsigned long)page);
128 }
129 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
130 return NULL;
131}
132
133static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
134 void *cookie)
135{
136 char *page = nd_get_link(nd);
137 if (!IS_ERR(page))
138 free_page((unsigned long)page);
139}
140
141const struct inode_operations sysfs_symlink_inode_operations = {
142 .setxattr = sysfs_setxattr,
Tejun Heo23223922013-11-23 17:40:02 -0500143 .removexattr = sysfs_removexattr,
144 .getxattr = sysfs_getxattr,
145 .listxattr = sysfs_listxattr,
Tejun Heo2072f1a2013-11-28 14:54:35 -0500146 .readlink = generic_readlink,
147 .follow_link = sysfs_follow_link,
148 .put_link = sysfs_put_link,
149 .setattr = sysfs_setattr,
150 .getattr = sysfs_getattr,
151 .permission = sysfs_permission,
152};