Commit | Line | Data |
---|---|---|
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 |
25 | struct 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; |
7653fe9d | 30 | struct kernfs_addrm_cxt acxt; |
2072f1af TH |
31 | int error; |
32 | ||
c637b8ac TH |
33 | kn = kernfs_new_node(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO, |
34 | KERNFS_LINK); | |
324a56e1 | 35 | if (!kn) |
2072f1af TH |
36 | return ERR_PTR(-ENOMEM); |
37 | ||
ac9bba03 | 38 | if (kernfs_ns_enabled(parent)) |
adc5e8b5 TH |
39 | kn->ns = target->ns; |
40 | kn->symlink.target_kn = target; | |
2072f1af TH |
41 | kernfs_get(target); /* ref owned by symlink */ |
42 | ||
798c75a0 GKH |
43 | kernfs_addrm_start(&acxt); |
44 | error = kernfs_add_one(&acxt, kn, parent); | |
45 | kernfs_addrm_finish(&acxt); | |
7653fe9d | 46 | |
2072f1af | 47 | if (!error) |
324a56e1 | 48 | return kn; |
2072f1af | 49 | |
324a56e1 | 50 | kernfs_put(kn); |
2072f1af TH |
51 | return ERR_PTR(error); |
52 | } | |
53 | ||
c637b8ac TH |
54 | static int kernfs_get_target_path(struct kernfs_node *parent, |
55 | struct kernfs_node *target, char *path) | |
2072f1af | 56 | { |
324a56e1 | 57 | struct kernfs_node *base, *kn; |
2072f1af TH |
58 | char *s = path; |
59 | int len = 0; | |
60 | ||
61 | /* go up to the root, stop at the base */ | |
324a56e1 | 62 | base = parent; |
adc5e8b5 TH |
63 | while (base->parent) { |
64 | kn = target->parent; | |
65 | while (kn->parent && base != kn) | |
66 | kn = kn->parent; | |
2072f1af | 67 | |
324a56e1 | 68 | if (base == kn) |
2072f1af TH |
69 | break; |
70 | ||
71 | strcpy(s, "../"); | |
72 | s += 3; | |
adc5e8b5 | 73 | base = base->parent; |
2072f1af TH |
74 | } |
75 | ||
76 | /* determine end of target string for reverse fillup */ | |
324a56e1 | 77 | kn = target; |
adc5e8b5 TH |
78 | while (kn->parent && kn != base) { |
79 | len += strlen(kn->name) + 1; | |
80 | kn = kn->parent; | |
2072f1af TH |
81 | } |
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 */ | |
324a56e1 | 91 | kn = target; |
adc5e8b5 TH |
92 | while (kn->parent && kn != base) { |
93 | int slen = strlen(kn->name); | |
2072f1af TH |
94 | |
95 | len -= slen; | |
adc5e8b5 | 96 | strncpy(s + len, kn->name, slen); |
2072f1af TH |
97 | if (len) |
98 | s[--len] = '/'; | |
99 | ||
adc5e8b5 | 100 | kn = kn->parent; |
2072f1af TH |
101 | } |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
c637b8ac | 106 | static int kernfs_getlink(struct dentry *dentry, char *path) |
2072f1af | 107 | { |
324a56e1 | 108 | struct kernfs_node *kn = dentry->d_fsdata; |
adc5e8b5 TH |
109 | struct kernfs_node *parent = kn->parent; |
110 | struct kernfs_node *target = kn->symlink.target_kn; | |
2072f1af TH |
111 | int error; |
112 | ||
a797bfc3 | 113 | mutex_lock(&kernfs_mutex); |
c637b8ac | 114 | error = kernfs_get_target_path(parent, target, path); |
a797bfc3 | 115 | mutex_unlock(&kernfs_mutex); |
2072f1af TH |
116 | |
117 | return error; | |
118 | } | |
119 | ||
c637b8ac | 120 | static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd) |
2072f1af TH |
121 | { |
122 | int error = -ENOMEM; | |
123 | unsigned long page = get_zeroed_page(GFP_KERNEL); | |
124 | if (page) { | |
c637b8ac | 125 | error = kernfs_getlink(dentry, (char *) page); |
2072f1af TH |
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 | ||
c637b8ac TH |
133 | static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd, |
134 | void *cookie) | |
2072f1af TH |
135 | { |
136 | char *page = nd_get_link(nd); | |
137 | if (!IS_ERR(page)) | |
138 | free_page((unsigned long)page); | |
139 | } | |
140 | ||
a797bfc3 | 141 | const struct inode_operations kernfs_symlink_iops = { |
c637b8ac TH |
142 | .setxattr = kernfs_iop_setxattr, |
143 | .removexattr = kernfs_iop_removexattr, | |
144 | .getxattr = kernfs_iop_getxattr, | |
145 | .listxattr = kernfs_iop_listxattr, | |
2072f1af | 146 | .readlink = generic_readlink, |
c637b8ac TH |
147 | .follow_link = kernfs_iop_follow_link, |
148 | .put_link = kernfs_iop_put_link, | |
149 | .setattr = kernfs_iop_setattr, | |
150 | .getattr = kernfs_iop_getattr, | |
151 | .permission = kernfs_iop_permission, | |
2072f1af | 152 | }; |