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 | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 30 | int error; |
| 31 | |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 32 | kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 33 | if (!kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 34 | return ERR_PTR(-ENOMEM); |
| 35 | |
Tejun Heo | ac9bba0 | 2013-11-29 17:19:09 -0500 | [diff] [blame] | 36 | if (kernfs_ns_enabled(parent)) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 37 | kn->ns = target->ns; |
| 38 | kn->symlink.target_kn = target; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 39 | kernfs_get(target); /* ref owned by symlink */ |
| 40 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 41 | error = kernfs_add_one(kn); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 42 | if (!error) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 43 | return kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 44 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 45 | kernfs_put(kn); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 46 | return ERR_PTR(error); |
| 47 | } |
| 48 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 49 | static int kernfs_get_target_path(struct kernfs_node *parent, |
| 50 | struct kernfs_node *target, char *path) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 51 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 52 | struct kernfs_node *base, *kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 53 | char *s = path; |
| 54 | int len = 0; |
| 55 | |
| 56 | /* go up to the root, stop at the base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 57 | base = parent; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 58 | while (base->parent) { |
| 59 | kn = target->parent; |
| 60 | while (kn->parent && base != kn) |
| 61 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 62 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 63 | if (base == kn) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 64 | break; |
| 65 | |
| 66 | strcpy(s, "../"); |
| 67 | s += 3; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 68 | base = base->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* determine end of target string for reverse fillup */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 72 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 73 | while (kn->parent && kn != base) { |
| 74 | len += strlen(kn->name) + 1; |
| 75 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* check limits */ |
| 79 | if (len < 2) |
| 80 | return -EINVAL; |
| 81 | len--; |
| 82 | if ((s - path) + len > PATH_MAX) |
| 83 | return -ENAMETOOLONG; |
| 84 | |
| 85 | /* reverse fillup of target string from target to base */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 86 | kn = target; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 87 | while (kn->parent && kn != base) { |
| 88 | int slen = strlen(kn->name); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 89 | |
| 90 | len -= slen; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 91 | strncpy(s + len, kn->name, slen); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 92 | if (len) |
| 93 | s[--len] = '/'; |
| 94 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 95 | kn = kn->parent; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 101 | static int kernfs_getlink(struct dentry *dentry, char *path) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 102 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 103 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 104 | struct kernfs_node *parent = kn->parent; |
| 105 | struct kernfs_node *target = kn->symlink.target_kn; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 106 | int error; |
| 107 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 108 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 109 | error = kernfs_get_target_path(parent, target, path); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 110 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 111 | |
| 112 | return error; |
| 113 | } |
| 114 | |
Al Viro | 6e77137 | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 115 | static const char *kernfs_iop_follow_link(struct dentry *dentry, void **cookie) |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 116 | { |
| 117 | int error = -ENOMEM; |
| 118 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 119 | if (!page) |
| 120 | return ERR_PTR(-ENOMEM); |
| 121 | error = kernfs_getlink(dentry, (char *)page); |
| 122 | if (unlikely(error < 0)) { |
| 123 | free_page((unsigned long)page); |
| 124 | return ERR_PTR(error); |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 125 | } |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 126 | return *cookie = (char *)page; |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 129 | const struct inode_operations kernfs_symlink_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 130 | .setxattr = kernfs_iop_setxattr, |
| 131 | .removexattr = kernfs_iop_removexattr, |
| 132 | .getxattr = kernfs_iop_getxattr, |
| 133 | .listxattr = kernfs_iop_listxattr, |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 134 | .readlink = generic_readlink, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 135 | .follow_link = kernfs_iop_follow_link, |
Al Viro | ecc087f | 2015-05-07 11:19:14 -0400 | [diff] [blame] | 136 | .put_link = free_page_put_link, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 137 | .setattr = kernfs_iop_setattr, |
| 138 | .getattr = kernfs_iop_getattr, |
| 139 | .permission = kernfs_iop_permission, |
Tejun Heo | 2072f1a | 2013-11-28 14:54:35 -0500 | [diff] [blame] | 140 | }; |