Linux 4.2.1
[linux/fpc-iii.git] / fs / kernfs / symlink.c
blobdb272528ab5bb01c192b5502650f29e0784663ce
1 /*
2 * fs/kernfs/symlink.c - kernfs symlink implementation
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>
8 * This file is released under the GPLv2.
9 */
11 #include <linux/fs.h>
12 #include <linux/gfp.h>
13 #include <linux/namei.h>
15 #include "kernfs-internal.h"
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
23 * Returns the created node on success, ERR_PTR() value on error.
25 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
26 const char *name,
27 struct kernfs_node *target)
29 struct kernfs_node *kn;
30 int error;
32 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
33 if (!kn)
34 return ERR_PTR(-ENOMEM);
36 if (kernfs_ns_enabled(parent))
37 kn->ns = target->ns;
38 kn->symlink.target_kn = target;
39 kernfs_get(target); /* ref owned by symlink */
41 error = kernfs_add_one(kn);
42 if (!error)
43 return kn;
45 kernfs_put(kn);
46 return ERR_PTR(error);
49 static int kernfs_get_target_path(struct kernfs_node *parent,
50 struct kernfs_node *target, char *path)
52 struct kernfs_node *base, *kn;
53 char *s = path;
54 int len = 0;
56 /* go up to the root, stop at the base */
57 base = parent;
58 while (base->parent) {
59 kn = target->parent;
60 while (kn->parent && base != kn)
61 kn = kn->parent;
63 if (base == kn)
64 break;
66 strcpy(s, "../");
67 s += 3;
68 base = base->parent;
71 /* determine end of target string for reverse fillup */
72 kn = target;
73 while (kn->parent && kn != base) {
74 len += strlen(kn->name) + 1;
75 kn = kn->parent;
78 /* check limits */
79 if (len < 2)
80 return -EINVAL;
81 len--;
82 if ((s - path) + len > PATH_MAX)
83 return -ENAMETOOLONG;
85 /* reverse fillup of target string from target to base */
86 kn = target;
87 while (kn->parent && kn != base) {
88 int slen = strlen(kn->name);
90 len -= slen;
91 strncpy(s + len, kn->name, slen);
92 if (len)
93 s[--len] = '/';
95 kn = kn->parent;
98 return 0;
101 static int kernfs_getlink(struct dentry *dentry, char *path)
103 struct kernfs_node *kn = dentry->d_fsdata;
104 struct kernfs_node *parent = kn->parent;
105 struct kernfs_node *target = kn->symlink.target_kn;
106 int error;
108 mutex_lock(&kernfs_mutex);
109 error = kernfs_get_target_path(parent, target, path);
110 mutex_unlock(&kernfs_mutex);
112 return error;
115 static const char *kernfs_iop_follow_link(struct dentry *dentry, void **cookie)
117 int error = -ENOMEM;
118 unsigned long page = get_zeroed_page(GFP_KERNEL);
119 if (!page)
120 return ERR_PTR(-ENOMEM);
121 error = kernfs_getlink(dentry, (char *)page);
122 if (unlikely(error < 0)) {
123 free_page((unsigned long)page);
124 return ERR_PTR(error);
126 return *cookie = (char *)page;
129 const struct inode_operations kernfs_symlink_iops = {
130 .setxattr = kernfs_iop_setxattr,
131 .removexattr = kernfs_iop_removexattr,
132 .getxattr = kernfs_iop_getxattr,
133 .listxattr = kernfs_iop_listxattr,
134 .readlink = generic_readlink,
135 .follow_link = kernfs_iop_follow_link,
136 .put_link = free_page_put_link,
137 .setattr = kernfs_iop_setattr,
138 .getattr = kernfs_iop_getattr,
139 .permission = kernfs_iop_permission,