blob: 0623c3edcfb9dce41fcfc28e5ff733ec09b5404a [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Joel Becker7063fbf2005-12-15 14:29:43 -08003 * symlink.c - operations for configfs symlinks.
4 *
Joel Becker7063fbf2005-12-15 14:29:43 -08005 * Based on sysfs:
6 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
7 *
8 * configfs Copyright (C) 2005 Oracle. All rights reserved.
9 */
10
11#include <linux/fs.h>
12#include <linux/module.h>
13#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080015
16#include <linux/configfs.h>
17#include "configfs_internal.h"
18
Louis Rilling9a73d782008-06-20 14:09:22 +020019/* Protects attachments of new symlinks */
20DEFINE_MUTEX(configfs_symlink_mutex);
21
Joel Becker7063fbf2005-12-15 14:29:43 -080022static int item_depth(struct config_item * item)
23{
24 struct config_item * p = item;
25 int depth = 0;
26 do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
27 return depth;
28}
29
30static int item_path_length(struct config_item * item)
31{
32 struct config_item * p = item;
33 int length = 1;
34 do {
35 length += strlen(config_item_name(p)) + 1;
36 p = p->ci_parent;
37 } while (p && !configfs_is_root(p));
38 return length;
39}
40
41static void fill_item_path(struct config_item * item, char * buffer, int length)
42{
43 struct config_item * p;
44
45 --length;
46 for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
47 int cur = strlen(config_item_name(p));
48
49 /* back up enough to print this bus id with '/' */
50 length -= cur;
Guenter Roeck18233422018-07-01 13:56:54 -070051 memcpy(buffer + length, config_item_name(p), cur);
Joel Becker7063fbf2005-12-15 14:29:43 -080052 *(buffer + --length) = '/';
53 }
54}
55
Al Viroe9c03af2019-09-11 09:00:01 +020056static int configfs_get_target_path(struct config_item *item,
57 struct config_item *target, char *path)
58{
59 int depth, size;
60 char *s;
61
62 depth = item_depth(item);
63 size = item_path_length(target) + depth * 3 - 1;
64 if (size > PATH_MAX)
65 return -ENAMETOOLONG;
66
67 pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
68
69 for (s = path; depth--; s += 3)
70 strcpy(s,"../");
71
72 fill_item_path(target, path, size);
73 pr_debug("%s: path = '%s'\n", __func__, path);
74 return 0;
75}
76
Joel Becker7063fbf2005-12-15 14:29:43 -080077static int create_link(struct config_item *parent_item,
Joel Beckere7515d02006-03-10 11:42:30 -080078 struct config_item *item,
Joel Becker7063fbf2005-12-15 14:29:43 -080079 struct dentry *dentry)
80{
81 struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
Al Viroe9c03af2019-09-11 09:00:01 +020082 char *body;
Joel Becker7063fbf2005-12-15 14:29:43 -080083 int ret;
84
Louis Rilling2a109f22008-07-04 16:56:05 +020085 if (!configfs_dirent_is_ready(target_sd))
Al Viroe9c03af2019-09-11 09:00:01 +020086 return -ENOENT;
Joel Becker7063fbf2005-12-15 14:29:43 -080087
Al Viroe9c03af2019-09-11 09:00:01 +020088 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
89 if (!body)
90 return -ENOMEM;
91
92 configfs_get(target_sd);
93 spin_lock(&configfs_dirent_lock);
94 if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
95 spin_unlock(&configfs_dirent_lock);
96 configfs_put(target_sd);
97 kfree(body);
98 return -ENOENT;
99 }
100 target_sd->s_links++;
101 spin_unlock(&configfs_dirent_lock);
Honggang Lie2f238f2019-11-04 20:43:22 +0800102 ret = configfs_get_target_path(parent_item, item, body);
Al Viroe9c03af2019-09-11 09:00:01 +0200103 if (!ret)
104 ret = configfs_create_link(target_sd, parent_item->ci_dentry,
105 dentry, body);
106 if (ret) {
107 spin_lock(&configfs_dirent_lock);
108 target_sd->s_links--;
109 spin_unlock(&configfs_dirent_lock);
110 configfs_put(target_sd);
111 kfree(body);
112 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800113 return ret;
114}
115
116
Al Viro421748e2008-08-02 01:04:36 -0400117static int get_target(const char *symname, struct path *path,
Al Virob7c177f2012-03-17 16:24:54 -0400118 struct config_item **target, struct super_block *sb)
Joel Becker7063fbf2005-12-15 14:29:43 -0800119{
120 int ret;
121
Al Viro421748e2008-08-02 01:04:36 -0400122 ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800123 if (!ret) {
Al Virob7c177f2012-03-17 16:24:54 -0400124 if (path->dentry->d_sb == sb) {
Al Viro421748e2008-08-02 01:04:36 -0400125 *target = configfs_get_config_item(path->dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800126 if (!*target) {
127 ret = -ENOENT;
Al Viro421748e2008-08-02 01:04:36 -0400128 path_put(path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800129 }
Al Viro9b6e3102010-01-13 22:10:57 -0500130 } else {
Joel Becker7063fbf2005-12-15 14:29:43 -0800131 ret = -EPERM;
Al Viro9b6e3102010-01-13 22:10:57 -0500132 path_put(path);
133 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800134 }
135
136 return ret;
137}
138
139
Christian Brauner549c7292021-01-21 14:19:43 +0100140int configfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
141 struct dentry *dentry, const char *symname)
Joel Becker7063fbf2005-12-15 14:29:43 -0800142{
143 int ret;
Al Viro421748e2008-08-02 01:04:36 -0400144 struct path path;
Louis Rilling2a109f22008-07-04 16:56:05 +0200145 struct configfs_dirent *sd;
Joel Becker7063fbf2005-12-15 14:29:43 -0800146 struct config_item *parent_item;
Subrata Modak3c48f232009-04-19 01:10:03 +0530147 struct config_item *target_item = NULL;
Bhumika Goyalaa293582017-10-16 17:18:40 +0200148 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800149
Louis Rilling2a109f22008-07-04 16:56:05 +0200150 sd = dentry->d_parent->d_fsdata;
151 /*
152 * Fake invisibility if dir belongs to a group/default groups hierarchy
153 * being attached
154 */
Louis Rilling2a109f22008-07-04 16:56:05 +0200155 if (!configfs_dirent_is_ready(sd))
Al Viroe9c03af2019-09-11 09:00:01 +0200156 return -ENOENT;
Louis Rilling2a109f22008-07-04 16:56:05 +0200157
Joel Becker7063fbf2005-12-15 14:29:43 -0800158 parent_item = configfs_get_config_item(dentry->d_parent);
159 type = parent_item->ci_type;
160
Louis Rilling2a109f22008-07-04 16:56:05 +0200161 ret = -EPERM;
Joel Becker7063fbf2005-12-15 14:29:43 -0800162 if (!type || !type->ct_item_ops ||
163 !type->ct_item_ops->allow_link)
164 goto out_put;
165
Al Viro351e5d82019-08-03 11:51:18 -0400166 /*
167 * This is really sick. What they wanted was a hybrid of
168 * link(2) and symlink(2) - they wanted the target resolved
169 * at syscall time (as link(2) would've done), be a directory
170 * (which link(2) would've refused to do) *AND* be a deep
171 * fucking magic, making the target busy from rmdir POV.
172 * symlink(2) is nothing of that sort, and the locking it
173 * gets matches the normal symlink(2) semantics. Without
174 * attempts to resolve the target (which might very well
175 * not even exist yet) done prior to locking the parent
176 * directory. This perversion, OTOH, needs to resolve
177 * the target, which would lead to obvious deadlocks if
178 * attempted with any directories locked.
179 *
180 * Unfortunately, that garbage is userland ABI and we should've
181 * said "no" back in 2005. Too late now, so we get to
182 * play very ugly games with locking.
183 *
184 * Try *ANYTHING* of that sort in new code, and you will
185 * really regret it. Just ask yourself - what could a BOFH
186 * do to me and do I want to find it out first-hand?
187 *
188 * AV, a thoroughly annoyed bastard.
189 */
190 inode_unlock(dir);
Al Virob7c177f2012-03-17 16:24:54 -0400191 ret = get_target(symname, &path, &target_item, dentry->d_sb);
Al Viro351e5d82019-08-03 11:51:18 -0400192 inode_lock(dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800193 if (ret)
194 goto out_put;
195
Al Viro351e5d82019-08-03 11:51:18 -0400196 if (dentry->d_inode || d_unhashed(dentry))
197 ret = -EEXIST;
198 else
Christian Brauner47291ba2021-01-21 14:19:24 +0100199 ret = inode_permission(&init_user_ns, dir,
200 MAY_WRITE | MAY_EXEC);
Al Viro351e5d82019-08-03 11:51:18 -0400201 if (!ret)
202 ret = type->ct_item_ops->allow_link(parent_item, target_item);
Louis Rillinge7520652008-06-12 17:26:47 +0200203 if (!ret) {
Louis Rilling9a73d782008-06-20 14:09:22 +0200204 mutex_lock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800205 ret = create_link(parent_item, target_item, dentry);
Louis Rilling9a73d782008-06-20 14:09:22 +0200206 mutex_unlock(&configfs_symlink_mutex);
Louis Rillinge7520652008-06-12 17:26:47 +0200207 if (ret && type->ct_item_ops->drop_link)
208 type->ct_item_ops->drop_link(parent_item,
209 target_item);
210 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800211
212 config_item_put(target_item);
Al Viro421748e2008-08-02 01:04:36 -0400213 path_put(&path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800214
215out_put:
216 config_item_put(parent_item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800217 return ret;
218}
219
220int configfs_unlink(struct inode *dir, struct dentry *dentry)
221{
Al Viroe9c03af2019-09-11 09:00:01 +0200222 struct configfs_dirent *sd = dentry->d_fsdata, *target_sd;
Joel Becker7063fbf2005-12-15 14:29:43 -0800223 struct config_item *parent_item;
Bhumika Goyalaa293582017-10-16 17:18:40 +0200224 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800225 int ret;
226
227 ret = -EPERM; /* What lack-of-symlink returns */
228 if (!(sd->s_type & CONFIGFS_ITEM_LINK))
229 goto out;
230
Al Viroe9c03af2019-09-11 09:00:01 +0200231 target_sd = sd->s_element;
Joel Becker7063fbf2005-12-15 14:29:43 -0800232
233 parent_item = configfs_get_config_item(dentry->d_parent);
234 type = parent_item->ci_type;
235
Louis Rilling6f610762008-06-16 19:00:58 +0200236 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800237 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200238 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800239 configfs_drop_dentry(sd, dentry->d_parent);
240 dput(dentry);
241 configfs_put(sd);
242
243 /*
244 * drop_link() must be called before
Al Viroe9c03af2019-09-11 09:00:01 +0200245 * decrementing target's ->s_links, so that the order of
Joel Becker7063fbf2005-12-15 14:29:43 -0800246 * drop_link(this, target) and drop_item(target) is preserved.
247 */
248 if (type && type->ct_item_ops &&
249 type->ct_item_ops->drop_link)
250 type->ct_item_ops->drop_link(parent_item,
Al Viroe9c03af2019-09-11 09:00:01 +0200251 target_sd->s_element);
Joel Becker7063fbf2005-12-15 14:29:43 -0800252
Louis Rilling5301a772008-06-16 19:00:59 +0200253 spin_lock(&configfs_dirent_lock);
Al Viroe9c03af2019-09-11 09:00:01 +0200254 target_sd->s_links--;
Louis Rilling5301a772008-06-16 19:00:59 +0200255 spin_unlock(&configfs_dirent_lock);
Al Viroe9c03af2019-09-11 09:00:01 +0200256 configfs_put(target_sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800257
258 config_item_put(parent_item);
259
260 ret = 0;
261
262out:
263 return ret;
264}
265
Arjan van de Ven754661f2007-02-12 00:55:38 -0800266const struct inode_operations configfs_symlink_inode_operations = {
Al Viroe9c03af2019-09-11 09:00:01 +0200267 .get_link = simple_get_link,
Joel Becker3d0f89b2006-01-25 13:31:07 -0800268 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -0800269};
270