Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * symlink.c - operations for sysfs symlinks. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/fs.h> |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 6 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/kobject.h> |
| 9 | #include <linux/namei.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 10 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 48 | static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | struct sysfs_dirent * parent_sd = parent->d_fsdata; |
| 51 | struct sysfs_symlink * sl; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 52 | struct sysfs_dirent * sd; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 53 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | error = -ENOMEM; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 56 | sl = kzalloc(sizeof(*sl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (!sl) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 58 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL); |
| 61 | if (!sl->link_name) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 62 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | strcpy(sl->link_name, name); |
| 65 | sl->target_kobj = kobject_get(target); |
| 66 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 67 | sd = sysfs_new_dirent(sl, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
| 68 | if (!sd) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 69 | goto err_out; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 70 | sysfs_attach_dirent(sd, parent_sd, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 72 | return 0; |
| 73 | |
| 74 | err_out: |
| 75 | if (sl) { |
| 76 | kobject_put(sl->target_kobj); |
| 77 | kfree(sl->link_name); |
| 78 | kfree(sl); |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return error; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * sysfs_create_link - create symlink between two objects. |
| 85 | * @kobj: object whose directory we're creating the link in. |
| 86 | * @target: object we're pointing to. |
| 87 | * @name: name of the symlink. |
| 88 | */ |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 89 | int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 91 | struct dentry *dentry = NULL; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 92 | int error = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 94 | BUG_ON(!name); |
| 95 | |
| 96 | if (!kobj) { |
| 97 | if (sysfs_mount && sysfs_mount->mnt_sb) |
| 98 | dentry = sysfs_mount->mnt_sb->s_root; |
| 99 | } else |
| 100 | dentry = kobj->dentry; |
| 101 | |
| 102 | if (!dentry) |
| 103 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 105 | mutex_lock(&dentry->d_inode->i_mutex); |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 106 | if (!sysfs_dirent_exist(dentry->d_fsdata, name)) |
| 107 | error = sysfs_add_link(dentry, name, target); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 108 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return error; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * sysfs_remove_link - remove symlink in object's directory. |
| 115 | * @kobj: object we're acting for. |
| 116 | * @name: name of the symlink to remove. |
| 117 | */ |
| 118 | |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 119 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | sysfs_hash_and_remove(kobj->dentry,name); |
| 122 | } |
| 123 | |
| 124 | static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target, |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 125 | char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | char * s; |
| 128 | int depth, size; |
| 129 | |
| 130 | depth = object_depth(kobj); |
| 131 | size = object_path_length(target) + depth * 3 - 1; |
| 132 | if (size > PATH_MAX) |
| 133 | return -ENAMETOOLONG; |
| 134 | |
| 135 | pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); |
| 136 | |
| 137 | for (s = path; depth--; s += 3) |
| 138 | strcpy(s,"../"); |
| 139 | |
| 140 | fill_object_path(target, path, size); |
| 141 | pr_debug("%s: path = '%s'\n", __FUNCTION__, path); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int sysfs_getlink(struct dentry *dentry, char * path) |
| 147 | { |
| 148 | struct kobject *kobj, *target_kobj; |
| 149 | int error = 0; |
| 150 | |
| 151 | kobj = sysfs_get_kobject(dentry->d_parent); |
| 152 | if (!kobj) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | target_kobj = sysfs_get_kobject(dentry); |
| 156 | if (!target_kobj) { |
| 157 | kobject_put(kobj); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | down_read(&sysfs_rename_sem); |
| 162 | error = sysfs_get_target_path(kobj, target_kobj, path); |
| 163 | up_read(&sysfs_rename_sem); |
| 164 | |
| 165 | kobject_put(kobj); |
| 166 | kobject_put(target_kobj); |
| 167 | return error; |
| 168 | |
| 169 | } |
| 170 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 171 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | int error = -ENOMEM; |
| 174 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 175 | if (page) |
| 176 | error = sysfs_getlink(dentry, (char *) page); |
| 177 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 178 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 181 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | char *page = nd_get_link(nd); |
| 184 | if (!IS_ERR(page)) |
| 185 | free_page((unsigned long)page); |
| 186 | } |
| 187 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 188 | const struct inode_operations sysfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .readlink = generic_readlink, |
| 190 | .follow_link = sysfs_follow_link, |
| 191 | .put_link = sysfs_put_link, |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | EXPORT_SYMBOL_GPL(sysfs_create_link); |
| 196 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |