2 * fs/sysfs/symlink.c - sysfs symlink implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
14 #include <linux/mount.h>
15 #include <linux/module.h>
16 #include <linux/kobject.h>
17 #include <linux/namei.h>
18 #include <linux/mutex.h>
23 * sysfs_create_link - create symlink between two objects.
24 * @kobj: object whose directory we're creating the link in.
25 * @target: object we're pointing to.
26 * @name: name of the symlink.
28 int sysfs_create_link(struct kobject
* kobj
, struct kobject
* target
, const char * name
)
30 struct sysfs_dirent
*parent_sd
= NULL
;
31 struct sysfs_dirent
*target_sd
= NULL
;
32 struct sysfs_dirent
*sd
= NULL
;
33 struct sysfs_addrm_cxt acxt
;
39 parent_sd
= &sysfs_root
;
47 /* target->sd can go away beneath us but is protected with
48 * sysfs_assoc_lock. Fetch target_sd from it.
50 spin_lock(&sysfs_assoc_lock
);
52 target_sd
= sysfs_get(target
->sd
);
53 spin_unlock(&sysfs_assoc_lock
);
60 sd
= sysfs_new_dirent(name
, S_IFLNK
|S_IRWXUGO
, SYSFS_KOBJ_LINK
);
64 sd
->s_symlink
.target_sd
= target_sd
;
65 target_sd
= NULL
; /* reference is now owned by the symlink */
67 sysfs_addrm_start(&acxt
, parent_sd
);
68 error
= sysfs_add_one(&acxt
, sd
);
69 sysfs_addrm_finish(&acxt
);
83 * sysfs_remove_link - remove symlink in object's directory.
84 * @kobj: object we're acting for.
85 * @name: name of the symlink to remove.
88 void sysfs_remove_link(struct kobject
* kobj
, const char * name
)
90 struct sysfs_dirent
*parent_sd
= NULL
;
93 parent_sd
= &sysfs_root
;
97 sysfs_hash_and_remove(parent_sd
, name
);
100 static int sysfs_get_target_path(struct sysfs_dirent
*parent_sd
,
101 struct sysfs_dirent
*target_sd
, char *path
)
103 struct sysfs_dirent
*base
, *sd
;
107 /* go up to the root, stop at the base */
109 while (base
->s_parent
) {
110 sd
= target_sd
->s_parent
;
111 while (sd
->s_parent
&& base
!= sd
)
119 base
= base
->s_parent
;
122 /* determine end of target string for reverse fillup */
124 while (sd
->s_parent
&& sd
!= base
) {
125 len
+= strlen(sd
->s_name
) + 1;
133 if ((s
- path
) + len
> PATH_MAX
)
134 return -ENAMETOOLONG
;
136 /* reverse fillup of target string from target to base */
138 while (sd
->s_parent
&& sd
!= base
) {
139 int slen
= strlen(sd
->s_name
);
142 strncpy(s
+ len
, sd
->s_name
, slen
);
152 static int sysfs_getlink(struct dentry
*dentry
, char * path
)
154 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
155 struct sysfs_dirent
*parent_sd
= sd
->s_parent
;
156 struct sysfs_dirent
*target_sd
= sd
->s_symlink
.target_sd
;
159 mutex_lock(&sysfs_mutex
);
160 error
= sysfs_get_target_path(parent_sd
, target_sd
, path
);
161 mutex_unlock(&sysfs_mutex
);
166 static void *sysfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
169 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
171 error
= sysfs_getlink(dentry
, (char *) page
);
172 nd_set_link(nd
, error
? ERR_PTR(error
) : (char *)page
);
176 static void sysfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
178 char *page
= nd_get_link(nd
);
180 free_page((unsigned long)page
);
183 const struct inode_operations sysfs_symlink_inode_operations
= {
184 .readlink
= generic_readlink
,
185 .follow_link
= sysfs_follow_link
,
186 .put_link
= sysfs_put_link
,
190 EXPORT_SYMBOL_GPL(sysfs_create_link
);
191 EXPORT_SYMBOL_GPL(sysfs_remove_link
);