x86/mm/doc: Clean up the x86-64 virtual memory layout descriptions
[linux-2.6-microblaze.git] / fs / kernfs / symlink.c
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  */
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  * Ownership of the link matches ownership of the target.
25  */
26 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
27                                        const char *name,
28                                        struct kernfs_node *target)
29 {
30         struct kernfs_node *kn;
31         int error;
32         kuid_t uid = GLOBAL_ROOT_UID;
33         kgid_t gid = GLOBAL_ROOT_GID;
34
35         if (target->iattr) {
36                 uid = target->iattr->ia_iattr.ia_uid;
37                 gid = target->iattr->ia_iattr.ia_gid;
38         }
39
40         kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
41                              KERNFS_LINK);
42         if (!kn)
43                 return ERR_PTR(-ENOMEM);
44
45         if (kernfs_ns_enabled(parent))
46                 kn->ns = target->ns;
47         kn->symlink.target_kn = target;
48         kernfs_get(target);     /* ref owned by symlink */
49
50         error = kernfs_add_one(kn);
51         if (!error)
52                 return kn;
53
54         kernfs_put(kn);
55         return ERR_PTR(error);
56 }
57
58 static int kernfs_get_target_path(struct kernfs_node *parent,
59                                   struct kernfs_node *target, char *path)
60 {
61         struct kernfs_node *base, *kn;
62         char *s = path;
63         int len = 0;
64
65         /* go up to the root, stop at the base */
66         base = parent;
67         while (base->parent) {
68                 kn = target->parent;
69                 while (kn->parent && base != kn)
70                         kn = kn->parent;
71
72                 if (base == kn)
73                         break;
74
75                 strcpy(s, "../");
76                 s += 3;
77                 base = base->parent;
78         }
79
80         /* determine end of target string for reverse fillup */
81         kn = target;
82         while (kn->parent && kn != base) {
83                 len += strlen(kn->name) + 1;
84                 kn = kn->parent;
85         }
86
87         /* check limits */
88         if (len < 2)
89                 return -EINVAL;
90         len--;
91         if ((s - path) + len > PATH_MAX)
92                 return -ENAMETOOLONG;
93
94         /* reverse fillup of target string from target to base */
95         kn = target;
96         while (kn->parent && kn != base) {
97                 int slen = strlen(kn->name);
98
99                 len -= slen;
100                 memcpy(s + len, kn->name, slen);
101                 if (len)
102                         s[--len] = '/';
103
104                 kn = kn->parent;
105         }
106
107         return 0;
108 }
109
110 static int kernfs_getlink(struct inode *inode, char *path)
111 {
112         struct kernfs_node *kn = inode->i_private;
113         struct kernfs_node *parent = kn->parent;
114         struct kernfs_node *target = kn->symlink.target_kn;
115         int error;
116
117         mutex_lock(&kernfs_mutex);
118         error = kernfs_get_target_path(parent, target, path);
119         mutex_unlock(&kernfs_mutex);
120
121         return error;
122 }
123
124 static const char *kernfs_iop_get_link(struct dentry *dentry,
125                                        struct inode *inode,
126                                        struct delayed_call *done)
127 {
128         char *body;
129         int error;
130
131         if (!dentry)
132                 return ERR_PTR(-ECHILD);
133         body = kzalloc(PAGE_SIZE, GFP_KERNEL);
134         if (!body)
135                 return ERR_PTR(-ENOMEM);
136         error = kernfs_getlink(inode, body);
137         if (unlikely(error < 0)) {
138                 kfree(body);
139                 return ERR_PTR(error);
140         }
141         set_delayed_call(done, kfree_link, body);
142         return body;
143 }
144
145 const struct inode_operations kernfs_symlink_iops = {
146         .listxattr      = kernfs_iop_listxattr,
147         .get_link       = kernfs_iop_get_link,
148         .setattr        = kernfs_iop_setattr,
149         .getattr        = kernfs_iop_getattr,
150         .permission     = kernfs_iop_permission,
151 };