Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * symlink.c - operations for sysfs symlinks. | |
3 | */ | |
4 | ||
5 | #include <linux/fs.h> | |
ceeee1fb | 6 | #include <linux/mount.h> |
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <linux/kobject.h> | |
9 | #include <linux/namei.h> | |
94bebf4d | 10 | #include <asm/semaphore.h> |
1da177e4 LT |
11 | |
12 | #include "sysfs.h" | |
13 | ||
14 | static int object_depth(struct kobject * kobj) | |
15 | { | |
16 | struct kobject * p = kobj; | |
17 | int depth = 0; | |
18 | do { depth++; } while ((p = p->parent)); | |
19 | return depth; | |
20 | } | |
21 | ||
22 | static int object_path_length(struct kobject * kobj) | |
23 | { | |
24 | struct kobject * p = kobj; | |
25 | int length = 1; | |
26 | do { | |
27 | length += strlen(kobject_name(p)) + 1; | |
28 | p = p->parent; | |
29 | } while (p); | |
30 | return length; | |
31 | } | |
32 | ||
33 | static void fill_object_path(struct kobject * kobj, char * buffer, int length) | |
34 | { | |
35 | struct kobject * p; | |
36 | ||
37 | --length; | |
38 | for (p = kobj; p; p = p->parent) { | |
39 | int cur = strlen(kobject_name(p)); | |
40 | ||
41 | /* back up enough to print this bus id with '/' */ | |
42 | length -= cur; | |
43 | strncpy(buffer + length,kobject_name(p),cur); | |
44 | *(buffer + --length) = '/'; | |
45 | } | |
46 | } | |
47 | ||
e3a15db2 | 48 | static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target) |
1da177e4 LT |
49 | { |
50 | struct sysfs_dirent * parent_sd = parent->d_fsdata; | |
51 | struct sysfs_symlink * sl; | |
a26cd722 | 52 | struct sysfs_dirent * sd; |
dfeb9fb0 | 53 | int error; |
1da177e4 LT |
54 | |
55 | error = -ENOMEM; | |
dfeb9fb0 | 56 | sl = kzalloc(sizeof(*sl), GFP_KERNEL); |
1da177e4 | 57 | if (!sl) |
dfeb9fb0 | 58 | goto err_out; |
1da177e4 | 59 | |
1da177e4 LT |
60 | sl->target_kobj = kobject_get(target); |
61 | ||
0c096b50 | 62 | sd = sysfs_new_dirent(name, sl, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
a26cd722 | 63 | if (!sd) |
dfeb9fb0 | 64 | goto err_out; |
a26cd722 | 65 | sysfs_attach_dirent(sd, parent_sd, NULL); |
dfeb9fb0 TH |
66 | |
67 | return 0; | |
68 | ||
69 | err_out: | |
70 | if (sl) { | |
71 | kobject_put(sl->target_kobj); | |
dfeb9fb0 TH |
72 | kfree(sl); |
73 | } | |
1da177e4 LT |
74 | return error; |
75 | } | |
76 | ||
77 | /** | |
78 | * sysfs_create_link - create symlink between two objects. | |
79 | * @kobj: object whose directory we're creating the link in. | |
80 | * @target: object we're pointing to. | |
81 | * @name: name of the symlink. | |
82 | */ | |
e3a15db2 | 83 | int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) |
1da177e4 | 84 | { |
ceeee1fb | 85 | struct dentry *dentry = NULL; |
c516865c | 86 | int error = -EEXIST; |
1da177e4 | 87 | |
ceeee1fb GKH |
88 | BUG_ON(!name); |
89 | ||
90 | if (!kobj) { | |
91 | if (sysfs_mount && sysfs_mount->mnt_sb) | |
92 | dentry = sysfs_mount->mnt_sb->s_root; | |
93 | } else | |
94 | dentry = kobj->dentry; | |
95 | ||
96 | if (!dentry) | |
97 | return -EFAULT; | |
1da177e4 | 98 | |
1b1dcc1b | 99 | mutex_lock(&dentry->d_inode->i_mutex); |
c516865c MS |
100 | if (!sysfs_dirent_exist(dentry->d_fsdata, name)) |
101 | error = sysfs_add_link(dentry, name, target); | |
1b1dcc1b | 102 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
103 | return error; |
104 | } | |
105 | ||
106 | ||
107 | /** | |
108 | * sysfs_remove_link - remove symlink in object's directory. | |
109 | * @kobj: object we're acting for. | |
110 | * @name: name of the symlink to remove. | |
111 | */ | |
112 | ||
e3a15db2 | 113 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
1da177e4 LT |
114 | { |
115 | sysfs_hash_and_remove(kobj->dentry,name); | |
116 | } | |
117 | ||
118 | static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target, | |
e3a15db2 | 119 | char *path) |
1da177e4 LT |
120 | { |
121 | char * s; | |
122 | int depth, size; | |
123 | ||
124 | depth = object_depth(kobj); | |
125 | size = object_path_length(target) + depth * 3 - 1; | |
126 | if (size > PATH_MAX) | |
127 | return -ENAMETOOLONG; | |
128 | ||
129 | pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); | |
130 | ||
131 | for (s = path; depth--; s += 3) | |
132 | strcpy(s,"../"); | |
133 | ||
134 | fill_object_path(target, path, size); | |
135 | pr_debug("%s: path = '%s'\n", __FUNCTION__, path); | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int sysfs_getlink(struct dentry *dentry, char * path) | |
141 | { | |
142 | struct kobject *kobj, *target_kobj; | |
143 | int error = 0; | |
144 | ||
145 | kobj = sysfs_get_kobject(dentry->d_parent); | |
146 | if (!kobj) | |
147 | return -EINVAL; | |
148 | ||
149 | target_kobj = sysfs_get_kobject(dentry); | |
150 | if (!target_kobj) { | |
151 | kobject_put(kobj); | |
152 | return -EINVAL; | |
153 | } | |
154 | ||
155 | down_read(&sysfs_rename_sem); | |
156 | error = sysfs_get_target_path(kobj, target_kobj, path); | |
157 | up_read(&sysfs_rename_sem); | |
158 | ||
159 | kobject_put(kobj); | |
160 | kobject_put(target_kobj); | |
161 | return error; | |
162 | ||
163 | } | |
164 | ||
cc314eef | 165 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
166 | { |
167 | int error = -ENOMEM; | |
168 | unsigned long page = get_zeroed_page(GFP_KERNEL); | |
169 | if (page) | |
170 | error = sysfs_getlink(dentry, (char *) page); | |
171 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); | |
cc314eef | 172 | return NULL; |
1da177e4 LT |
173 | } |
174 | ||
cc314eef | 175 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
176 | { |
177 | char *page = nd_get_link(nd); | |
178 | if (!IS_ERR(page)) | |
179 | free_page((unsigned long)page); | |
180 | } | |
181 | ||
c5ef1c42 | 182 | const struct inode_operations sysfs_symlink_inode_operations = { |
1da177e4 LT |
183 | .readlink = generic_readlink, |
184 | .follow_link = sysfs_follow_link, | |
185 | .put_link = sysfs_put_link, | |
186 | }; | |
187 | ||
188 | ||
189 | EXPORT_SYMBOL_GPL(sysfs_create_link); | |
190 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |