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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | /** |
| 23 | * sysfs_create_link - create symlink between two objects. |
| 24 | * @kobj: object whose directory we're creating the link in. |
| 25 | * @target: object we're pointing to. |
| 26 | * @name: name of the symlink. |
| 27 | */ |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 28 | 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] | 29 | { |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 30 | struct sysfs_dirent *parent_sd = NULL; |
| 31 | struct sysfs_dirent *target_sd = NULL; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 32 | struct sysfs_dirent *sd = NULL; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 33 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 34 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 36 | BUG_ON(!name); |
| 37 | |
Eric W. Biederman | 7d0c7d6 | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 38 | if (!kobj) |
| 39 | parent_sd = &sysfs_root; |
| 40 | else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 41 | parent_sd = kobj->sd; |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 42 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 43 | error = -EFAULT; |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 44 | if (!parent_sd) |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 45 | goto out_put; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 46 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 47 | /* target->sd can go away beneath us but is protected with |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 48 | * sysfs_assoc_lock. Fetch target_sd from it. |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 49 | */ |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 50 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 51 | if (target->sd) |
| 52 | target_sd = sysfs_get(target->sd); |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 53 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 54 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 55 | error = -ENOENT; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 56 | if (!target_sd) |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 57 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 59 | error = -ENOMEM; |
| 60 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
| 61 | if (!sd) |
| 62 | goto out_put; |
Tejun Heo | a1da4df | 2007-07-18 16:14:45 +0900 | [diff] [blame] | 63 | |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 64 | sd->s_symlink.target_sd = target_sd; |
Tejun Heo | a1da4df | 2007-07-18 16:14:45 +0900 | [diff] [blame] | 65 | target_sd = NULL; /* reference is now owned by the symlink */ |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 66 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 67 | sysfs_addrm_start(&acxt, parent_sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 68 | error = sysfs_add_one(&acxt, sd); |
| 69 | sysfs_addrm_finish(&acxt); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 70 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 71 | if (error) |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 72 | goto out_put; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 73 | |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 74 | return 0; |
| 75 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 76 | out_put: |
| 77 | sysfs_put(target_sd); |
| 78 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return error; |
| 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /** |
| 83 | * sysfs_remove_link - remove symlink in object's directory. |
| 84 | * @kobj: object we're acting for. |
| 85 | * @name: name of the symlink to remove. |
| 86 | */ |
| 87 | |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 88 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 90 | sysfs_hash_and_remove(kobj->sd, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 93 | static int sysfs_get_target_path(struct sysfs_dirent *parent_sd, |
| 94 | struct sysfs_dirent *target_sd, char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 96 | struct sysfs_dirent *base, *sd; |
| 97 | char *s = path; |
| 98 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 100 | /* go up to the root, stop at the base */ |
| 101 | base = parent_sd; |
| 102 | while (base->s_parent) { |
| 103 | sd = target_sd->s_parent; |
| 104 | while (sd->s_parent && base != sd) |
| 105 | sd = sd->s_parent; |
| 106 | |
| 107 | if (base == sd) |
| 108 | break; |
| 109 | |
| 110 | strcpy(s, "../"); |
| 111 | s += 3; |
| 112 | base = base->s_parent; |
| 113 | } |
| 114 | |
| 115 | /* determine end of target string for reverse fillup */ |
| 116 | sd = target_sd; |
| 117 | while (sd->s_parent && sd != base) { |
| 118 | len += strlen(sd->s_name) + 1; |
| 119 | sd = sd->s_parent; |
| 120 | } |
| 121 | |
| 122 | /* check limits */ |
| 123 | if (len < 2) |
| 124 | return -EINVAL; |
| 125 | len--; |
| 126 | if ((s - path) + len > PATH_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return -ENAMETOOLONG; |
| 128 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 129 | /* reverse fillup of target string from target to base */ |
| 130 | sd = target_sd; |
| 131 | while (sd->s_parent && sd != base) { |
| 132 | int slen = strlen(sd->s_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 134 | len -= slen; |
| 135 | strncpy(s + len, sd->s_name, slen); |
| 136 | if (len) |
| 137 | s[--len] = '/'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Kay Sievers | 2f90a85 | 2007-11-01 20:20:52 +0100 | [diff] [blame^] | 139 | sd = sd->s_parent; |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int sysfs_getlink(struct dentry *dentry, char * path) |
| 146 | { |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 147 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 148 | struct sysfs_dirent *parent_sd = sd->s_parent; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 149 | struct sysfs_dirent *target_sd = sd->s_symlink.target_sd; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 150 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 152 | mutex_lock(&sysfs_mutex); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 153 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 154 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 156 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 159 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | int error = -ENOMEM; |
| 162 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 163 | if (page) |
| 164 | error = sysfs_getlink(dentry, (char *) page); |
| 165 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 166 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 169 | 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] | 170 | { |
| 171 | char *page = nd_get_link(nd); |
| 172 | if (!IS_ERR(page)) |
| 173 | free_page((unsigned long)page); |
| 174 | } |
| 175 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 176 | const struct inode_operations sysfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | .readlink = generic_readlink, |
| 178 | .follow_link = sysfs_follow_link, |
| 179 | .put_link = sysfs_put_link, |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | EXPORT_SYMBOL_GPL(sysfs_create_link); |
| 184 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |