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