2 * symlink.c - operations for sysfs symlinks.
6 #include <linux/module.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
12 static int object_depth(struct kobject
* kobj
)
14 struct kobject
* p
= kobj
;
16 do { depth
++; } while ((p
= p
->parent
));
20 static int object_path_length(struct kobject
* kobj
)
22 struct kobject
* p
= kobj
;
25 length
+= strlen(kobject_name(p
)) + 1;
31 static void fill_object_path(struct kobject
* kobj
, char * buffer
, int length
)
36 for (p
= kobj
; p
; p
= p
->parent
) {
37 int cur
= strlen(kobject_name(p
));
39 /* back up enough to print this bus id with '/' */
41 strncpy(buffer
+ length
,kobject_name(p
),cur
);
42 *(buffer
+ --length
) = '/';
46 static int sysfs_add_link(struct dentry
* parent
, const char * name
, struct kobject
* target
)
48 struct sysfs_dirent
* parent_sd
= parent
->d_fsdata
;
49 struct sysfs_symlink
* sl
;
53 sl
= kmalloc(sizeof(*sl
), GFP_KERNEL
);
57 sl
->link_name
= kmalloc(strlen(name
) + 1, GFP_KERNEL
);
61 strcpy(sl
->link_name
, name
);
62 sl
->target_kobj
= kobject_get(target
);
64 error
= sysfs_make_dirent(parent_sd
, NULL
, sl
, S_IFLNK
|S_IRWXUGO
,
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.
83 int sysfs_create_link(struct kobject
* kobj
, struct kobject
* target
, const char * name
)
85 struct dentry
* dentry
= kobj
->dentry
;
88 BUG_ON(!kobj
|| !kobj
->dentry
|| !name
);
90 mutex_lock(&dentry
->d_inode
->i_mutex
);
91 if (!sysfs_dirent_exist(dentry
->d_fsdata
, name
))
92 error
= sysfs_add_link(dentry
, name
, target
);
93 mutex_unlock(&dentry
->d_inode
->i_mutex
);
99 * sysfs_remove_link - remove symlink in object's directory.
100 * @kobj: object we're acting for.
101 * @name: name of the symlink to remove.
104 void sysfs_remove_link(struct kobject
* kobj
, const char * name
)
106 sysfs_hash_and_remove(kobj
->dentry
,name
);
109 static int sysfs_get_target_path(struct kobject
* kobj
, struct kobject
* target
,
115 depth
= object_depth(kobj
);
116 size
= object_path_length(target
) + depth
* 3 - 1;
118 return -ENAMETOOLONG
;
120 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__
, depth
, size
);
122 for (s
= path
; depth
--; s
+= 3)
125 fill_object_path(target
, path
, size
);
126 pr_debug("%s: path = '%s'\n", __FUNCTION__
, path
);
131 static int sysfs_getlink(struct dentry
*dentry
, char * path
)
133 struct kobject
*kobj
, *target_kobj
;
136 kobj
= sysfs_get_kobject(dentry
->d_parent
);
140 target_kobj
= sysfs_get_kobject(dentry
);
146 down_read(&sysfs_rename_sem
);
147 error
= sysfs_get_target_path(kobj
, target_kobj
, path
);
148 up_read(&sysfs_rename_sem
);
151 kobject_put(target_kobj
);
156 static void *sysfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
159 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
161 error
= sysfs_getlink(dentry
, (char *) page
);
162 nd_set_link(nd
, error
? ERR_PTR(error
) : (char *)page
);
166 static void sysfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
168 char *page
= nd_get_link(nd
);
170 free_page((unsigned long)page
);
173 struct inode_operations sysfs_symlink_inode_operations
= {
174 .readlink
= generic_readlink
,
175 .follow_link
= sysfs_follow_link
,
176 .put_link
= sysfs_put_link
,
180 EXPORT_SYMBOL_GPL(sysfs_create_link
);
181 EXPORT_SYMBOL_GPL(sysfs_remove_link
);