symlink.c 8.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * symlink.c - operations for configfs symlinks.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 *
 * Based on sysfs:
 * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
 *
 * configfs Copyright (C) 2005 Oracle.  All rights reserved.
 */

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/namei.h>
30
#include <linux/slab.h>
31 32 33 34

#include <linux/configfs.h>
#include "configfs_internal.h"

35 36 37
/* Protects attachments of new symlinks */
DEFINE_MUTEX(configfs_symlink_mutex);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static int item_depth(struct config_item * item)
{
	struct config_item * p = item;
	int depth = 0;
	do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
	return depth;
}

static int item_path_length(struct config_item * item)
{
	struct config_item * p = item;
	int length = 1;
	do {
		length += strlen(config_item_name(p)) + 1;
		p = p->ci_parent;
	} while (p && !configfs_is_root(p));
	return length;
}

static void fill_item_path(struct config_item * item, char * buffer, int length)
{
	struct config_item * p;

	--length;
	for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
		int cur = strlen(config_item_name(p));

		/* back up enough to print this bus id with '/' */
		length -= cur;
67
		memcpy(buffer + length, config_item_name(p), cur);
68 69 70 71 72
		*(buffer + --length) = '/';
	}
}

static int create_link(struct config_item *parent_item,
73
		       struct config_item *item,
74 75 76 77 78 79
		       struct dentry *dentry)
{
	struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
	struct configfs_symlink *sl;
	int ret;

80 81 82
	ret = -ENOENT;
	if (!configfs_dirent_is_ready(target_sd))
		goto out;
83 84 85
	ret = -ENOMEM;
	sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
	if (sl) {
86
		spin_lock(&configfs_dirent_lock);
87 88 89 90 91
		if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
			spin_unlock(&configfs_dirent_lock);
			kfree(sl);
			return -ENOENT;
		}
92
		sl->sl_target = config_item_get(item);
93
		list_add(&sl->sl_list, &target_sd->s_links);
94
		spin_unlock(&configfs_dirent_lock);
95 96 97
		ret = configfs_create_link(sl, parent_item->ci_dentry,
					   dentry);
		if (ret) {
98
			spin_lock(&configfs_dirent_lock);
99
			list_del_init(&sl->sl_list);
100
			spin_unlock(&configfs_dirent_lock);
101 102 103 104 105
			config_item_put(item);
			kfree(sl);
		}
	}

106
out:
107 108 109 110
	return ret;
}


111
static int get_target(const char *symname, struct path *path,
Al Viro's avatar
Al Viro committed
112
		      struct config_item **target, struct super_block *sb)
113 114 115
{
	int ret;

116
	ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
117
	if (!ret) {
Al Viro's avatar
Al Viro committed
118
		if (path->dentry->d_sb == sb) {
119
			*target = configfs_get_config_item(path->dentry);
120 121
			if (!*target) {
				ret = -ENOENT;
122
				path_put(path);
123
			}
Al Viro's avatar
Al Viro committed
124
		} else {
125
			ret = -EPERM;
Al Viro's avatar
Al Viro committed
126 127
			path_put(path);
		}
128 129 130 131 132 133 134 135 136
	}

	return ret;
}


int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	int ret;
137
	struct path path;
138
	struct configfs_dirent *sd;
139
	struct config_item *parent_item;
140
	struct config_item *target_item = NULL;
141 142
	struct config_item_type *type;

143 144 145 146 147 148 149 150 151
	sd = dentry->d_parent->d_fsdata;
	/*
	 * Fake invisibility if dir belongs to a group/default groups hierarchy
	 * being attached
	 */
	ret = -ENOENT;
	if (!configfs_dirent_is_ready(sd))
		goto out;

152 153 154
	parent_item = configfs_get_config_item(dentry->d_parent);
	type = parent_item->ci_type;

155
	ret = -EPERM;
156 157 158 159
	if (!type || !type->ct_item_ops ||
	    !type->ct_item_ops->allow_link)
		goto out_put;

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	/*
	 * This is really sick.  What they wanted was a hybrid of
	 * link(2) and symlink(2) - they wanted the target resolved
	 * at syscall time (as link(2) would've done), be a directory
	 * (which link(2) would've refused to do) *AND* be a deep
	 * fucking magic, making the target busy from rmdir POV.
	 * symlink(2) is nothing of that sort, and the locking it
	 * gets matches the normal symlink(2) semantics.  Without
	 * attempts to resolve the target (which might very well
	 * not even exist yet) done prior to locking the parent
	 * directory.  This perversion, OTOH, needs to resolve
	 * the target, which would lead to obvious deadlocks if
	 * attempted with any directories locked.
	 *
	 * Unfortunately, that garbage is userland ABI and we should've
	 * said "no" back in 2005.  Too late now, so we get to
	 * play very ugly games with locking.
	 *
	 * Try *ANYTHING* of that sort in new code, and you will
	 * really regret it.  Just ask yourself - what could a BOFH
	 * do to me and do I want to find it out first-hand?
	 *
	 *  AV, a thoroughly annoyed bastard.
	 */
	inode_unlock(dir);
Al Viro's avatar
Al Viro committed
185
	ret = get_target(symname, &path, &target_item, dentry->d_sb);
186
	inode_lock(dir);
187 188 189
	if (ret)
		goto out_put;

190 191 192 193 194 195
	if (dentry->d_inode || d_unhashed(dentry))
		ret = -EEXIST;
	else
		ret = inode_permission(dir, MAY_WRITE | MAY_EXEC);
	if (!ret)
		ret = type->ct_item_ops->allow_link(parent_item, target_item);
196
	if (!ret) {
197
		mutex_lock(&configfs_symlink_mutex);
198
		ret = create_link(parent_item, target_item, dentry);
199
		mutex_unlock(&configfs_symlink_mutex);
200 201 202 203
		if (ret && type->ct_item_ops->drop_link)
			type->ct_item_ops->drop_link(parent_item,
						     target_item);
	}
204 205

	config_item_put(target_item);
206
	path_put(&path);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

out_put:
	config_item_put(parent_item);

out:
	return ret;
}

int configfs_unlink(struct inode *dir, struct dentry *dentry)
{
	struct configfs_dirent *sd = dentry->d_fsdata;
	struct configfs_symlink *sl;
	struct config_item *parent_item;
	struct config_item_type *type;
	int ret;

	ret = -EPERM;  /* What lack-of-symlink returns */
	if (!(sd->s_type & CONFIGFS_ITEM_LINK))
		goto out;

	sl = sd->s_element;

	parent_item = configfs_get_config_item(dentry->d_parent);
	type = parent_item->ci_type;

232
	spin_lock(&configfs_dirent_lock);
233
	list_del_init(&sd->s_sibling);
234
	spin_unlock(&configfs_dirent_lock);
235 236 237 238 239 240 241 242 243 244 245 246 247 248
	configfs_drop_dentry(sd, dentry->d_parent);
	dput(dentry);
	configfs_put(sd);

	/*
	 * drop_link() must be called before
	 * list_del_init(&sl->sl_list), so that the order of
	 * drop_link(this, target) and drop_item(target) is preserved.
	 */
	if (type && type->ct_item_ops &&
	    type->ct_item_ops->drop_link)
		type->ct_item_ops->drop_link(parent_item,
					       sl->sl_target);

249
	spin_lock(&configfs_dirent_lock);
250
	list_del_init(&sl->sl_list);
251
	spin_unlock(&configfs_dirent_lock);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

	/* Put reference from create_link() */
	config_item_put(sl->sl_target);
	kfree(sl);

	config_item_put(parent_item);

	ret = 0;

out:
	return ret;
}

static int configfs_get_target_path(struct config_item * item, struct config_item * target,
				   char *path)
{
	char * s;
	int depth, size;

	depth = item_depth(item);
	size = item_path_length(target) + depth * 3 - 1;
	if (size > PATH_MAX)
		return -ENAMETOOLONG;

276
	pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
277 278 279 280 281

	for (s = path; depth--; s += 3)
		strcpy(s,"../");

	fill_item_path(target, path, size);
282
	pr_debug("%s: path = '%s'\n", __func__, path);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

	return 0;
}

static int configfs_getlink(struct dentry *dentry, char * path)
{
	struct config_item *item, *target_item;
	int error = 0;

	item = configfs_get_config_item(dentry->d_parent);
	if (!item)
		return -EINVAL;

	target_item = configfs_get_config_item(dentry);
	if (!target_item) {
		config_item_put(item);
		return -EINVAL;
	}

	down_read(&configfs_rename_sem);
	error = configfs_get_target_path(item, target_item, path);
	up_read(&configfs_rename_sem);

	config_item_put(item);
	config_item_put(target_item);
	return error;

}

312
static const char *configfs_get_link(struct dentry *dentry,
313 314
				     struct inode *inode,
				     struct delayed_call *done)
315
{
316
	char *body;
317
	int error;
318

319 320 321
	if (!dentry)
		return ERR_PTR(-ECHILD);

322 323
	body = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!body)
324 325
		return ERR_PTR(-ENOMEM);

326
	error = configfs_getlink(dentry, body);
327
	if (!error) {
328 329
		set_delayed_call(done, kfree_link, body);
		return body;
330 331
	}

332
	kfree(body);
333
	return ERR_PTR(error);
334 335
}

336
const struct inode_operations configfs_symlink_inode_operations = {
337
	.get_link = configfs_get_link,
338
	.readlink = generic_readlink,
339
	.setattr = configfs_setattr,
340 341
};