2 * symlink.c - operations for sysfs symlinks.
6 #include <linux/module.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
12 static struct inode_operations sysfs_symlink_inode_operations
= {
13 .readlink
= generic_readlink
,
14 .follow_link
= sysfs_follow_link
,
15 .put_link
= sysfs_put_link
,
18 static int init_symlink(struct inode
* inode
)
20 inode
->i_op
= &sysfs_symlink_inode_operations
;
24 static int object_depth(struct kobject
* kobj
)
26 struct kobject
* p
= kobj
;
28 do { depth
++; } while ((p
= p
->parent
));
32 static int object_path_length(struct kobject
* kobj
)
34 struct kobject
* p
= kobj
;
37 length
+= strlen(kobject_name(p
)) + 1;
43 static void fill_object_path(struct kobject
* kobj
, char * buffer
, int length
)
48 for (p
= kobj
; p
; p
= p
->parent
) {
49 int cur
= strlen(kobject_name(p
));
51 /* back up enough to print this bus id with '/' */
53 strncpy(buffer
+ length
,kobject_name(p
),cur
);
54 *(buffer
+ --length
) = '/';
59 * sysfs_create_link - create symlink between two objects.
60 * @kobj: object whose directory we're creating the link in.
61 * @target: object we're pointing to.
62 * @name: name of the symlink.
64 int sysfs_create_link(struct kobject
* kobj
, struct kobject
* target
, char * name
)
66 struct dentry
* dentry
= kobj
->dentry
;
70 down(&dentry
->d_inode
->i_sem
);
71 d
= sysfs_get_dentry(dentry
,name
);
73 error
= sysfs_create(d
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
76 * associate the link dentry with the target kobject
78 d
->d_fsdata
= kobject_get(target
);
82 up(&dentry
->d_inode
->i_sem
);
88 * sysfs_remove_link - remove symlink in object's directory.
89 * @kobj: object we're acting for.
90 * @name: name of the symlink to remove.
93 void sysfs_remove_link(struct kobject
* kobj
, char * name
)
95 sysfs_hash_and_remove(kobj
->dentry
,name
);
98 static int sysfs_get_target_path(struct kobject
* kobj
, struct kobject
* target
,
104 depth
= object_depth(kobj
);
105 size
= object_path_length(target
) + depth
* 3 - 1;
107 return -ENAMETOOLONG
;
109 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__
, depth
, size
);
111 for (s
= path
; depth
--; s
+= 3)
114 fill_object_path(target
, path
, size
);
115 pr_debug("%s: path = '%s'\n", __FUNCTION__
, path
);
120 static int sysfs_getlink(struct dentry
*dentry
, char * path
)
122 struct kobject
*kobj
, *target_kobj
;
125 kobj
= sysfs_get_kobject(dentry
->d_parent
);
129 target_kobj
= sysfs_get_kobject(dentry
);
135 down_read(&sysfs_rename_sem
);
136 error
= sysfs_get_target_path(kobj
, target_kobj
, path
);
137 up_read(&sysfs_rename_sem
);
140 kobject_put(target_kobj
);
145 int sysfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
148 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
150 error
= sysfs_getlink(dentry
, (char *) page
);
151 nd_set_link(nd
, error
? ERR_PTR(error
) : (char *)page
);
155 void sysfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
157 char *page
= nd_get_link(nd
);
159 free_page((unsigned long)page
);
162 EXPORT_SYMBOL(sysfs_create_link
);
163 EXPORT_SYMBOL(sysfs_remove_link
);