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 | |
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 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 47 | static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name, |
| 48 | struct sysfs_dirent * target_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 50 | struct sysfs_dirent * sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 52 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 53 | if (!sd) |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 54 | return -ENOMEM; |
| 55 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 56 | sd->s_elem.symlink.target_sd = target_sd; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 57 | sysfs_attach_dirent(sd, parent_sd, NULL); |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 58 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * sysfs_create_link - create symlink between two objects. |
| 63 | * @kobj: object whose directory we're creating the link in. |
| 64 | * @target: object we're pointing to. |
| 65 | * @name: name of the symlink. |
| 66 | */ |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 67 | 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] | 68 | { |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 69 | struct dentry *dentry = NULL; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 70 | struct sysfs_dirent *parent_sd = NULL; |
| 71 | struct sysfs_dirent *target_sd = NULL; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 72 | int error = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Greg Kroah-Hartman | ceeee1f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 74 | BUG_ON(!name); |
| 75 | |
| 76 | if (!kobj) { |
| 77 | if (sysfs_mount && sysfs_mount->mnt_sb) |
| 78 | dentry = sysfs_mount->mnt_sb->s_root; |
| 79 | } else |
| 80 | dentry = kobj->dentry; |
| 81 | |
| 82 | if (!dentry) |
| 83 | return -EFAULT; |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 84 | parent_sd = dentry->d_fsdata; |
| 85 | |
| 86 | /* target->dentry can go away beneath us but is protected with |
| 87 | * kobj_sysfs_assoc_lock. Fetch target_sd from it. |
| 88 | */ |
| 89 | spin_lock(&kobj_sysfs_assoc_lock); |
| 90 | if (target->dentry) |
| 91 | target_sd = sysfs_get(target->dentry->d_fsdata); |
| 92 | spin_unlock(&kobj_sysfs_assoc_lock); |
| 93 | |
| 94 | if (!target_sd) |
| 95 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 97 | mutex_lock(&dentry->d_inode->i_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame^] | 98 | if (!sysfs_find_dirent(dentry->d_fsdata, name)) |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 99 | error = sysfs_add_link(parent_sd, name, target_sd); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 100 | mutex_unlock(&dentry->d_inode->i_mutex); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 101 | |
| 102 | if (error) |
| 103 | sysfs_put(target_sd); |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return error; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * sysfs_remove_link - remove symlink in object's directory. |
| 111 | * @kobj: object we're acting for. |
| 112 | * @name: name of the symlink to remove. |
| 113 | */ |
| 114 | |
Dmitry Torokhov | e3a15db | 2005-04-26 02:31:08 -0500 | [diff] [blame] | 115 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | sysfs_hash_and_remove(kobj->dentry,name); |
| 118 | } |
| 119 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 120 | static int sysfs_get_target_path(struct sysfs_dirent * parent_sd, |
| 121 | struct sysfs_dirent * target_sd, char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | char * s; |
| 124 | int depth, size; |
| 125 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 126 | depth = object_depth(parent_sd); |
| 127 | size = object_path_length(target_sd) + depth * 3 - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (size > PATH_MAX) |
| 129 | return -ENAMETOOLONG; |
| 130 | |
| 131 | pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); |
| 132 | |
| 133 | for (s = path; depth--; s += 3) |
| 134 | strcpy(s,"../"); |
| 135 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 136 | fill_object_path(target_sd, path, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | pr_debug("%s: path = '%s'\n", __FUNCTION__, path); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int sysfs_getlink(struct dentry *dentry, char * path) |
| 143 | { |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 144 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 145 | struct sysfs_dirent *parent_sd = sd->s_parent; |
| 146 | struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd; |
| 147 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | down_read(&sysfs_rename_sem); |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 150 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | up_read(&sysfs_rename_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 153 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 156 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | int error = -ENOMEM; |
| 159 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 160 | if (page) |
| 161 | error = sysfs_getlink(dentry, (char *) page); |
| 162 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 163 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | char *page = nd_get_link(nd); |
| 169 | if (!IS_ERR(page)) |
| 170 | free_page((unsigned long)page); |
| 171 | } |
| 172 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 173 | const struct inode_operations sysfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .readlink = generic_readlink, |
| 175 | .follow_link = sysfs_follow_link, |
| 176 | .put_link = sysfs_put_link, |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | EXPORT_SYMBOL_GPL(sysfs_create_link); |
| 181 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |