blob: af3570bb43035c6707cbe7ff812ac9d5b585a97c [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 */
25struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
26 const char *name,
27 struct sysfs_dirent *target)
28{
29 struct sysfs_dirent *sd;
30 struct sysfs_addrm_cxt acxt;
31 int error;
32
33 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
34 if (!sd)
35 return ERR_PTR(-ENOMEM);
36
37 if (parent->s_flags & SYSFS_FLAG_NS)
38 sd->s_ns = target->s_ns;
39 sd->s_symlink.target_sd = target;
40 kernfs_get(target); /* ref owned by symlink */
41
42 sysfs_addrm_start(&acxt);
43 error = sysfs_add_one(&acxt, sd, parent);
44 sysfs_addrm_finish(&acxt);
45
46 if (!error)
47 return sd;
48
49 kernfs_put(sd);
50 return ERR_PTR(error);
51}
52
53static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
54 struct sysfs_dirent *target_sd, char *path)
55{
56 struct sysfs_dirent *base, *sd;
57 char *s = path;
58 int len = 0;
59
60 /* go up to the root, stop at the base */
61 base = parent_sd;
62 while (base->s_parent) {
63 sd = target_sd->s_parent;
64 while (sd->s_parent && base != sd)
65 sd = sd->s_parent;
66
67 if (base == sd)
68 break;
69
70 strcpy(s, "../");
71 s += 3;
72 base = base->s_parent;
73 }
74
75 /* determine end of target string for reverse fillup */
76 sd = target_sd;
77 while (sd->s_parent && sd != base) {
78 len += strlen(sd->s_name) + 1;
79 sd = sd->s_parent;
80 }
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 */
90 sd = target_sd;
91 while (sd->s_parent && sd != base) {
92 int slen = strlen(sd->s_name);
93
94 len -= slen;
95 strncpy(s + len, sd->s_name, slen);
96 if (len)
97 s[--len] = '/';
98
99 sd = sd->s_parent;
100 }
101
102 return 0;
103}
104
105static int sysfs_getlink(struct dentry *dentry, char *path)
106{
107 struct sysfs_dirent *sd = dentry->d_fsdata;
108 struct sysfs_dirent *parent_sd = sd->s_parent;
109 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
110 int error;
111
112 mutex_lock(&sysfs_mutex);
113 error = sysfs_get_target_path(parent_sd, target_sd, path);
114 mutex_unlock(&sysfs_mutex);
115
116 return error;
117}
118
119static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
120{
121 int error = -ENOMEM;
122 unsigned long page = get_zeroed_page(GFP_KERNEL);
123 if (page) {
124 error = sysfs_getlink(dentry, (char *) page);
125 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
132static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
133 void *cookie)
134{
135 char *page = nd_get_link(nd);
136 if (!IS_ERR(page))
137 free_page((unsigned long)page);
138}
139
140const struct inode_operations sysfs_symlink_inode_operations = {
141 .setxattr = sysfs_setxattr,
142 .readlink = generic_readlink,
143 .follow_link = sysfs_follow_link,
144 .put_link = sysfs_put_link,
145 .setattr = sysfs_setattr,
146 .getattr = sysfs_getattr,
147 .permission = sysfs_permission,
148};