replace ->follow_link() with new method that could stay in RCU mode
[linux-block.git] / fs / kernfs / symlink.c
CommitLineData
b8441ed2
TH
1/*
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 */
2072f1af
TH
10
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 */
324a56e1
TH
25struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
26 const char *name,
27 struct kernfs_node *target)
2072f1af 28{
324a56e1 29 struct kernfs_node *kn;
2072f1af
TH
30 int error;
31
db4aad20 32 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
324a56e1 33 if (!kn)
2072f1af
TH
34 return ERR_PTR(-ENOMEM);
35
ac9bba03 36 if (kernfs_ns_enabled(parent))
adc5e8b5
TH
37 kn->ns = target->ns;
38 kn->symlink.target_kn = target;
2072f1af
TH
39 kernfs_get(target); /* ref owned by symlink */
40
988cd7af 41 error = kernfs_add_one(kn);
2072f1af 42 if (!error)
324a56e1 43 return kn;
2072f1af 44
324a56e1 45 kernfs_put(kn);
2072f1af
TH
46 return ERR_PTR(error);
47}
48
c637b8ac
TH
49static int kernfs_get_target_path(struct kernfs_node *parent,
50 struct kernfs_node *target, char *path)
2072f1af 51{
324a56e1 52 struct kernfs_node *base, *kn;
2072f1af
TH
53 char *s = path;
54 int len = 0;
55
56 /* go up to the root, stop at the base */
324a56e1 57 base = parent;
adc5e8b5
TH
58 while (base->parent) {
59 kn = target->parent;
60 while (kn->parent && base != kn)
61 kn = kn->parent;
2072f1af 62
324a56e1 63 if (base == kn)
2072f1af
TH
64 break;
65
66 strcpy(s, "../");
67 s += 3;
adc5e8b5 68 base = base->parent;
2072f1af
TH
69 }
70
71 /* determine end of target string for reverse fillup */
324a56e1 72 kn = target;
adc5e8b5
TH
73 while (kn->parent && kn != base) {
74 len += strlen(kn->name) + 1;
75 kn = kn->parent;
2072f1af
TH
76 }
77
78 /* check limits */
79 if (len < 2)
80 return -EINVAL;
81 len--;
82 if ((s - path) + len > PATH_MAX)
83 return -ENAMETOOLONG;
84
85 /* reverse fillup of target string from target to base */
324a56e1 86 kn = target;
adc5e8b5
TH
87 while (kn->parent && kn != base) {
88 int slen = strlen(kn->name);
2072f1af
TH
89
90 len -= slen;
adc5e8b5 91 strncpy(s + len, kn->name, slen);
2072f1af
TH
92 if (len)
93 s[--len] = '/';
94
adc5e8b5 95 kn = kn->parent;
2072f1af
TH
96 }
97
98 return 0;
99}
100
c637b8ac 101static int kernfs_getlink(struct dentry *dentry, char *path)
2072f1af 102{
324a56e1 103 struct kernfs_node *kn = dentry->d_fsdata;
adc5e8b5
TH
104 struct kernfs_node *parent = kn->parent;
105 struct kernfs_node *target = kn->symlink.target_kn;
2072f1af
TH
106 int error;
107
a797bfc3 108 mutex_lock(&kernfs_mutex);
c637b8ac 109 error = kernfs_get_target_path(parent, target, path);
a797bfc3 110 mutex_unlock(&kernfs_mutex);
2072f1af
TH
111
112 return error;
113}
114
6b255391
AV
115static const char *kernfs_iop_get_link(struct dentry *dentry,
116 struct inode *inode, void **cookie)
2072f1af
TH
117{
118 int error = -ENOMEM;
6b255391
AV
119 unsigned long page;
120
121 if (!dentry)
122 return ERR_PTR(-ECHILD);
123 page = get_zeroed_page(GFP_KERNEL);
680baacb
AV
124 if (!page)
125 return ERR_PTR(-ENOMEM);
126 error = kernfs_getlink(dentry, (char *)page);
127 if (unlikely(error < 0)) {
128 free_page((unsigned long)page);
129 return ERR_PTR(error);
2072f1af 130 }
680baacb 131 return *cookie = (char *)page;
2072f1af
TH
132}
133
a797bfc3 134const struct inode_operations kernfs_symlink_iops = {
c637b8ac
TH
135 .setxattr = kernfs_iop_setxattr,
136 .removexattr = kernfs_iop_removexattr,
137 .getxattr = kernfs_iop_getxattr,
138 .listxattr = kernfs_iop_listxattr,
2072f1af 139 .readlink = generic_readlink,
6b255391 140 .get_link = kernfs_iop_get_link,
ecc087ff 141 .put_link = free_page_put_link,
c637b8ac
TH
142 .setattr = kernfs_iop_setattr,
143 .getattr = kernfs_iop_getattr,
144 .permission = kernfs_iop_permission,
2072f1af 145};