Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * fs/kernfs/symlink.c - kernfs symlink implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/gfp.h> |
| 13 | #include <linux/namei.h> |
| 14 | |
| 15 | #include "kernfs-internal.h" |
| 16 | |
| 17 | /** |
| 18 | * kernfs_create_link - create a symlink |
| 19 | * @parent: directory to create the symlink in |
| 20 | * @name: name of the symlink |
| 21 | * @target: target node for the symlink to point to |
| 22 | * |
| 23 | * Returns the created node on success, ERR_PTR() value on error. |
| 24 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 25 | struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, |
| 26 | const char *name, |
| 27 | struct kernfs_node *target) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 28 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 29 | struct kernfs_node *kn; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 30 | struct kernfs_addrm_cxt acxt; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 31 | int error; |
| 32 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 33 | kn = sysfs_new_dirent(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO, |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame^] | 34 | KERNFS_LINK); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 35 | if (!kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 36 | return ERR_PTR(-ENOMEM); |
| 37 | |
Tejun Heo | ac9bba0 | 2013-11-29 17:19:09 -0500 | [diff] [blame] | 38 | if (kernfs_ns_enabled(parent)) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 39 | kn->ns = target->ns; |
| 40 | kn->symlink.target_kn = target; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 41 | kernfs_get(target); /* ref owned by symlink */ |
| 42 | |
| 43 | sysfs_addrm_start(&acxt); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 44 | error = sysfs_add_one(&acxt, kn, parent); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 45 | sysfs_addrm_finish(&acxt); |
| 46 | |
| 47 | if (!error) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 48 | return kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 49 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 50 | kernfs_put(kn); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 51 | return ERR_PTR(error); |
| 52 | } |
| 53 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 54 | static int sysfs_get_target_path(struct kernfs_node *parent, |
| 55 | struct kernfs_node *target, char *path) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 56 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 57 | struct kernfs_node *base, *kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 58 | char *s = path; |
| 59 | int len = 0; |
| 60 | |
| 61 | /* go up to the root, stop at the base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 62 | base = parent; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 63 | while (base->parent) { |
| 64 | kn = target->parent; |
| 65 | while (kn->parent && base != kn) |
| 66 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 67 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 68 | if (base == kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 69 | break; |
| 70 | |
| 71 | strcpy(s, "../"); |
| 72 | s += 3; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 73 | base = base->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* determine end of target string for reverse fillup */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 77 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 78 | while (kn->parent && kn != base) { |
| 79 | len += strlen(kn->name) + 1; |
| 80 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* check limits */ |
| 84 | if (len < 2) |
| 85 | return -EINVAL; |
| 86 | len--; |
| 87 | if ((s - path) + len > PATH_MAX) |
| 88 | return -ENAMETOOLONG; |
| 89 | |
| 90 | /* reverse fillup of target string from target to base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 91 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 92 | while (kn->parent && kn != base) { |
| 93 | int slen = strlen(kn->name); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 94 | |
| 95 | len -= slen; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 96 | strncpy(s + len, kn->name, slen); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 97 | if (len) |
| 98 | s[--len] = '/'; |
| 99 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 100 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int sysfs_getlink(struct dentry *dentry, char *path) |
| 107 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 108 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 109 | struct kernfs_node *parent = kn->parent; |
| 110 | struct kernfs_node *target = kn->symlink.target_kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 111 | int error; |
| 112 | |
| 113 | mutex_lock(&sysfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 114 | error = sysfs_get_target_path(parent, target, path); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 115 | mutex_unlock(&sysfs_mutex); |
| 116 | |
| 117 | return error; |
| 118 | } |
| 119 | |
| 120 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 121 | { |
| 122 | int error = -ENOMEM; |
| 123 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 124 | if (page) { |
| 125 | error = sysfs_getlink(dentry, (char *) page); |
| 126 | if (error < 0) |
| 127 | free_page((unsigned long)page); |
| 128 | } |
| 129 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, |
| 134 | void *cookie) |
| 135 | { |
| 136 | char *page = nd_get_link(nd); |
| 137 | if (!IS_ERR(page)) |
| 138 | free_page((unsigned long)page); |
| 139 | } |
| 140 | |
| 141 | const struct inode_operations sysfs_symlink_inode_operations = { |
| 142 | .setxattr = sysfs_setxattr, |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 143 | .removexattr = sysfs_removexattr, |
| 144 | .getxattr = sysfs_getxattr, |
| 145 | .listxattr = sysfs_listxattr, |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 146 | .readlink = generic_readlink, |
| 147 | .follow_link = sysfs_follow_link, |
| 148 | .put_link = sysfs_put_link, |
| 149 | .setattr = sysfs_setattr, |
| 150 | .getattr = sysfs_getattr, |
| 151 | .permission = sysfs_permission, |
| 152 | }; |