xref: /openbmc/linux/fs/configfs/symlink.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fa60ce2cSMasahiro Yamada /*
37063fbf2SJoel Becker  * symlink.c - operations for configfs symlinks.
47063fbf2SJoel Becker  *
57063fbf2SJoel Becker  * Based on sysfs:
67063fbf2SJoel Becker  * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
77063fbf2SJoel Becker  *
87063fbf2SJoel Becker  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
97063fbf2SJoel Becker  */
107063fbf2SJoel Becker 
117063fbf2SJoel Becker #include <linux/fs.h>
127063fbf2SJoel Becker #include <linux/module.h>
137063fbf2SJoel Becker #include <linux/namei.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
157063fbf2SJoel Becker 
167063fbf2SJoel Becker #include <linux/configfs.h>
177063fbf2SJoel Becker #include "configfs_internal.h"
187063fbf2SJoel Becker 
199a73d78cSLouis Rilling /* Protects attachments of new symlinks */
209a73d78cSLouis Rilling DEFINE_MUTEX(configfs_symlink_mutex);
219a73d78cSLouis Rilling 
item_depth(struct config_item * item)227063fbf2SJoel Becker static int item_depth(struct config_item * item)
237063fbf2SJoel Becker {
247063fbf2SJoel Becker 	struct config_item * p = item;
257063fbf2SJoel Becker 	int depth = 0;
267063fbf2SJoel Becker 	do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
277063fbf2SJoel Becker 	return depth;
287063fbf2SJoel Becker }
297063fbf2SJoel Becker 
item_path_length(struct config_item * item)307063fbf2SJoel Becker static int item_path_length(struct config_item * item)
317063fbf2SJoel Becker {
327063fbf2SJoel Becker 	struct config_item * p = item;
337063fbf2SJoel Becker 	int length = 1;
347063fbf2SJoel Becker 	do {
357063fbf2SJoel Becker 		length += strlen(config_item_name(p)) + 1;
367063fbf2SJoel Becker 		p = p->ci_parent;
377063fbf2SJoel Becker 	} while (p && !configfs_is_root(p));
387063fbf2SJoel Becker 	return length;
397063fbf2SJoel Becker }
407063fbf2SJoel Becker 
fill_item_path(struct config_item * item,char * buffer,int length)417063fbf2SJoel Becker static void fill_item_path(struct config_item * item, char * buffer, int length)
427063fbf2SJoel Becker {
437063fbf2SJoel Becker 	struct config_item * p;
447063fbf2SJoel Becker 
457063fbf2SJoel Becker 	--length;
467063fbf2SJoel Becker 	for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
477063fbf2SJoel Becker 		int cur = strlen(config_item_name(p));
487063fbf2SJoel Becker 
497063fbf2SJoel Becker 		/* back up enough to print this bus id with '/' */
507063fbf2SJoel Becker 		length -= cur;
511823342aSGuenter Roeck 		memcpy(buffer + length, config_item_name(p), cur);
527063fbf2SJoel Becker 		*(buffer + --length) = '/';
537063fbf2SJoel Becker 	}
547063fbf2SJoel Becker }
557063fbf2SJoel Becker 
configfs_get_target_path(struct config_item * item,struct config_item * target,char * path)56e9c03af2SAl Viro static int configfs_get_target_path(struct config_item *item,
57e9c03af2SAl Viro 		struct config_item *target, char *path)
58e9c03af2SAl Viro {
59e9c03af2SAl Viro 	int depth, size;
60e9c03af2SAl Viro 	char *s;
61e9c03af2SAl Viro 
62e9c03af2SAl Viro 	depth = item_depth(item);
63e9c03af2SAl Viro 	size = item_path_length(target) + depth * 3 - 1;
64e9c03af2SAl Viro 	if (size > PATH_MAX)
65e9c03af2SAl Viro 		return -ENAMETOOLONG;
66e9c03af2SAl Viro 
67e9c03af2SAl Viro 	pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
68e9c03af2SAl Viro 
69e9c03af2SAl Viro 	for (s = path; depth--; s += 3)
70e9c03af2SAl Viro 		strcpy(s,"../");
71e9c03af2SAl Viro 
72e9c03af2SAl Viro 	fill_item_path(target, path, size);
73e9c03af2SAl Viro 	pr_debug("%s: path = '%s'\n", __func__, path);
74e9c03af2SAl Viro 	return 0;
75e9c03af2SAl Viro }
76e9c03af2SAl Viro 
create_link(struct config_item * parent_item,struct config_item * item,struct dentry * dentry)777063fbf2SJoel Becker static int create_link(struct config_item *parent_item,
787063fbf2SJoel Becker 		       struct config_item *item,
797063fbf2SJoel Becker 		       struct dentry *dentry)
807063fbf2SJoel Becker {
817063fbf2SJoel Becker 	struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
82e9c03af2SAl Viro 	char *body;
837063fbf2SJoel Becker 	int ret;
847063fbf2SJoel Becker 
852a109f2aSLouis Rilling 	if (!configfs_dirent_is_ready(target_sd))
86e9c03af2SAl Viro 		return -ENOENT;
87e9c03af2SAl Viro 
88e9c03af2SAl Viro 	body = kzalloc(PAGE_SIZE, GFP_KERNEL);
89e9c03af2SAl Viro 	if (!body)
90e9c03af2SAl Viro 		return -ENOMEM;
91e9c03af2SAl Viro 
92e9c03af2SAl Viro 	configfs_get(target_sd);
935301a77dSLouis Rilling 	spin_lock(&configfs_dirent_lock);
944768e9b1SLouis Rilling 	if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
954768e9b1SLouis Rilling 		spin_unlock(&configfs_dirent_lock);
96e9c03af2SAl Viro 		configfs_put(target_sd);
97e9c03af2SAl Viro 		kfree(body);
984768e9b1SLouis Rilling 		return -ENOENT;
994768e9b1SLouis Rilling 	}
100e9c03af2SAl Viro 	target_sd->s_links++;
1015301a77dSLouis Rilling 	spin_unlock(&configfs_dirent_lock);
102e2f238f7SHonggang Li 	ret = configfs_get_target_path(parent_item, item, body);
103e9c03af2SAl Viro 	if (!ret)
104e9c03af2SAl Viro 		ret = configfs_create_link(target_sd, parent_item->ci_dentry,
105e9c03af2SAl Viro 					   dentry, body);
1067063fbf2SJoel Becker 	if (ret) {
1075301a77dSLouis Rilling 		spin_lock(&configfs_dirent_lock);
108e9c03af2SAl Viro 		target_sd->s_links--;
1095301a77dSLouis Rilling 		spin_unlock(&configfs_dirent_lock);
110e9c03af2SAl Viro 		configfs_put(target_sd);
111e9c03af2SAl Viro 		kfree(body);
1127063fbf2SJoel Becker 	}
1137063fbf2SJoel Becker 	return ret;
1147063fbf2SJoel Becker }
1157063fbf2SJoel Becker 
1167063fbf2SJoel Becker 
get_target(const char * symname,struct path * path,struct config_item ** target,struct super_block * sb)117421748ecSAl Viro static int get_target(const char *symname, struct path *path,
118b7c177fcSAl Viro 		      struct config_item **target, struct super_block *sb)
1197063fbf2SJoel Becker {
1207063fbf2SJoel Becker 	int ret;
1217063fbf2SJoel Becker 
122421748ecSAl Viro 	ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
1237063fbf2SJoel Becker 	if (!ret) {
124b7c177fcSAl Viro 		if (path->dentry->d_sb == sb) {
125421748ecSAl Viro 			*target = configfs_get_config_item(path->dentry);
1267063fbf2SJoel Becker 			if (!*target) {
1277063fbf2SJoel Becker 				ret = -ENOENT;
128421748ecSAl Viro 				path_put(path);
1297063fbf2SJoel Becker 			}
1309b6e3102SAl Viro 		} else {
1317063fbf2SJoel Becker 			ret = -EPERM;
1329b6e3102SAl Viro 			path_put(path);
1339b6e3102SAl Viro 		}
1347063fbf2SJoel Becker 	}
1357063fbf2SJoel Becker 
1367063fbf2SJoel Becker 	return ret;
1377063fbf2SJoel Becker }
1387063fbf2SJoel Becker 
1397063fbf2SJoel Becker 
configfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)1407a77db95SChristian Brauner int configfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
141549c7297SChristian Brauner 		     struct dentry *dentry, const char *symname)
1427063fbf2SJoel Becker {
1437063fbf2SJoel Becker 	int ret;
144421748ecSAl Viro 	struct path path;
1452a109f2aSLouis Rilling 	struct configfs_dirent *sd;
1467063fbf2SJoel Becker 	struct config_item *parent_item;
1473c48f23aSSubrata Modak 	struct config_item *target_item = NULL;
148aa293583SBhumika Goyal 	const struct config_item_type *type;
1497063fbf2SJoel Becker 
1502a109f2aSLouis Rilling 	sd = dentry->d_parent->d_fsdata;
1512a109f2aSLouis Rilling 	/*
1522a109f2aSLouis Rilling 	 * Fake invisibility if dir belongs to a group/default groups hierarchy
1532a109f2aSLouis Rilling 	 * being attached
1542a109f2aSLouis Rilling 	 */
1552a109f2aSLouis Rilling 	if (!configfs_dirent_is_ready(sd))
156e9c03af2SAl Viro 		return -ENOENT;
1572a109f2aSLouis Rilling 
1587063fbf2SJoel Becker 	parent_item = configfs_get_config_item(dentry->d_parent);
1597063fbf2SJoel Becker 	type = parent_item->ci_type;
1607063fbf2SJoel Becker 
1612a109f2aSLouis Rilling 	ret = -EPERM;
1627063fbf2SJoel Becker 	if (!type || !type->ct_item_ops ||
1637063fbf2SJoel Becker 	    !type->ct_item_ops->allow_link)
1647063fbf2SJoel Becker 		goto out_put;
1657063fbf2SJoel Becker 
166351e5d86SAl Viro 	/*
167351e5d86SAl Viro 	 * This is really sick.  What they wanted was a hybrid of
168351e5d86SAl Viro 	 * link(2) and symlink(2) - they wanted the target resolved
169351e5d86SAl Viro 	 * at syscall time (as link(2) would've done), be a directory
170351e5d86SAl Viro 	 * (which link(2) would've refused to do) *AND* be a deep
171351e5d86SAl Viro 	 * fucking magic, making the target busy from rmdir POV.
172351e5d86SAl Viro 	 * symlink(2) is nothing of that sort, and the locking it
173351e5d86SAl Viro 	 * gets matches the normal symlink(2) semantics.  Without
174351e5d86SAl Viro 	 * attempts to resolve the target (which might very well
175351e5d86SAl Viro 	 * not even exist yet) done prior to locking the parent
176351e5d86SAl Viro 	 * directory.  This perversion, OTOH, needs to resolve
177351e5d86SAl Viro 	 * the target, which would lead to obvious deadlocks if
178351e5d86SAl Viro 	 * attempted with any directories locked.
179351e5d86SAl Viro 	 *
180351e5d86SAl Viro 	 * Unfortunately, that garbage is userland ABI and we should've
181351e5d86SAl Viro 	 * said "no" back in 2005.  Too late now, so we get to
182351e5d86SAl Viro 	 * play very ugly games with locking.
183351e5d86SAl Viro 	 *
184351e5d86SAl Viro 	 * Try *ANYTHING* of that sort in new code, and you will
185351e5d86SAl Viro 	 * really regret it.  Just ask yourself - what could a BOFH
186351e5d86SAl Viro 	 * do to me and do I want to find it out first-hand?
187351e5d86SAl Viro 	 *
188351e5d86SAl Viro 	 *  AV, a thoroughly annoyed bastard.
189351e5d86SAl Viro 	 */
190351e5d86SAl Viro 	inode_unlock(dir);
191b7c177fcSAl Viro 	ret = get_target(symname, &path, &target_item, dentry->d_sb);
192351e5d86SAl Viro 	inode_lock(dir);
1937063fbf2SJoel Becker 	if (ret)
1947063fbf2SJoel Becker 		goto out_put;
1957063fbf2SJoel Becker 
196351e5d86SAl Viro 	if (dentry->d_inode || d_unhashed(dentry))
197351e5d86SAl Viro 		ret = -EEXIST;
198351e5d86SAl Viro 	else
199*4609e1f1SChristian Brauner 		ret = inode_permission(&nop_mnt_idmap, dir,
20047291baaSChristian Brauner 				       MAY_WRITE | MAY_EXEC);
201351e5d86SAl Viro 	if (!ret)
2027063fbf2SJoel Becker 		ret = type->ct_item_ops->allow_link(parent_item, target_item);
203e7520651SLouis Rilling 	if (!ret) {
2049a73d78cSLouis Rilling 		mutex_lock(&configfs_symlink_mutex);
2057063fbf2SJoel Becker 		ret = create_link(parent_item, target_item, dentry);
2069a73d78cSLouis Rilling 		mutex_unlock(&configfs_symlink_mutex);
207e7520651SLouis Rilling 		if (ret && type->ct_item_ops->drop_link)
208e7520651SLouis Rilling 			type->ct_item_ops->drop_link(parent_item,
209e7520651SLouis Rilling 						     target_item);
210e7520651SLouis Rilling 	}
2117063fbf2SJoel Becker 
2127063fbf2SJoel Becker 	config_item_put(target_item);
213421748ecSAl Viro 	path_put(&path);
2147063fbf2SJoel Becker 
2157063fbf2SJoel Becker out_put:
2167063fbf2SJoel Becker 	config_item_put(parent_item);
2177063fbf2SJoel Becker 	return ret;
2187063fbf2SJoel Becker }
2197063fbf2SJoel Becker 
configfs_unlink(struct inode * dir,struct dentry * dentry)2207063fbf2SJoel Becker int configfs_unlink(struct inode *dir, struct dentry *dentry)
2217063fbf2SJoel Becker {
222e9c03af2SAl Viro 	struct configfs_dirent *sd = dentry->d_fsdata, *target_sd;
2237063fbf2SJoel Becker 	struct config_item *parent_item;
224aa293583SBhumika Goyal 	const struct config_item_type *type;
2257063fbf2SJoel Becker 	int ret;
2267063fbf2SJoel Becker 
2277063fbf2SJoel Becker 	ret = -EPERM;  /* What lack-of-symlink returns */
2287063fbf2SJoel Becker 	if (!(sd->s_type & CONFIGFS_ITEM_LINK))
2297063fbf2SJoel Becker 		goto out;
2307063fbf2SJoel Becker 
231e9c03af2SAl Viro 	target_sd = sd->s_element;
2327063fbf2SJoel Becker 
2337063fbf2SJoel Becker 	parent_item = configfs_get_config_item(dentry->d_parent);
2347063fbf2SJoel Becker 	type = parent_item->ci_type;
2357063fbf2SJoel Becker 
2366f610764SLouis Rilling 	spin_lock(&configfs_dirent_lock);
2377063fbf2SJoel Becker 	list_del_init(&sd->s_sibling);
2386f610764SLouis Rilling 	spin_unlock(&configfs_dirent_lock);
2397063fbf2SJoel Becker 	configfs_drop_dentry(sd, dentry->d_parent);
2407063fbf2SJoel Becker 	dput(dentry);
2417063fbf2SJoel Becker 	configfs_put(sd);
2427063fbf2SJoel Becker 
2437063fbf2SJoel Becker 	/*
2447063fbf2SJoel Becker 	 * drop_link() must be called before
245e9c03af2SAl Viro 	 * decrementing target's ->s_links, so that the order of
2467063fbf2SJoel Becker 	 * drop_link(this, target) and drop_item(target) is preserved.
2477063fbf2SJoel Becker 	 */
2487063fbf2SJoel Becker 	if (type && type->ct_item_ops &&
2497063fbf2SJoel Becker 	    type->ct_item_ops->drop_link)
2507063fbf2SJoel Becker 		type->ct_item_ops->drop_link(parent_item,
251e9c03af2SAl Viro 					       target_sd->s_element);
2527063fbf2SJoel Becker 
2535301a77dSLouis Rilling 	spin_lock(&configfs_dirent_lock);
254e9c03af2SAl Viro 	target_sd->s_links--;
2555301a77dSLouis Rilling 	spin_unlock(&configfs_dirent_lock);
256e9c03af2SAl Viro 	configfs_put(target_sd);
2577063fbf2SJoel Becker 
2587063fbf2SJoel Becker 	config_item_put(parent_item);
2597063fbf2SJoel Becker 
2607063fbf2SJoel Becker 	ret = 0;
2617063fbf2SJoel Becker 
2627063fbf2SJoel Becker out:
2637063fbf2SJoel Becker 	return ret;
2647063fbf2SJoel Becker }
2657063fbf2SJoel Becker 
266754661f1SArjan van de Ven const struct inode_operations configfs_symlink_inode_operations = {
267e9c03af2SAl Viro 	.get_link = simple_get_link,
2683d0f89bbSJoel Becker 	.setattr = configfs_setattr,
2697063fbf2SJoel Becker };
2707063fbf2SJoel Becker 
271