V4L/DVB (6102): dvb: remove some unneeded vmalloc() return value casts from av7110
[deliverable/linux.git] / fs / sysfs / symlink.c
1 /*
2 * symlink.c - operations for sysfs symlinks.
3 */
4
5 #include <linux/fs.h>
6 #include <linux/mount.h>
7 #include <linux/module.h>
8 #include <linux/kobject.h>
9 #include <linux/namei.h>
10 #include <asm/semaphore.h>
11
12 #include "sysfs.h"
13
14 static int object_depth(struct sysfs_dirent *sd)
15 {
16 int depth = 0;
17
18 for (; sd->s_parent; sd = sd->s_parent)
19 depth++;
20
21 return depth;
22 }
23
24 static int object_path_length(struct sysfs_dirent * sd)
25 {
26 int length = 1;
27
28 for (; sd->s_parent; sd = sd->s_parent)
29 length += strlen(sd->s_name) + 1;
30
31 return length;
32 }
33
34 static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
35 {
36 --length;
37 for (; sd->s_parent; sd = sd->s_parent) {
38 int cur = strlen(sd->s_name);
39
40 /* back up enough to print this bus id with '/' */
41 length -= cur;
42 strncpy(buffer + length, sd->s_name, cur);
43 *(buffer + --length) = '/';
44 }
45 }
46
47 /**
48 * sysfs_create_link - create symlink between two objects.
49 * @kobj: object whose directory we're creating the link in.
50 * @target: object we're pointing to.
51 * @name: name of the symlink.
52 */
53 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
54 {
55 struct sysfs_dirent *parent_sd = NULL;
56 struct sysfs_dirent *target_sd = NULL;
57 struct sysfs_dirent *sd = NULL;
58 struct sysfs_addrm_cxt acxt;
59 int error;
60
61 BUG_ON(!name);
62
63 if (!kobj) {
64 if (sysfs_mount && sysfs_mount->mnt_sb)
65 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
66 } else
67 parent_sd = kobj->sd;
68
69 error = -EFAULT;
70 if (!parent_sd)
71 goto out_put;
72
73 /* target->sd can go away beneath us but is protected with
74 * sysfs_assoc_lock. Fetch target_sd from it.
75 */
76 spin_lock(&sysfs_assoc_lock);
77 if (target->sd)
78 target_sd = sysfs_get(target->sd);
79 spin_unlock(&sysfs_assoc_lock);
80
81 error = -ENOENT;
82 if (!target_sd)
83 goto out_put;
84
85 error = -ENOMEM;
86 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
87 if (!sd)
88 goto out_put;
89
90 sd->s_elem.symlink.target_sd = target_sd;
91 target_sd = NULL; /* reference is now owned by the symlink */
92
93 sysfs_addrm_start(&acxt, parent_sd);
94
95 if (!sysfs_find_dirent(parent_sd, name)) {
96 sysfs_add_one(&acxt, sd);
97 sysfs_link_sibling(sd);
98 }
99
100 if (!sysfs_addrm_finish(&acxt)) {
101 error = -EEXIST;
102 goto out_put;
103 }
104
105 return 0;
106
107 out_put:
108 sysfs_put(target_sd);
109 sysfs_put(sd);
110 return error;
111 }
112
113
114 /**
115 * sysfs_remove_link - remove symlink in object's directory.
116 * @kobj: object we're acting for.
117 * @name: name of the symlink to remove.
118 */
119
120 void sysfs_remove_link(struct kobject * kobj, const char * name)
121 {
122 sysfs_hash_and_remove(kobj->sd, name);
123 }
124
125 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
126 struct sysfs_dirent * target_sd, char *path)
127 {
128 char * s;
129 int depth, size;
130
131 depth = object_depth(parent_sd);
132 size = object_path_length(target_sd) + depth * 3 - 1;
133 if (size > PATH_MAX)
134 return -ENAMETOOLONG;
135
136 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
137
138 for (s = path; depth--; s += 3)
139 strcpy(s,"../");
140
141 fill_object_path(target_sd, path, size);
142 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
143
144 return 0;
145 }
146
147 static int sysfs_getlink(struct dentry *dentry, char * path)
148 {
149 struct sysfs_dirent *sd = dentry->d_fsdata;
150 struct sysfs_dirent *parent_sd = sd->s_parent;
151 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
152 int error;
153
154 mutex_lock(&sysfs_mutex);
155 error = sysfs_get_target_path(parent_sd, target_sd, path);
156 mutex_unlock(&sysfs_mutex);
157
158 return error;
159 }
160
161 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
162 {
163 int error = -ENOMEM;
164 unsigned long page = get_zeroed_page(GFP_KERNEL);
165 if (page)
166 error = sysfs_getlink(dentry, (char *) page);
167 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
168 return NULL;
169 }
170
171 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
172 {
173 char *page = nd_get_link(nd);
174 if (!IS_ERR(page))
175 free_page((unsigned long)page);
176 }
177
178 const struct inode_operations sysfs_symlink_inode_operations = {
179 .readlink = generic_readlink,
180 .follow_link = sysfs_follow_link,
181 .put_link = sysfs_put_link,
182 };
183
184
185 EXPORT_SYMBOL_GPL(sysfs_create_link);
186 EXPORT_SYMBOL_GPL(sysfs_remove_link);
This page took 0.037568 seconds and 5 git commands to generate.