Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 2 | * fs/sysfs/symlink.c - sysfs symlink implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | * |
| 10 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/fs.h> |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 14 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kobject.h> |
| 17 | #include <linux/namei.h> |
Dave Young | 869512a | 2007-07-26 14:53:53 +0000 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "sysfs.h" |
| 21 | |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame^] | 22 | static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target, |
| 23 | const char *name, int warn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 25 | struct sysfs_dirent *parent_sd = NULL; |
| 26 | struct sysfs_dirent *target_sd = NULL; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 27 | struct sysfs_dirent *sd = NULL; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 28 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 29 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 31 | BUG_ON(!name); |
| 32 | |
Eric W. Biederman | 7d0c7d6 | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 33 | if (!kobj) |
| 34 | parent_sd = &sysfs_root; |
| 35 | else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 36 | parent_sd = kobj->sd; |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 37 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 38 | error = -EFAULT; |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 39 | if (!parent_sd) |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 40 | goto out_put; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 41 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 42 | /* target->sd can go away beneath us but is protected with |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 43 | * sysfs_assoc_lock. Fetch target_sd from it. |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 44 | */ |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 45 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 46 | if (target->sd) |
| 47 | target_sd = sysfs_get(target->sd); |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 48 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 49 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 50 | error = -ENOENT; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 51 | if (!target_sd) |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 52 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 54 | error = -ENOMEM; |
| 55 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
| 56 | if (!sd) |
| 57 | goto out_put; |
Tejun Heo | a1da4df | 2007-07-18 16:14:45 +0900 | [diff] [blame] | 58 | |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 59 | sd->s_symlink.target_sd = target_sd; |
Tejun Heo | a1da4df | 2007-07-18 16:14:45 +0900 | [diff] [blame] | 60 | target_sd = NULL; /* reference is now owned by the symlink */ |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 61 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 62 | sysfs_addrm_start(&acxt, parent_sd); |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame^] | 63 | if (warn) |
| 64 | error = sysfs_add_one(&acxt, sd); |
| 65 | else |
| 66 | error = __sysfs_add_one(&acxt, sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 67 | sysfs_addrm_finish(&acxt); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 68 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 69 | if (error) |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 70 | goto out_put; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 71 | |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 72 | return 0; |
| 73 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 74 | out_put: |
| 75 | sysfs_put(target_sd); |
| 76 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return error; |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /** |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame^] | 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. |
| 85 | */ |
| 86 | int sysfs_create_link(struct kobject *kobj, struct kobject *target, |
| 87 | const char *name) |
| 88 | { |
| 89 | return sysfs_do_create_link(kobj, target, name, 1); |
| 90 | } |
| 91 | |
| 92 | /** |
| 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. |
| 97 | * |
| 98 | * This function does the same as sysf_create_link(), but it |
| 99 | * doesn't warn if the link already exists. |
| 100 | */ |
| 101 | int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target, |
| 102 | const char *name) |
| 103 | { |
| 104 | return sysfs_do_create_link(kobj, target, name, 0); |
| 105 | } |
| 106 | |
| 107 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 111 | */ |
| 112 | |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 113 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Mark Fasheh | a839c5a | 2008-01-29 14:35:18 -0800 | [diff] [blame] | 115 | struct sysfs_dirent *parent_sd = NULL; |
| 116 | |
| 117 | if (!kobj) |
| 118 | parent_sd = &sysfs_root; |
| 119 | else |
| 120 | parent_sd = kobj->sd; |
| 121 | |
| 122 | sysfs_hash_and_remove(parent_sd, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 125 | static int sysfs_get_target_path(struct sysfs_dirent *parent_sd, |
| 126 | struct sysfs_dirent *target_sd, char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 128 | struct sysfs_dirent *base, *sd; |
| 129 | char *s = path; |
| 130 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 132 | /* go up to the root, stop at the base */ |
| 133 | base = parent_sd; |
| 134 | while (base->s_parent) { |
| 135 | sd = target_sd->s_parent; |
| 136 | while (sd->s_parent && base != sd) |
| 137 | sd = sd->s_parent; |
| 138 | |
| 139 | if (base == sd) |
| 140 | break; |
| 141 | |
| 142 | strcpy(s, "../"); |
| 143 | s += 3; |
| 144 | base = base->s_parent; |
| 145 | } |
| 146 | |
| 147 | /* determine end of target string for reverse fillup */ |
| 148 | sd = target_sd; |
| 149 | while (sd->s_parent && sd != base) { |
| 150 | len += strlen(sd->s_name) + 1; |
| 151 | sd = sd->s_parent; |
| 152 | } |
| 153 | |
| 154 | /* check limits */ |
| 155 | if (len < 2) |
| 156 | return -EINVAL; |
| 157 | len--; |
| 158 | if ((s - path) + len > PATH_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return -ENAMETOOLONG; |
| 160 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 161 | /* reverse fillup of target string from target to base */ |
| 162 | sd = target_sd; |
| 163 | while (sd->s_parent && sd != base) { |
| 164 | int slen = strlen(sd->s_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 166 | len -= slen; |
| 167 | strncpy(s + len, sd->s_name, slen); |
| 168 | if (len) |
| 169 | s[--len] = '/'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame] | 171 | sd = sd->s_parent; |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int sysfs_getlink(struct dentry *dentry, char * path) |
| 178 | { |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 179 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 180 | struct sysfs_dirent *parent_sd = sd->s_parent; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 181 | struct sysfs_dirent *target_sd = sd->s_symlink.target_sd; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 182 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 184 | mutex_lock(&sysfs_mutex); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 185 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 186 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 188 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 191 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int error = -ENOMEM; |
| 194 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 195 | if (page) |
| 196 | error = sysfs_getlink(dentry, (char *) page); |
| 197 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 198 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 201 | 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] | 202 | { |
| 203 | char *page = nd_get_link(nd); |
| 204 | if (!IS_ERR(page)) |
| 205 | free_page((unsigned long)page); |
| 206 | } |
| 207 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 208 | const struct inode_operations sysfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | .readlink = generic_readlink, |
| 210 | .follow_link = sysfs_follow_link, |
| 211 | .put_link = sysfs_put_link, |
| 212 | }; |
| 213 | |
| 214 | |
| 215 | EXPORT_SYMBOL_GPL(sysfs_create_link); |
| 216 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |