dir.c 44.1 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 30 31 32
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * dir.c - Operations for configfs directories.
 *
 * 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.
 */

#undef DEBUG

#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/module.h>
#include <linux/slab.h>
33
#include <linux/err.h>
34 35 36 37 38

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

DECLARE_RWSEM(configfs_rename_sem);
39 40
/*
 * Protects mutations of configfs_dirent linkage together with proper i_mutex
41
 * Also protects mutations of symlinks linkage to target configfs_dirent
42 43
 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
 * and configfs_dirent_lock locked, in that order.
44 45
 * This allows one to safely traverse configfs_dirent trees and symlinks without
 * having to lock inodes.
46 47 48 49
 *
 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
 * unlocked is not reliable unless in detach_groups() called from
 * rmdir()/unregister() and from configfs_attach_group()
50 51
 */
DEFINE_SPINLOCK(configfs_dirent_lock);
52 53 54 55

static void configfs_d_iput(struct dentry * dentry,
			    struct inode * inode)
{
56
	struct configfs_dirent *sd = dentry->d_fsdata;
57 58 59

	if (sd) {
		BUG_ON(sd->s_dentry != dentry);
60 61
		/* Coordinate with configfs_readdir */
		spin_lock(&configfs_dirent_lock);
62
		sd->s_dentry = NULL;
63
		spin_unlock(&configfs_dirent_lock);
64 65 66 67 68 69 70 71 72
		configfs_put(sd);
	}
	iput(inode);
}

/*
 * We _must_ delete our dentries on last dput, as the chain-to-parent
 * behavior is required to clear the parents of default_groups.
 */
N
Nick Piggin 已提交
73
static int configfs_d_delete(const struct dentry *dentry)
74 75 76 77
{
	return 1;
}

A
Al Viro 已提交
78
const struct dentry_operations configfs_dentry_ops = {
79 80 81 82 83
	.d_iput		= configfs_d_iput,
	/* simple_delete_dentry() isn't exported */
	.d_delete	= configfs_d_delete,
};

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#ifdef CONFIG_LOCKDEP

/*
 * Helpers to make lockdep happy with our recursive locking of default groups'
 * inodes (see configfs_attach_group() and configfs_detach_group()).
 * We put default groups i_mutexes in separate classes according to their depth
 * from the youngest non-default group ancestor.
 *
 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
 * groups A/B and A/C will have their inode's mutex in class
 * default_group_class[0], and default group A/C/D will be in
 * default_group_class[1].
 *
 * The lock classes are declared and assigned in inode.c, according to the
 * s_depth value.
 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
 * default groups, and reset to -1 when all default groups are attached. During
 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
 * inode's mutex is set to default_group_class[s_depth - 1].
 */

static void configfs_init_dirent_depth(struct configfs_dirent *sd)
{
	sd->s_depth = -1;
}

static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
					  struct configfs_dirent *sd)
{
	int parent_depth = parent_sd->s_depth;

	if (parent_depth >= 0)
		sd->s_depth = parent_depth + 1;
}

static void
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
{
	/*
	 * item's i_mutex class is already setup, so s_depth is now only
	 * used to set new sub-directories s_depth, which is always done
	 * with item's i_mutex locked.
	 */
	/*
	 *  sd->s_depth == -1 iff we are a non default group.
	 *  else (we are a default group) sd->s_depth > 0 (see
	 *  create_dir()).
	 */
	if (sd->s_depth == -1)
		/*
		 * We are a non default group and we are going to create
		 * default groups.
		 */
		sd->s_depth = 0;
}

static void
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
{
	/* We will not create default groups anymore. */
	sd->s_depth = -1;
}

#else /* CONFIG_LOCKDEP */

static void configfs_init_dirent_depth(struct configfs_dirent *sd)
{
}

static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
					  struct configfs_dirent *sd)
{
}

static void
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
{
}

static void
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
{
}

#endif /* CONFIG_LOCKDEP */

170 171 172
/*
 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
 */
173 174
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
						   void *element, int type)
175 176 177
{
	struct configfs_dirent * sd;

178
	sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
179
	if (!sd)
180
		return ERR_PTR(-ENOMEM);
181 182 183 184 185

	atomic_set(&sd->s_count, 1);
	INIT_LIST_HEAD(&sd->s_links);
	INIT_LIST_HEAD(&sd->s_children);
	sd->s_element = element;
186
	sd->s_type = type;
187
	configfs_init_dirent_depth(sd);
188
	spin_lock(&configfs_dirent_lock);
189 190 191 192 193
	if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
		spin_unlock(&configfs_dirent_lock);
		kmem_cache_free(configfs_dir_cachep, sd);
		return ERR_PTR(-ENOENT);
	}
194 195
	list_add(&sd->s_sibling, &parent_sd->s_children);
	spin_unlock(&configfs_dirent_lock);
196 197 198 199

	return sd;
}

200 201 202 203 204 205 206
/*
 *
 * Return -EEXIST if there is already a configfs element with the same
 * name for the same parent.
 *
 * called with parent inode's i_mutex held
 */
207 208
static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
				  const unsigned char *new)
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
{
	struct configfs_dirent * sd;

	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
		if (sd->s_element) {
			const unsigned char *existing = configfs_get_name(sd);
			if (strcmp(existing, new))
				continue;
			else
				return -EEXIST;
		}
	}

	return 0;
}


226 227 228 229 230 231
int configfs_make_dirent(struct configfs_dirent * parent_sd,
			 struct dentry * dentry, void * element,
			 umode_t mode, int type)
{
	struct configfs_dirent * sd;

232
	sd = configfs_new_dirent(parent_sd, element, type);
233 234
	if (IS_ERR(sd))
		return PTR_ERR(sd);
235 236 237

	sd->s_mode = mode;
	sd->s_dentry = dentry;
238
	if (dentry)
239 240 241 242 243 244 245 246 247 248 249
		dentry->d_fsdata = configfs_get(sd);

	return 0;
}

static int init_dir(struct inode * inode)
{
	inode->i_op = &configfs_dir_inode_operations;
	inode->i_fop = &configfs_dir_operations;

	/* directory inodes start off with i_nlink == 2 (for "." entry) */
250
	inc_nlink(inode);
251 252 253
	return 0;
}

254
static int configfs_init_file(struct inode * inode)
255 256 257 258 259 260 261 262 263 264 265 266
{
	inode->i_size = PAGE_SIZE;
	inode->i_fop = &configfs_file_operations;
	return 0;
}

static int init_symlink(struct inode * inode)
{
	inode->i_op = &configfs_symlink_inode_operations;
	return 0;
}

267
static int create_dir(struct config_item *k, struct dentry *d)
268 269 270
{
	int error;
	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
271 272 273
	struct dentry *p = d->d_parent;

	BUG_ON(!k);
274

275 276 277
	error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
	if (!error)
		error = configfs_make_dirent(p->d_fsdata, d, k, mode,
278
					     CONFIGFS_DIR | CONFIGFS_USET_CREATING);
279
	if (!error) {
280
		configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
281
		error = configfs_create(d, mode, init_dir);
282
		if (!error) {
283
			inc_nlink(p->d_inode);
284 285 286
		} else {
			struct configfs_dirent *sd = d->d_fsdata;
			if (sd) {
287
				spin_lock(&configfs_dirent_lock);
288
				list_del_init(&sd->s_sibling);
289
				spin_unlock(&configfs_dirent_lock);
290 291
				configfs_put(sd);
			}
292 293 294 295 296 297 298 299 300 301
		}
	}
	return error;
}


/**
 *	configfs_create_dir - create a directory for an config_item.
 *	@item:		config_itemwe're creating directory for.
 *	@dentry:	config_item's dentry.
302 303 304
 *
 *	Note: user-created entries won't be allowed under this new directory
 *	until it is validated by configfs_dir_set_ready()
305 306 307 308
 */

static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
{
309
	int error = create_dir(item, dentry);
310 311 312 313 314
	if (!error)
		item->ci_dentry = dentry;
	return error;
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/*
 * Allow userspace to create new entries under a new directory created with
 * configfs_create_dir(), and under all of its chidlren directories recursively.
 * @sd		configfs_dirent of the new directory to validate
 *
 * Caller must hold configfs_dirent_lock.
 */
static void configfs_dir_set_ready(struct configfs_dirent *sd)
{
	struct configfs_dirent *child_sd;

	sd->s_type &= ~CONFIGFS_USET_CREATING;
	list_for_each_entry(child_sd, &sd->s_children, s_sibling)
		if (child_sd->s_type & CONFIGFS_USET_CREATING)
			configfs_dir_set_ready(child_sd);
}

/*
 * Check that a directory does not belong to a directory hierarchy being
 * attached and not validated yet.
 * @sd		configfs_dirent of the directory to check
 *
 * @return	non-zero iff the directory was validated
 *
 * Note: takes configfs_dirent_lock, so the result may change from false to true
 * in two consecutive calls, but never from true to false.
 */
int configfs_dirent_is_ready(struct configfs_dirent *sd)
{
	int ret;

	spin_lock(&configfs_dirent_lock);
	ret = !(sd->s_type & CONFIGFS_USET_CREATING);
	spin_unlock(&configfs_dirent_lock);

	return ret;
}

353 354 355 356 357 358 359
int configfs_create_link(struct configfs_symlink *sl,
			 struct dentry *parent,
			 struct dentry *dentry)
{
	int err = 0;
	umode_t mode = S_IFLNK | S_IRWXUGO;

360 361
	err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
				   CONFIGFS_ITEM_LINK);
362
	if (!err) {
363
		err = configfs_create(dentry, mode, init_symlink);
364
		if (err) {
365 366
			struct configfs_dirent *sd = dentry->d_fsdata;
			if (sd) {
367
				spin_lock(&configfs_dirent_lock);
368
				list_del_init(&sd->s_sibling);
369
				spin_unlock(&configfs_dirent_lock);
370 371 372
				configfs_put(sd);
			}
		}
373 374 375 376 377 378 379 380 381 382
	}
	return err;
}

static void remove_dir(struct dentry * d)
{
	struct dentry * parent = dget(d->d_parent);
	struct configfs_dirent * sd;

	sd = d->d_fsdata;
383
	spin_lock(&configfs_dirent_lock);
384
	list_del_init(&sd->s_sibling);
385
	spin_unlock(&configfs_dirent_lock);
386 387 388 389
	configfs_put(sd);
	if (d->d_inode)
		simple_rmdir(parent->d_inode,d);

N
Nick Piggin 已提交
390
	pr_debug(" o %s removing done (%d)\n",d->d_name.name, d->d_count);
391 392 393 394 395 396 397 398 399 400 401

	dput(parent);
}

/**
 * configfs_remove_dir - remove an config_item's directory.
 * @item:	config_item we're removing.
 *
 * The only thing special about this is that we remove any files in
 * the directory before we remove the directory, and we've inlined
 * what used to be configfs_rmdir() below, instead of calling separately.
402 403
 *
 * Caller holds the mutex of the item's inode
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
 */

static void configfs_remove_dir(struct config_item * item)
{
	struct dentry * dentry = dget(item->ci_dentry);

	if (!dentry)
		return;

	remove_dir(dentry);
	/**
	 * Drop reference from dget() on entrance.
	 */
	dput(dentry);
}


/* attaches attribute's configfs_dirent to the dentry corresponding to the
 * attribute file
 */
static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
{
	struct configfs_attribute * attr = sd->s_element;
	int error;

429 430
	dentry->d_fsdata = configfs_get(sd);
	sd->s_dentry = dentry;
431 432
	error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
				configfs_init_file);
433 434
	if (error) {
		configfs_put(sd);
435
		return error;
436
	}
437 438 439 440 441 442 443 444

	d_rehash(dentry);

	return 0;
}

static struct dentry * configfs_lookup(struct inode *dir,
				       struct dentry *dentry,
A
Al Viro 已提交
445
				       unsigned int flags)
446 447 448 449
{
	struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
	struct configfs_dirent * sd;
	int found = 0;
450 451 452 453 454 455 456 457 458 459 460 461 462
	int err;

	/*
	 * Fake invisibility if dir belongs to a group/default groups hierarchy
	 * being attached
	 *
	 * This forbids userspace to read/write attributes of items which may
	 * not complete their initialization, since the dentries of the
	 * attributes won't be instantiated.
	 */
	err = -ENOENT;
	if (!configfs_dirent_is_ready(parent_sd))
		goto out;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
		if (sd->s_type & CONFIGFS_NOT_PINNED) {
			const unsigned char * name = configfs_get_name(sd);

			if (strcmp(name, dentry->d_name.name))
				continue;

			found = 1;
			err = configfs_attach_attr(sd, dentry);
			break;
		}
	}

	if (!found) {
		/*
		 * If it doesn't exist and it isn't a NOT_PINNED item,
		 * it must be negative.
		 */
482 483 484 485
		if (dentry->d_name.len > NAME_MAX)
			return ERR_PTR(-ENAMETOOLONG);
		d_add(dentry, NULL);
		return NULL;
486 487
	}

488
out:
489 490 491 492 493
	return ERR_PTR(err);
}

/*
 * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
494 495 496 497 498
 * attributes and are removed by rmdir().  We recurse, setting
 * CONFIGFS_USET_DROPPING on all children that are candidates for
 * default detach.
 * If there is an error, the caller will reset the flags via
 * configfs_detach_rollback().
499
 */
500
static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
501 502 503 504 505
{
	struct configfs_dirent *parent_sd = dentry->d_fsdata;
	struct configfs_dirent *sd;
	int ret;

506 507 508
	/* Mark that we're trying to drop the group */
	parent_sd->s_type |= CONFIGFS_USET_DROPPING;

509 510 511 512 513 514
	ret = -EBUSY;
	if (!list_empty(&parent_sd->s_links))
		goto out;

	ret = 0;
	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
515 516
		if (!sd->s_element ||
		    (sd->s_type & CONFIGFS_NOT_PINNED))
517 518
			continue;
		if (sd->s_type & CONFIGFS_USET_DEFAULT) {
519 520 521 522 523 524
			/* Abort if racing with mkdir() */
			if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
				if (wait_mutex)
					*wait_mutex = &sd->s_dentry->d_inode->i_mutex;
				return -EAGAIN;
			}
525

526 527 528 529
			/*
			 * Yup, recursive.  If there's a problem, blame
			 * deep nesting of default_groups
			 */
530
			ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
531
			if (!ret)
532
				continue;
533 534 535 536 537 538 539 540 541 542 543
		} else
			ret = -ENOTEMPTY;

		break;
	}

out:
	return ret;
}

/*
544
 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
545 546 547 548 549 550 551
 * set.
 */
static void configfs_detach_rollback(struct dentry *dentry)
{
	struct configfs_dirent *parent_sd = dentry->d_fsdata;
	struct configfs_dirent *sd;

552 553 554 555
	parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;

	list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
		if (sd->s_type & CONFIGFS_USET_DEFAULT)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			configfs_detach_rollback(sd->s_dentry);
}

static void detach_attrs(struct config_item * item)
{
	struct dentry * dentry = dget(item->ci_dentry);
	struct configfs_dirent * parent_sd;
	struct configfs_dirent * sd, * tmp;

	if (!dentry)
		return;

	pr_debug("configfs %s: dropping attrs for  dir\n",
		 dentry->d_name.name);

	parent_sd = dentry->d_fsdata;
	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
		if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
			continue;
575
		spin_lock(&configfs_dirent_lock);
576
		list_del_init(&sd->s_sibling);
577
		spin_unlock(&configfs_dirent_lock);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
		configfs_drop_dentry(sd, dentry);
		configfs_put(sd);
	}

	/**
	 * Drop reference from dget() on entrance.
	 */
	dput(dentry);
}

static int populate_attrs(struct config_item *item)
{
	struct config_item_type *t = item->ci_type;
	struct configfs_attribute *attr;
	int error = 0;
	int i;

	if (!t)
		return -EINVAL;
	if (t->ct_attrs) {
		for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
			if ((error = configfs_create_file(item, attr)))
				break;
		}
	}

	if (error)
		detach_attrs(item);

	return error;
}

static int configfs_attach_group(struct config_item *parent_item,
				 struct config_item *item,
				 struct dentry *dentry);
static void configfs_detach_group(struct config_item *item);

static void detach_groups(struct config_group *group)
{
	struct dentry * dentry = dget(group->cg_item.ci_dentry);
	struct dentry *child;
	struct configfs_dirent *parent_sd;
	struct configfs_dirent *sd, *tmp;

	if (!dentry)
		return;

	parent_sd = dentry->d_fsdata;
	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
		if (!sd->s_element ||
		    !(sd->s_type & CONFIGFS_USET_DEFAULT))
			continue;

		child = sd->s_dentry;

633 634
		mutex_lock(&child->d_inode->i_mutex);

635 636
		configfs_detach_group(sd->s_element);
		child->d_inode->i_flags |= S_DEAD;
637
		dont_mount(child);
638

639
		mutex_unlock(&child->d_inode->i_mutex);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

		d_delete(child);
		dput(child);
	}

	/**
	 * Drop reference from dget() on entrance.
	 */
	dput(dentry);
}

/*
 * This fakes mkdir(2) on a default_groups[] entry.  It
 * creates a dentry, attachs it, and then does fixup
 * on the sd->s_type.
 *
 * We could, perhaps, tweak our parent's ->mkdir for a minute and
 * try using vfs_mkdir.  Just a thought.
 */
static int create_default_group(struct config_group *parent_group,
				struct config_group *group)
{
	int ret;
	struct qstr name;
	struct configfs_dirent *sd;
	/* We trust the caller holds a reference to parent */
	struct dentry *child, *parent = parent_group->cg_item.ci_dentry;

	if (!group->cg_item.ci_name)
		group->cg_item.ci_name = group->cg_item.ci_namebuf;
	name.name = group->cg_item.ci_name;
	name.len = strlen(name.name);
	name.hash = full_name_hash(name.name, name.len);

	ret = -ENOMEM;
	child = d_alloc(parent, &name);
	if (child) {
		d_add(child, NULL);

		ret = configfs_attach_group(&parent_group->cg_item,
					    &group->cg_item, child);
		if (!ret) {
			sd = child->d_fsdata;
			sd->s_type |= CONFIGFS_USET_DEFAULT;
		} else {
685 686
			BUG_ON(child->d_inode);
			d_drop(child);
687 688 689 690 691 692 693 694 695 696 697 698 699
			dput(child);
		}
	}

	return ret;
}

static int populate_groups(struct config_group *group)
{
	struct config_group *new_group;
	int ret = 0;
	int i;

700
	if (group->default_groups) {
701 702 703 704
		for (i = 0; group->default_groups[i]; i++) {
			new_group = group->default_groups[i];

			ret = create_default_group(group, new_group);
705 706
			if (ret) {
				detach_groups(group);
707
				break;
708
			}
709 710 711 712 713 714 715 716
		}
	}

	return ret;
}

/*
 * All of link_obj/unlink_obj/link_group/unlink_group require that
717
 * subsys->su_mutex is held.
718 719 720 721 722 723 724 725 726 727 728 729
 */

static void unlink_obj(struct config_item *item)
{
	struct config_group *group;

	group = item->ci_group;
	if (group) {
		list_del_init(&item->ci_entry);

		item->ci_group = NULL;
		item->ci_parent = NULL;
730 731

		/* Drop the reference for ci_entry */
732 733
		config_item_put(item);

734
		/* Drop the reference for ci_parent */
735 736 737 738 739 740
		config_group_put(group);
	}
}

static void link_obj(struct config_item *parent_item, struct config_item *item)
{
741 742 743 744
	/*
	 * Parent seems redundant with group, but it makes certain
	 * traversals much nicer.
	 */
745
	item->ci_parent = parent_item;
746 747 748 749 750

	/*
	 * We hold a reference on the parent for the child's ci_parent
	 * link.
	 */
751 752 753
	item->ci_group = config_group_get(to_config_group(parent_item));
	list_add_tail(&item->ci_entry, &item->ci_group->cg_children);

754 755 756 757
	/*
	 * We hold a reference on the child for ci_entry on the parent's
	 * cg_children
	 */
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	config_item_get(item);
}

static void unlink_group(struct config_group *group)
{
	int i;
	struct config_group *new_group;

	if (group->default_groups) {
		for (i = 0; group->default_groups[i]; i++) {
			new_group = group->default_groups[i];
			unlink_group(new_group);
		}
	}

	group->cg_subsys = NULL;
	unlink_obj(&group->cg_item);
}

static void link_group(struct config_group *parent_group, struct config_group *group)
{
	int i;
	struct config_group *new_group;
	struct configfs_subsystem *subsys = NULL; /* gcc is a turd */

	link_obj(&parent_group->cg_item, &group->cg_item);

	if (parent_group->cg_subsys)
		subsys = parent_group->cg_subsys;
	else if (configfs_is_root(&parent_group->cg_item))
		subsys = to_configfs_subsystem(group);
	else
		BUG();
	group->cg_subsys = subsys;

	if (group->default_groups) {
		for (i = 0; group->default_groups[i]; i++) {
			new_group = group->default_groups[i];
			link_group(group, new_group);
		}
	}
}

/*
 * The goal is that configfs_attach_item() (and
 * configfs_attach_group()) can be called from either the VFS or this
 * module.  That is, they assume that the items have been created,
 * the dentry allocated, and the dcache is all ready to go.
 *
 * If they fail, they must clean up after themselves as if they
 * had never been called.  The caller (VFS or local function) will
 * handle cleaning up the dcache bits.
 *
 * configfs_detach_group() and configfs_detach_item() behave similarly on
 * the way out.  They assume that the proper semaphores are held, they
 * clean up the configfs items, and they expect their callers will
 * handle the dcache bits.
 */
static int configfs_attach_item(struct config_item *parent_item,
				struct config_item *item,
				struct dentry *dentry)
{
	int ret;

	ret = configfs_create_dir(item, dentry);
	if (!ret) {
		ret = populate_attrs(item);
		if (ret) {
826 827 828 829 830 831
			/*
			 * We are going to remove an inode and its dentry but
			 * the VFS may already have hit and used them. Thus,
			 * we must lock them as rmdir() would.
			 */
			mutex_lock(&dentry->d_inode->i_mutex);
832
			configfs_remove_dir(item);
833
			dentry->d_inode->i_flags |= S_DEAD;
834
			dont_mount(dentry);
835
			mutex_unlock(&dentry->d_inode->i_mutex);
836 837 838 839 840 841 842
			d_delete(dentry);
		}
	}

	return ret;
}

843
/* Caller holds the mutex of the item's inode */
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static void configfs_detach_item(struct config_item *item)
{
	detach_attrs(item);
	configfs_remove_dir(item);
}

static int configfs_attach_group(struct config_item *parent_item,
				 struct config_item *item,
				 struct dentry *dentry)
{
	int ret;
	struct configfs_dirent *sd;

	ret = configfs_attach_item(parent_item, item, dentry);
	if (!ret) {
		sd = dentry->d_fsdata;
		sd->s_type |= CONFIGFS_USET_DIR;

862 863 864 865 866 867 868 869 870 871
		/*
		 * FYI, we're faking mkdir in populate_groups()
		 * We must lock the group's inode to avoid races with the VFS
		 * which can already hit the inode and try to add/remove entries
		 * under it.
		 *
		 * We must also lock the inode to remove it safely in case of
		 * error, as rmdir() would.
		 */
		mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
872
		configfs_adjust_dir_dirent_depth_before_populate(sd);
873 874 875
		ret = populate_groups(to_config_group(item));
		if (ret) {
			configfs_detach_item(item);
876
			dentry->d_inode->i_flags |= S_DEAD;
877
			dont_mount(dentry);
878
		}
879
		configfs_adjust_dir_dirent_depth_after_populate(sd);
880 881 882
		mutex_unlock(&dentry->d_inode->i_mutex);
		if (ret)
			d_delete(dentry);
883 884 885 886 887
	}

	return ret;
}

888
/* Caller holds the mutex of the group's inode */
889 890 891 892 893 894
static void configfs_detach_group(struct config_item *item)
{
	detach_groups(to_config_group(item));
	configfs_detach_item(item);
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
/*
 * After the item has been detached from the filesystem view, we are
 * ready to tear it out of the hierarchy.  Notify the client before
 * we do that so they can perform any cleanup that requires
 * navigating the hierarchy.  A client does not need to provide this
 * callback.  The subsystem semaphore MUST be held by the caller, and
 * references must be valid for both items.  It also assumes the
 * caller has validated ci_type.
 */
static void client_disconnect_notify(struct config_item *parent_item,
				     struct config_item *item)
{
	struct config_item_type *type;

	type = parent_item->ci_type;
	BUG_ON(!type);

	if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
		type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
						      item);
}

917 918 919 920 921 922 923 924 925 926 927 928 929 930
/*
 * Drop the initial reference from make_item()/make_group()
 * This function assumes that reference is held on item
 * and that item holds a valid reference to the parent.  Also, it
 * assumes the caller has validated ci_type.
 */
static void client_drop_item(struct config_item *parent_item,
			     struct config_item *item)
{
	struct config_item_type *type;

	type = parent_item->ci_type;
	BUG_ON(!type);

931 932 933 934
	/*
	 * If ->drop_item() exists, it is responsible for the
	 * config_item_put().
	 */
935 936
	if (type->ct_group_ops && type->ct_group_ops->drop_item)
		type->ct_group_ops->drop_item(to_config_group(parent_item),
937
					      item);
938 939 940 941
	else
		config_item_put(item);
}

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
#ifdef DEBUG
static void configfs_dump_one(struct configfs_dirent *sd, int level)
{
	printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));

#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
	type_print(CONFIGFS_ROOT);
	type_print(CONFIGFS_DIR);
	type_print(CONFIGFS_ITEM_ATTR);
	type_print(CONFIGFS_ITEM_LINK);
	type_print(CONFIGFS_USET_DIR);
	type_print(CONFIGFS_USET_DEFAULT);
	type_print(CONFIGFS_USET_DROPPING);
#undef type_print
}

static int configfs_dump(struct configfs_dirent *sd, int level)
{
	struct configfs_dirent *child_sd;
	int ret = 0;

	configfs_dump_one(sd, level);

	if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
		return 0;

	list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
		ret = configfs_dump(child_sd, level + 2);
		if (ret)
			break;
	}

	return ret;
}
#endif


/*
 * configfs_depend_item() and configfs_undepend_item()
 *
 * WARNING: Do not call these from a configfs callback!
 *
 * This describes these functions and their helpers.
 *
 * Allow another kernel system to depend on a config_item.  If this
L
Lucas De Marchi 已提交
987
 * happens, the item cannot go away until the dependent can live without
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
 * it.  The idea is to give client modules as simple an interface as
 * possible.  When a system asks them to depend on an item, they just
 * call configfs_depend_item().  If the item is live and the client
 * driver is in good shape, we'll happily do the work for them.
 *
 * Why is the locking complex?  Because configfs uses the VFS to handle
 * all locking, but this function is called outside the normal
 * VFS->configfs path.  So it must take VFS locks to prevent the
 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc).  This is
 * why you can't call these functions underneath configfs callbacks.
 *
 * Note, btw, that this can be called at *any* time, even when a configfs
 * subsystem isn't registered, or when configfs is loading or unloading.
 * Just like configfs_register_subsystem().  So we take the same
1002 1003
 * precautions.  We pin the filesystem.  We lock configfs_dirent_lock.
 * If we can find the target item in the
1004
 * configfs tree, it must be part of the subsystem tree as well, so we
1005 1006
 * do not need the subsystem semaphore.  Holding configfs_dirent_lock helps
 * locking out mkdir() and rmdir(), who might be racing us.
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 */

/*
 * configfs_depend_prep()
 *
 * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
 * attributes.  This is similar but not the same to configfs_detach_prep().
 * Note that configfs_detach_prep() expects the parent to be locked when it
 * is called, but we lock the parent *inside* configfs_depend_prep().  We
 * do that so we can unlock it if we find nothing.
 *
 * Here we do a depth-first search of the dentry hierarchy looking for
1019 1020 1021 1022 1023 1024 1025 1026 1027
 * our object.
 * We deliberately ignore items tagged as dropping since they are virtually
 * dead, as well as items in the middle of attachment since they virtually
 * do not exist yet. This completes the locking out of racing mkdir() and
 * rmdir().
 * Note: subdirectories in the middle of attachment start with s_type =
 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir().  When
 * CONFIGFS_USET_CREATING is set, we ignore the item.  The actual set of
 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1028
 *
1029
 * If the target is not found, -ENOENT is bubbled up.
1030 1031 1032
 *
 * This adds a requirement that all config_items be unique!
 *
1033
 * This is recursive.  There isn't
1034 1035 1036 1037 1038 1039
 * much on the stack, though, so folks that need this function - be careful
 * about your stack!  Patches will be accepted to make it iterative.
 */
static int configfs_depend_prep(struct dentry *origin,
				struct config_item *target)
{
1040
	struct configfs_dirent *child_sd, *sd;
1041 1042
	int ret = 0;

1043 1044
	BUG_ON(!origin || !origin->d_fsdata);
	sd = origin->d_fsdata;
1045 1046 1047 1048 1049

	if (sd->s_element == target)  /* Boo-yah */
		goto out;

	list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1050 1051 1052
		if ((child_sd->s_type & CONFIGFS_DIR) &&
		    !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
		    !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
			ret = configfs_depend_prep(child_sd->s_dentry,
						   target);
			if (!ret)
				goto out;  /* Child path boo-yah */
		}
	}

	/* We looped all our children and didn't find target */
	ret = -ENOENT;

out:
	return ret;
}

int configfs_depend_item(struct configfs_subsystem *subsys,
			 struct config_item *target)
{
	int ret;
	struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
	struct config_item *s_item = &subsys->su_group.cg_item;
A
Al Viro 已提交
1073
	struct dentry *root;
1074 1075 1076 1077 1078

	/*
	 * Pin the configfs filesystem.  This means we can safely access
	 * the root of the configfs filesystem.
	 */
1079 1080 1081
	root = configfs_pin_fs();
	if (IS_ERR(root))
		return PTR_ERR(root);
1082 1083 1084 1085 1086 1087

	/*
	 * Next, lock the root directory.  We're going to check that the
	 * subsystem is really registered, and so we need to lock out
	 * configfs_[un]register_subsystem().
	 */
A
Al Viro 已提交
1088
	mutex_lock(&root->d_inode->i_mutex);
1089

A
Al Viro 已提交
1090
	root_sd = root->d_fsdata;
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

	list_for_each_entry(p, &root_sd->s_children, s_sibling) {
		if (p->s_type & CONFIGFS_DIR) {
			if (p->s_element == s_item) {
				subsys_sd = p;
				break;
			}
		}
	}

	if (!subsys_sd) {
		ret = -ENOENT;
		goto out_unlock_fs;
	}

	/* Ok, now we can trust subsys/s_item */

1108 1109
	spin_lock(&configfs_dirent_lock);
	/* Scan the tree, return 0 if found */
1110 1111
	ret = configfs_depend_prep(subsys_sd->s_dentry, target);
	if (ret)
1112
		goto out_unlock_dirent_lock;
1113

1114 1115 1116 1117
	/*
	 * We are sure that the item is not about to be removed by rmdir(), and
	 * not in the middle of attachment by mkdir().
	 */
1118 1119 1120
	p = target->ci_dentry->d_fsdata;
	p->s_dependent_count += 1;

1121 1122
out_unlock_dirent_lock:
	spin_unlock(&configfs_dirent_lock);
1123
out_unlock_fs:
A
Al Viro 已提交
1124
	mutex_unlock(&root->d_inode->i_mutex);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

	/*
	 * If we succeeded, the fs is pinned via other methods.  If not,
	 * we're done with it anyway.  So release_fs() is always right.
	 */
	configfs_release_fs();

	return ret;
}
EXPORT_SYMBOL(configfs_depend_item);

/*
 * Release the dependent linkage.  This is much simpler than
 * configfs_depend_item() because we know that that the client driver is
 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
 */
void configfs_undepend_item(struct configfs_subsystem *subsys,
			    struct config_item *target)
{
	struct configfs_dirent *sd;

	/*
1147 1148
	 * Since we can trust everything is pinned, we just need
	 * configfs_dirent_lock.
1149
	 */
1150
	spin_lock(&configfs_dirent_lock);
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

	sd = target->ci_dentry->d_fsdata;
	BUG_ON(sd->s_dependent_count < 1);

	sd->s_dependent_count -= 1;

	/*
	 * After this unlock, we cannot trust the item to stay alive!
	 * DO NOT REFERENCE item after this unlock.
	 */
1161
	spin_unlock(&configfs_dirent_lock);
1162 1163
}
EXPORT_SYMBOL(configfs_undepend_item);
1164

1165
static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1166
{
1167 1168 1169 1170
	int ret = 0;
	int module_got = 0;
	struct config_group *group = NULL;
	struct config_item *item = NULL;
1171 1172 1173 1174
	struct config_item *parent_item;
	struct configfs_subsystem *subsys;
	struct configfs_dirent *sd;
	struct config_item_type *type;
1175
	struct module *subsys_owner = NULL, *new_item_owner = NULL;
1176 1177 1178
	char *name;

	sd = dentry->d_parent->d_fsdata;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

	/*
	 * Fake invisibility if dir belongs to a group/default groups hierarchy
	 * being attached
	 */
	if (!configfs_dirent_is_ready(sd)) {
		ret = -ENOENT;
		goto out;
	}

1189 1190 1191 1192
	if (!(sd->s_type & CONFIGFS_USET_DIR)) {
		ret = -EPERM;
		goto out;
	}
1193

1194
	/* Get a working ref for the duration of this function */
1195 1196 1197 1198 1199 1200 1201 1202
	parent_item = configfs_get_config_item(dentry->d_parent);
	type = parent_item->ci_type;
	subsys = to_config_group(parent_item)->cg_subsys;
	BUG_ON(!subsys);

	if (!type || !type->ct_group_ops ||
	    (!type->ct_group_ops->make_group &&
	     !type->ct_group_ops->make_item)) {
1203 1204
		ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
		goto out_put;
1205 1206
	}

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	/*
	 * The subsystem may belong to a different module than the item
	 * being created.  We don't want to safely pin the new item but
	 * fail to pin the subsystem it sits under.
	 */
	if (!subsys->su_group.cg_item.ci_type) {
		ret = -EINVAL;
		goto out_put;
	}
	subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
	if (!try_module_get(subsys_owner)) {
		ret = -EINVAL;
		goto out_put;
	}

1222 1223
	name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
	if (!name) {
1224
		ret = -ENOMEM;
1225
		goto out_subsys_put;
1226
	}
1227

1228 1229
	snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);

1230
	mutex_lock(&subsys->su_mutex);
1231
	if (type->ct_group_ops->make_group) {
1232
		group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1233 1234 1235
		if (!group)
			group = ERR_PTR(-ENOMEM);
		if (!IS_ERR(group)) {
1236 1237
			link_group(to_config_group(parent_item), group);
			item = &group->cg_item;
1238 1239
		} else
			ret = PTR_ERR(group);
1240
	} else {
1241
		item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1242 1243 1244
		if (!item)
			item = ERR_PTR(-ENOMEM);
		if (!IS_ERR(item))
1245
			link_obj(parent_item, item);
1246 1247
		else
			ret = PTR_ERR(item);
1248
	}
1249
	mutex_unlock(&subsys->su_mutex);
1250 1251

	kfree(name);
1252
	if (ret) {
1253
		/*
1254
		 * If ret != 0, then link_obj() was never called.
1255 1256
		 * There are no extra references to clean up.
		 */
1257
		goto out_subsys_put;
1258 1259
	}

1260 1261 1262 1263 1264
	/*
	 * link_obj() has been called (via link_group() for groups).
	 * From here on out, errors must clean that up.
	 */

1265
	type = item->ci_type;
1266 1267 1268 1269
	if (!type) {
		ret = -EINVAL;
		goto out_unlink;
	}
1270

1271 1272
	new_item_owner = type->ct_owner;
	if (!try_module_get(new_item_owner)) {
1273 1274 1275
		ret = -EINVAL;
		goto out_unlink;
	}
1276

1277 1278 1279 1280 1281 1282 1283
	/*
	 * I hate doing it this way, but if there is
	 * an error,  module_put() probably should
	 * happen after any cleanup.
	 */
	module_got = 1;

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	/*
	 * Make racing rmdir() fail if it did not tag parent with
	 * CONFIGFS_USET_DROPPING
	 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
	 * fail and let rmdir() terminate correctly
	 */
	spin_lock(&configfs_dirent_lock);
	/* This will make configfs_detach_prep() fail */
	sd->s_type |= CONFIGFS_USET_IN_MKDIR;
	spin_unlock(&configfs_dirent_lock);

1295 1296 1297 1298 1299
	if (group)
		ret = configfs_attach_group(parent_item, item, dentry);
	else
		ret = configfs_attach_item(parent_item, item, dentry);

1300 1301
	spin_lock(&configfs_dirent_lock);
	sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1302 1303
	if (!ret)
		configfs_dir_set_ready(dentry->d_fsdata);
1304 1305
	spin_unlock(&configfs_dirent_lock);

1306 1307 1308
out_unlink:
	if (ret) {
		/* Tear down everything we built up */
1309
		mutex_lock(&subsys->su_mutex);
1310 1311

		client_disconnect_notify(parent_item, item);
1312 1313 1314 1315 1316
		if (group)
			unlink_group(group);
		else
			unlink_obj(item);
		client_drop_item(parent_item, item);
1317

1318
		mutex_unlock(&subsys->su_mutex);
1319 1320

		if (module_got)
1321
			module_put(new_item_owner);
1322 1323
	}

1324 1325 1326 1327
out_subsys_put:
	if (ret)
		module_put(subsys_owner);

1328 1329
out_put:
	/*
1330 1331 1332
	 * link_obj()/link_group() took a reference from child->parent,
	 * so the parent is safely pinned.  We can drop our working
	 * reference.
1333 1334 1335 1336
	 */
	config_item_put(parent_item);

out:
1337 1338 1339 1340 1341 1342 1343 1344 1345
	return ret;
}

static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
{
	struct config_item *parent_item;
	struct config_item *item;
	struct configfs_subsystem *subsys;
	struct configfs_dirent *sd;
1346
	struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1347 1348 1349 1350 1351 1352
	int ret;

	sd = dentry->d_fsdata;
	if (sd->s_type & CONFIGFS_USET_DEFAULT)
		return -EPERM;

1353
	/* Get a working ref until we have the child */
1354 1355 1356 1357 1358 1359 1360 1361 1362
	parent_item = configfs_get_config_item(dentry->d_parent);
	subsys = to_config_group(parent_item)->cg_subsys;
	BUG_ON(!subsys);

	if (!parent_item->ci_type) {
		config_item_put(parent_item);
		return -EINVAL;
	}

1363 1364 1365 1366
	/* configfs_mkdir() shouldn't have allowed this */
	BUG_ON(!subsys->su_group.cg_item.ci_type);
	subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;

1367 1368 1369 1370
	/*
	 * Ensure that no racing symlink() will make detach_prep() fail while
	 * the new link is temporarily attached
	 */
1371 1372 1373
	do {
		struct mutex *wait_mutex;

1374 1375
		mutex_lock(&configfs_symlink_mutex);
		spin_lock(&configfs_dirent_lock);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
		/*
		 * Here's where we check for dependents.  We're protected by
		 * configfs_dirent_lock.
		 * If no dependent, atomically tag the item as dropping.
		 */
		ret = sd->s_dependent_count ? -EBUSY : 0;
		if (!ret) {
			ret = configfs_detach_prep(dentry, &wait_mutex);
			if (ret)
				configfs_detach_rollback(dentry);
		}
1387 1388 1389 1390
		spin_unlock(&configfs_dirent_lock);
		mutex_unlock(&configfs_symlink_mutex);

		if (ret) {
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
			if (ret != -EAGAIN) {
				config_item_put(parent_item);
				return ret;
			}

			/* Wait until the racing operation terminates */
			mutex_lock(wait_mutex);
			mutex_unlock(wait_mutex);
		}
	} while (ret == -EAGAIN);
1401

1402
	/* Get a working ref for the duration of this function */
1403 1404 1405 1406 1407 1408
	item = configfs_get_config_item(dentry);

	/* Drop reference from above, item already holds one. */
	config_item_put(parent_item);

	if (item->ci_type)
1409
		dead_item_owner = item->ci_type->ct_owner;
1410 1411 1412 1413

	if (sd->s_type & CONFIGFS_USET_DIR) {
		configfs_detach_group(item);

1414
		mutex_lock(&subsys->su_mutex);
1415
		client_disconnect_notify(parent_item, item);
1416 1417 1418 1419
		unlink_group(to_config_group(item));
	} else {
		configfs_detach_item(item);

1420
		mutex_lock(&subsys->su_mutex);
1421
		client_disconnect_notify(parent_item, item);
1422 1423 1424 1425
		unlink_obj(item);
	}

	client_drop_item(parent_item, item);
1426
	mutex_unlock(&subsys->su_mutex);
1427 1428 1429 1430

	/* Drop our reference from above */
	config_item_put(item);

1431 1432
	module_put(dead_item_owner);
	module_put(subsys_owner);
1433 1434 1435 1436

	return 0;
}

1437
const struct inode_operations configfs_dir_inode_operations = {
1438 1439 1440 1441 1442
	.mkdir		= configfs_mkdir,
	.rmdir		= configfs_rmdir,
	.symlink	= configfs_symlink,
	.unlink		= configfs_unlink,
	.lookup		= configfs_lookup,
1443
	.setattr	= configfs_setattr,
1444 1445
};

1446 1447 1448 1449 1450
const struct inode_operations configfs_root_inode_operations = {
	.lookup		= configfs_lookup,
	.setattr	= configfs_setattr,
};

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
#if 0
int configfs_rename_dir(struct config_item * item, const char *new_name)
{
	int error = 0;
	struct dentry * new_dentry, * parent;

	if (!strcmp(config_item_name(item), new_name))
		return -EINVAL;

	if (!item->parent)
		return -EINVAL;

	down_write(&configfs_rename_sem);
	parent = item->parent->dentry;

1466
	mutex_lock(&parent->d_inode->i_mutex);
1467 1468 1469

	new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
	if (!IS_ERR(new_dentry)) {
1470
		if (!new_dentry->d_inode) {
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
			error = config_item_set_name(item, "%s", new_name);
			if (!error) {
				d_add(new_dentry, NULL);
				d_move(item->dentry, new_dentry);
			}
			else
				d_delete(new_dentry);
		} else
			error = -EEXIST;
		dput(new_dentry);
	}
1482
	mutex_unlock(&parent->d_inode->i_mutex);
1483 1484 1485 1486 1487 1488 1489 1490
	up_write(&configfs_rename_sem);

	return error;
}
#endif

static int configfs_dir_open(struct inode *inode, struct file *file)
{
1491
	struct dentry * dentry = file->f_path.dentry;
1492
	struct configfs_dirent * parent_sd = dentry->d_fsdata;
1493
	int err;
1494

1495
	mutex_lock(&dentry->d_inode->i_mutex);
1496 1497 1498 1499 1500 1501
	/*
	 * Fake invisibility if dir belongs to a group/default groups hierarchy
	 * being attached
	 */
	err = -ENOENT;
	if (configfs_dirent_is_ready(parent_sd)) {
1502
		file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1503 1504 1505 1506 1507
		if (IS_ERR(file->private_data))
			err = PTR_ERR(file->private_data);
		else
			err = 0;
	}
1508
	mutex_unlock(&dentry->d_inode->i_mutex);
1509

1510
	return err;
1511 1512 1513 1514
}

static int configfs_dir_close(struct inode *inode, struct file *file)
{
1515
	struct dentry * dentry = file->f_path.dentry;
1516 1517
	struct configfs_dirent * cursor = file->private_data;

1518
	mutex_lock(&dentry->d_inode->i_mutex);
1519
	spin_lock(&configfs_dirent_lock);
1520
	list_del_init(&cursor->s_sibling);
1521
	spin_unlock(&configfs_dirent_lock);
1522
	mutex_unlock(&dentry->d_inode->i_mutex);
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

	release_configfs_dirent(cursor);

	return 0;
}

/* Relationship between s_mode and the DT_xxx types */
static inline unsigned char dt_type(struct configfs_dirent *sd)
{
	return (sd->s_mode >> 12) & 15;
}

static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
{
1537
	struct dentry *dentry = filp->f_path.dentry;
A
Al Viro 已提交
1538
	struct super_block *sb = dentry->d_sb;
1539 1540 1541
	struct configfs_dirent * parent_sd = dentry->d_fsdata;
	struct configfs_dirent *cursor = filp->private_data;
	struct list_head *p, *q = &cursor->s_sibling;
1542
	ino_t ino = 0;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
	int i = filp->f_pos;

	switch (i) {
		case 0:
			ino = dentry->d_inode->i_ino;
			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
				break;
			filp->f_pos++;
			i++;
			/* fallthrough */
		case 1:
			ino = parent_ino(dentry);
			if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
				break;
			filp->f_pos++;
			i++;
			/* fallthrough */
		default:
			if (filp->f_pos == 2) {
1562
				spin_lock(&configfs_dirent_lock);
A
Akinobu Mita 已提交
1563
				list_move(q, &parent_sd->s_children);
1564
				spin_unlock(&configfs_dirent_lock);
1565 1566 1567 1568 1569
			}
			for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
				struct configfs_dirent *next;
				const char * name;
				int len;
1570
				struct inode *inode = NULL;
1571 1572 1573 1574 1575 1576 1577 1578

				next = list_entry(p, struct configfs_dirent,
						   s_sibling);
				if (!next->s_element)
					continue;

				name = configfs_get_name(next);
				len = strlen(name);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

				/*
				 * We'll have a dentry and an inode for
				 * PINNED items and for open attribute
				 * files.  We lock here to prevent a race
				 * with configfs_d_iput() clearing
				 * s_dentry before calling iput().
				 *
				 * Why do we go to the trouble?  If
				 * someone has an attribute file open,
				 * the inode number should match until
				 * they close it.  Beyond that, we don't
				 * care.
				 */
				spin_lock(&configfs_dirent_lock);
				dentry = next->s_dentry;
				if (dentry)
					inode = dentry->d_inode;
				if (inode)
					ino = inode->i_ino;
				spin_unlock(&configfs_dirent_lock);
				if (!inode)
A
Al Viro 已提交
1601
					ino = iunique(sb, 2);
1602 1603 1604 1605 1606

				if (filldir(dirent, name, len, filp->f_pos, ino,
						 dt_type(next)) < 0)
					return 0;

1607
				spin_lock(&configfs_dirent_lock);
A
Akinobu Mita 已提交
1608
				list_move(q, p);
1609
				spin_unlock(&configfs_dirent_lock);
1610 1611 1612 1613 1614 1615 1616
				p = q;
				filp->f_pos++;
			}
	}
	return 0;
}

1617
static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1618
{
1619
	struct dentry * dentry = file->f_path.dentry;
1620

1621
	mutex_lock(&dentry->d_inode->i_mutex);
1622
	switch (whence) {
1623 1624 1625 1626 1627 1628
		case 1:
			offset += file->f_pos;
		case 0:
			if (offset >= 0)
				break;
		default:
A
Al Viro 已提交
1629
			mutex_unlock(&file_inode(file)->i_mutex);
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
			return -EINVAL;
	}
	if (offset != file->f_pos) {
		file->f_pos = offset;
		if (file->f_pos >= 2) {
			struct configfs_dirent *sd = dentry->d_fsdata;
			struct configfs_dirent *cursor = file->private_data;
			struct list_head *p;
			loff_t n = file->f_pos - 2;

1640
			spin_lock(&configfs_dirent_lock);
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
			list_del(&cursor->s_sibling);
			p = sd->s_children.next;
			while (n && p != &sd->s_children) {
				struct configfs_dirent *next;
				next = list_entry(p, struct configfs_dirent,
						   s_sibling);
				if (next->s_element)
					n--;
				p = p->next;
			}
			list_add_tail(&cursor->s_sibling, p);
1652
			spin_unlock(&configfs_dirent_lock);
1653 1654
		}
	}
1655
	mutex_unlock(&dentry->d_inode->i_mutex);
1656 1657 1658
	return offset;
}

1659
const struct file_operations configfs_dir_operations = {
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
	.open		= configfs_dir_open,
	.release	= configfs_dir_close,
	.llseek		= configfs_dir_lseek,
	.read		= generic_read_dir,
	.readdir	= configfs_readdir,
};

int configfs_register_subsystem(struct configfs_subsystem *subsys)
{
	int err;
	struct config_group *group = &subsys->su_group;
	struct qstr name;
	struct dentry *dentry;
A
Al Viro 已提交
1673
	struct dentry *root;
1674 1675
	struct configfs_dirent *sd;

1676 1677 1678
	root = configfs_pin_fs();
	if (IS_ERR(root))
		return PTR_ERR(root);
1679 1680 1681 1682

	if (!group->cg_item.ci_name)
		group->cg_item.ci_name = group->cg_item.ci_namebuf;

A
Al Viro 已提交
1683
	sd = root->d_fsdata;
1684 1685
	link_group(to_config_group(sd->s_element), group);

A
Al Viro 已提交
1686
	mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1687 1688 1689 1690 1691 1692

	name.name = group->cg_item.ci_name;
	name.len = strlen(name.name);
	name.hash = full_name_hash(name.name, name.len);

	err = -ENOMEM;
A
Al Viro 已提交
1693
	dentry = d_alloc(root, &name);
1694 1695
	if (dentry) {
		d_add(dentry, NULL);
1696

1697 1698 1699
		err = configfs_attach_group(sd->s_element, &group->cg_item,
					    dentry);
		if (err) {
1700 1701
			BUG_ON(dentry->d_inode);
			d_drop(dentry);
1702
			dput(dentry);
1703 1704 1705 1706
		} else {
			spin_lock(&configfs_dirent_lock);
			configfs_dir_set_ready(dentry->d_fsdata);
			spin_unlock(&configfs_dirent_lock);
1707 1708
		}
	}
1709

A
Al Viro 已提交
1710
	mutex_unlock(&root->d_inode->i_mutex);
1711

1712 1713 1714
	if (err) {
		unlink_group(group);
		configfs_release_fs();
1715 1716 1717 1718 1719 1720 1721 1722 1723
	}

	return err;
}

void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
{
	struct config_group *group = &subsys->su_group;
	struct dentry *dentry = group->cg_item.ci_dentry;
A
Al Viro 已提交
1724
	struct dentry *root = dentry->d_sb->s_root;
1725

A
Al Viro 已提交
1726
	if (dentry->d_parent != root) {
1727 1728 1729 1730
		printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
		return;
	}

A
Al Viro 已提交
1731
	mutex_lock_nested(&root->d_inode->i_mutex,
M
Mark Fasheh 已提交
1732 1733
			  I_MUTEX_PARENT);
	mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1734
	mutex_lock(&configfs_symlink_mutex);
1735
	spin_lock(&configfs_dirent_lock);
1736
	if (configfs_detach_prep(dentry, NULL)) {
1737 1738
		printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
	}
1739
	spin_unlock(&configfs_dirent_lock);
1740
	mutex_unlock(&configfs_symlink_mutex);
1741 1742
	configfs_detach_group(&group->cg_item);
	dentry->d_inode->i_flags |= S_DEAD;
1743
	dont_mount(dentry);
1744
	mutex_unlock(&dentry->d_inode->i_mutex);
1745 1746 1747

	d_delete(dentry);

A
Al Viro 已提交
1748
	mutex_unlock(&root->d_inode->i_mutex);
1749 1750 1751 1752 1753 1754 1755 1756 1757

	dput(dentry);

	unlink_group(group);
	configfs_release_fs();
}

EXPORT_SYMBOL(configfs_register_subsystem);
EXPORT_SYMBOL(configfs_unregister_subsystem);