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>
22 static int sysfs_do_create_link(struct kobject
*kobj
, struct kobject
*target
,
23 const char *name
, int warn
)
25 struct sysfs_dirent
*parent_sd
= NULL
;
26 struct sysfs_dirent
*target_sd
= NULL
;
27 struct sysfs_dirent
*sd
= NULL
;
28 struct sysfs_addrm_cxt acxt
;
34 parent_sd
= &sysfs_root
;
42 /* target->sd can go away beneath us but is protected with
43 * sysfs_assoc_lock. Fetch target_sd from it.
45 spin_lock(&sysfs_assoc_lock
);
47 target_sd
= sysfs_get(target
->sd
);
48 spin_unlock(&sysfs_assoc_lock
);
55 sd
= sysfs_new_dirent(name
, S_IFLNK
|S_IRWXUGO
, SYSFS_KOBJ_LINK
);
59 sd
->s_symlink
.target_sd
= target_sd
;
60 target_sd
= NULL
; /* reference is now owned by the symlink */
62 sysfs_addrm_start(&acxt
, parent_sd
);
64 error
= sysfs_add_one(&acxt
, sd
);
66 error
= __sysfs_add_one(&acxt
, sd
);
67 sysfs_addrm_finish(&acxt
);
81 * sysfs_create_link - create symlink between two objects.
82 * @kobj: object whose directory we're creating the link in.
83 * @target: object we're pointing to.
84 * @name: name of the symlink.
86 int sysfs_create_link(struct kobject
*kobj
, struct kobject
*target
,
89 return sysfs_do_create_link(kobj
, target
, name
, 1);
93 * sysfs_create_link_nowarn - create symlink between two objects.
94 * @kobj: object whose directory we're creating the link in.
95 * @target: object we're pointing to.
96 * @name: name of the symlink.
98 * This function does the same as sysf_create_link(), but it
99 * doesn't warn if the link already exists.
101 int sysfs_create_link_nowarn(struct kobject
*kobj
, struct kobject
*target
,
104 return sysfs_do_create_link(kobj
, target
, name
, 0);
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.
113 void sysfs_remove_link(struct kobject
* kobj
, const char * name
)
115 struct sysfs_dirent
*parent_sd
= NULL
;
118 parent_sd
= &sysfs_root
;
120 parent_sd
= kobj
->sd
;
122 sysfs_hash_and_remove(parent_sd
, name
);
125 static int sysfs_get_target_path(struct sysfs_dirent
*parent_sd
,
126 struct sysfs_dirent
*target_sd
, char *path
)
128 struct sysfs_dirent
*base
, *sd
;
132 /* go up to the root, stop at the base */
134 while (base
->s_parent
) {
135 sd
= target_sd
->s_parent
;
136 while (sd
->s_parent
&& base
!= sd
)
144 base
= base
->s_parent
;
147 /* determine end of target string for reverse fillup */
149 while (sd
->s_parent
&& sd
!= base
) {
150 len
+= strlen(sd
->s_name
) + 1;
158 if ((s
- path
) + len
> PATH_MAX
)
159 return -ENAMETOOLONG
;
161 /* reverse fillup of target string from target to base */
163 while (sd
->s_parent
&& sd
!= base
) {
164 int slen
= strlen(sd
->s_name
);
167 strncpy(s
+ len
, sd
->s_name
, slen
);
177 static int sysfs_getlink(struct dentry
*dentry
, char * path
)
179 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
180 struct sysfs_dirent
*parent_sd
= sd
->s_parent
;
181 struct sysfs_dirent
*target_sd
= sd
->s_symlink
.target_sd
;
184 mutex_lock(&sysfs_mutex
);
185 error
= sysfs_get_target_path(parent_sd
, target_sd
, path
);
186 mutex_unlock(&sysfs_mutex
);
191 static void *sysfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
194 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
196 error
= sysfs_getlink(dentry
, (char *) page
);
198 free_page((unsigned long)page
);
200 nd_set_link(nd
, error
? ERR_PTR(error
) : (char *)page
);
204 static void sysfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
206 char *page
= nd_get_link(nd
);
208 free_page((unsigned long)page
);
211 const struct inode_operations sysfs_symlink_inode_operations
= {
212 .readlink
= generic_readlink
,
213 .follow_link
= sysfs_follow_link
,
214 .put_link
= sysfs_put_link
,
218 EXPORT_SYMBOL_GPL(sysfs_create_link
);
219 EXPORT_SYMBOL_GPL(sysfs_remove_link
);