dir.c 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * fs/kernfs/dir.c - kernfs directory implementation
 *
 * Copyright (c) 2001-3 Patrick Mochel
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
 *
 * This file is released under the GPLv2.
 */
10 11 12 13 14 15 16 17 18 19 20 21

#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/idr.h>
#include <linux/slab.h>
#include <linux/security.h>
#include <linux/hash.h>

#include "kernfs-internal.h"

DEFINE_MUTEX(sysfs_mutex);

22
#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/**
 *	sysfs_name_hash
 *	@name: Null terminated string to hash
 *	@ns:   Namespace tag to hash
 *
 *	Returns 31 bit hash of ns + name (so it fits in an off_t )
 */
static unsigned int sysfs_name_hash(const char *name, const void *ns)
{
	unsigned long hash = init_name_hash();
	unsigned int len = strlen(name);
	while (len--)
		hash = partial_name_hash(*name++, hash);
	hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
	hash &= 0x7fffffffU;
	/* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
	if (hash < 1)
		hash += 2;
	if (hash >= INT_MAX)
		hash = INT_MAX - 1;
	return hash;
}

static int sysfs_name_compare(unsigned int hash, const char *name,
48
			      const void *ns, const struct kernfs_node *kn)
49
{
50 51 52 53 54
	if (hash != kn->hash)
		return hash - kn->hash;
	if (ns != kn->ns)
		return ns - kn->ns;
	return strcmp(name, kn->name);
55 56
}

57 58
static int sysfs_sd_compare(const struct kernfs_node *left,
			    const struct kernfs_node *right)
59
{
60
	return sysfs_name_compare(left->hash, left->name, left->ns, right);
61 62 63
}

/**
64 65
 *	sysfs_link_sibling - link kernfs_node into sibling rbtree
 *	@kn: kernfs_node of interest
66
 *
67
 *	Link @kn into its sibling rbtree which starts from
68
 *	@kn->parent->dir.children.
69 70 71 72 73 74 75
 *
 *	Locking:
 *	mutex_lock(sysfs_mutex)
 *
 *	RETURNS:
 *	0 on susccess -EEXIST on failure.
 */
76
static int sysfs_link_sibling(struct kernfs_node *kn)
77
{
78
	struct rb_node **node = &kn->parent->dir.children.rb_node;
79 80
	struct rb_node *parent = NULL;

81
	if (sysfs_type(kn) == SYSFS_DIR)
82
		kn->parent->dir.subdirs++;
83 84

	while (*node) {
85
		struct kernfs_node *pos;
86 87
		int result;

88
		pos = rb_to_kn(*node);
89
		parent = *node;
90
		result = sysfs_sd_compare(kn, pos);
91
		if (result < 0)
92
			node = &pos->rb.rb_left;
93
		else if (result > 0)
94
			node = &pos->rb.rb_right;
95 96 97 98
		else
			return -EEXIST;
	}
	/* add new node and rebalance the tree */
99 100
	rb_link_node(&kn->rb, parent, node);
	rb_insert_color(&kn->rb, &kn->parent->dir.children);
101 102 103 104
	return 0;
}

/**
105 106
 *	sysfs_unlink_sibling - unlink kernfs_node from sibling rbtree
 *	@kn: kernfs_node of interest
107
 *
108
 *	Unlink @kn from its sibling rbtree which starts from
109
 *	kn->parent->dir.children.
110 111 112 113
 *
 *	Locking:
 *	mutex_lock(sysfs_mutex)
 */
114
static void sysfs_unlink_sibling(struct kernfs_node *kn)
115
{
116
	if (sysfs_type(kn) == SYSFS_DIR)
117
		kn->parent->dir.subdirs--;
118

119
	rb_erase(&kn->rb, &kn->parent->dir.children);
120 121 122
}

/**
123 124
 *	sysfs_get_active - get an active reference to kernfs_node
 *	@kn: kernfs_node to get an active reference to
125
 *
126
 *	Get an active reference of @kn.  This function is noop if @kn
127 128 129
 *	is NULL.
 *
 *	RETURNS:
130
 *	Pointer to @kn on success, NULL on failure.
131
 */
132
struct kernfs_node *sysfs_get_active(struct kernfs_node *kn)
133
{
134
	if (unlikely(!kn))
135 136
		return NULL;

137
	if (!atomic_inc_unless_negative(&kn->active))
138 139
		return NULL;

140
	if (kn->flags & SYSFS_FLAG_LOCKDEP)
141 142
		rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
	return kn;
143 144 145
}

/**
146 147
 *	sysfs_put_active - put an active reference to kernfs_node
 *	@kn: kernfs_node to put an active reference to
148
 *
149
 *	Put an active reference to @kn.  This function is noop if @kn
150 151
 *	is NULL.
 */
152
void sysfs_put_active(struct kernfs_node *kn)
153 154 155
{
	int v;

156
	if (unlikely(!kn))
157 158
		return;

159
	if (kn->flags & SYSFS_FLAG_LOCKDEP)
160
		rwsem_release(&kn->dep_map, 1, _RET_IP_);
161
	v = atomic_dec_return(&kn->active);
162 163 164
	if (likely(v != SD_DEACTIVATED_BIAS))
		return;

165 166 167
	/*
	 * atomic_dec_return() is a mb(), we'll always see the updated
	 * kn->u.completion.
168
	 */
169
	complete(kn->u.completion);
170 171 172
}

/**
173 174
 *	sysfs_deactivate - deactivate kernfs_node
 *	@kn: kernfs_node to deactivate
175 176 177
 *
 *	Deny new active references and drain existing ones.
 */
178
static void sysfs_deactivate(struct kernfs_node *kn)
179 180 181 182
{
	DECLARE_COMPLETION_ONSTACK(wait);
	int v;

183
	BUG_ON(!(kn->flags & SYSFS_FLAG_REMOVED));
184

185
	if (!(sysfs_type(kn) & SYSFS_ACTIVE_REF))
186 187
		return;

188
	kn->u.completion = (void *)&wait;
189

190
	rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
191
	/* atomic_add_return() is a mb(), put_active() will always see
192
	 * the updated kn->u.completion.
193
	 */
194
	v = atomic_add_return(SD_DEACTIVATED_BIAS, &kn->active);
195 196

	if (v != SD_DEACTIVATED_BIAS) {
197
		lock_contended(&kn->dep_map, _RET_IP_);
198 199 200
		wait_for_completion(&wait);
	}

201 202
	lock_acquired(&kn->dep_map, _RET_IP_);
	rwsem_release(&kn->dep_map, 1, _RET_IP_);
203 204 205
}

/**
206 207
 * kernfs_get - get a reference count on a kernfs_node
 * @kn: the target kernfs_node
208
 */
209
void kernfs_get(struct kernfs_node *kn)
210
{
211
	if (kn) {
212 213
		WARN_ON(!atomic_read(&kn->count));
		atomic_inc(&kn->count);
214 215 216 217 218
	}
}
EXPORT_SYMBOL_GPL(kernfs_get);

/**
219 220
 * kernfs_put - put a reference count on a kernfs_node
 * @kn: the target kernfs_node
221
 *
222
 * Put a reference count of @kn and destroy it if it reached zero.
223
 */
224
void kernfs_put(struct kernfs_node *kn)
225
{
226
	struct kernfs_node *parent;
227
	struct kernfs_root *root;
228

229
	if (!kn || !atomic_dec_and_test(&kn->count))
230
		return;
231
	root = kernfs_root(kn);
232 233
 repeat:
	/* Moving/renaming is always done while holding reference.
234
	 * kn->parent won't change beneath us.
235
	 */
236
	parent = kn->parent;
237

238
	WARN(!(kn->flags & SYSFS_FLAG_REMOVED),
239
		"sysfs: free using entry: %s/%s\n",
240
		parent ? parent->name : "", kn->name);
241 242

	if (sysfs_type(kn) == SYSFS_KOBJ_LINK)
243
		kernfs_put(kn->symlink.target_kn);
244
	if (sysfs_type(kn) & SYSFS_COPY_NAME)
245 246 247 248 249 250
		kfree(kn->name);
	if (kn->iattr) {
		if (kn->iattr->ia_secdata)
			security_release_secctx(kn->iattr->ia_secdata,
						kn->iattr->ia_secdata_len);
		simple_xattrs_free(&kn->iattr->xattrs);
251
	}
252 253
	kfree(kn->iattr);
	ida_simple_remove(&root->ino_ida, kn->ino);
254
	kmem_cache_free(sysfs_dir_cachep, kn);
255

256 257
	kn = parent;
	if (kn) {
258
		if (atomic_dec_and_test(&kn->count))
259 260
			goto repeat;
	} else {
261
		/* just released the root kn, free @root too */
262
		ida_destroy(&root->ino_ida);
263 264
		kfree(root);
	}
265 266 267 268 269
}
EXPORT_SYMBOL_GPL(kernfs_put);

static int sysfs_dentry_delete(const struct dentry *dentry)
{
270
	struct kernfs_node *kn = dentry->d_fsdata;
271
	return !(kn && !(kn->flags & SYSFS_FLAG_REMOVED));
272 273 274 275
}

static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
{
276
	struct kernfs_node *kn;
277 278 279 280

	if (flags & LOOKUP_RCU)
		return -ECHILD;

281
	kn = dentry->d_fsdata;
282 283 284
	mutex_lock(&sysfs_mutex);

	/* The sysfs dirent has been deleted */
285
	if (kn->flags & SYSFS_FLAG_REMOVED)
286 287 288
		goto out_bad;

	/* The sysfs dirent has been moved? */
289
	if (dentry->d_parent->d_fsdata != kn->parent)
290 291 292
		goto out_bad;

	/* The sysfs dirent has been renamed */
293
	if (strcmp(dentry->d_name.name, kn->name) != 0)
294 295 296
		goto out_bad;

	/* The sysfs dirent has been moved to a different namespace */
297 298
	if (kn->parent && kernfs_ns_enabled(kn->parent) &&
	    sysfs_info(dentry->d_sb)->ns != kn->ns)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		goto out_bad;

	mutex_unlock(&sysfs_mutex);
out_valid:
	return 1;
out_bad:
	/* Remove the dentry from the dcache hashes.
	 * If this is a deleted dentry we use d_drop instead of d_delete
	 * so sysfs doesn't need to cope with negative dentries.
	 *
	 * If this is a dentry that has simply been renamed we
	 * use d_drop to remove it from the dcache lookup on its
	 * old parent.  If this dentry persists later when a lookup
	 * is performed at its new name the dentry will be readded
	 * to the dcache hashes.
	 */
	mutex_unlock(&sysfs_mutex);

	/* If we have submounts we must allow the vfs caches
	 * to lie about the state of the filesystem to prevent
	 * leaks and other nasty things.
	 */
	if (check_submounts_and_drop(dentry) != 0)
		goto out_valid;

	return 0;
}

static void sysfs_dentry_release(struct dentry *dentry)
{
	kernfs_put(dentry->d_fsdata);
}

const struct dentry_operations sysfs_dentry_ops = {
	.d_revalidate	= sysfs_dentry_revalidate,
	.d_delete	= sysfs_dentry_delete,
	.d_release	= sysfs_dentry_release,
};

338 339
struct kernfs_node *sysfs_new_dirent(struct kernfs_root *root,
				     const char *name, umode_t mode, int type)
340 341
{
	char *dup_name = NULL;
342
	struct kernfs_node *kn;
343
	int ret;
344 345 346 347 348 349 350

	if (type & SYSFS_COPY_NAME) {
		name = dup_name = kstrdup(name, GFP_KERNEL);
		if (!name)
			return NULL;
	}

351 352
	kn = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
	if (!kn)
353 354
		goto err_out1;

355 356
	ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
	if (ret < 0)
357
		goto err_out2;
358
	kn->ino = ret;
359

360 361
	atomic_set(&kn->count, 1);
	atomic_set(&kn->active, 0);
362

363 364 365
	kn->name = name;
	kn->mode = mode;
	kn->flags = type | SYSFS_FLAG_REMOVED;
366

367
	return kn;
368 369

 err_out2:
370
	kmem_cache_free(sysfs_dir_cachep, kn);
371 372 373 374 375 376
 err_out1:
	kfree(dup_name);
	return NULL;
}

/**
377
 *	sysfs_addrm_start - prepare for kernfs_node add/remove
378 379 380
 *	@acxt: pointer to sysfs_addrm_cxt to be used
 *
 *	This function is called when the caller is about to add or remove
381 382
 *	kernfs_node.  This function acquires sysfs_mutex.  @acxt is used to
 *	keep and pass context to other addrm functions.
383 384 385 386 387 388 389 390 391 392 393 394 395 396
 *
 *	LOCKING:
 *	Kernel thread context (may sleep).  sysfs_mutex is locked on
 *	return.
 */
void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
	__acquires(sysfs_mutex)
{
	memset(acxt, 0, sizeof(*acxt));

	mutex_lock(&sysfs_mutex);
}

/**
397
 *	sysfs_add_one - add kernfs_node to parent without warning
398
 *	@acxt: addrm context to use
399 400
 *	@kn: kernfs_node to be added
 *	@parent: the parent kernfs_node to add @kn to
401
 *
402 403 404
 *	Get @parent and set @kn->parent to it and increment nlink of the
 *	parent inode if @kn is a directory and link into the children list
 *	of the parent.
405 406 407 408 409 410 411 412 413 414 415 416
 *
 *	This function should be called between calls to
 *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
 *	passed the same @acxt as passed to sysfs_addrm_start().
 *
 *	LOCKING:
 *	Determined by sysfs_addrm_start().
 *
 *	RETURNS:
 *	0 on success, -EEXIST if entry with the given name already
 *	exists.
 */
417 418
int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct kernfs_node *kn,
		  struct kernfs_node *parent)
419
{
420
	bool has_ns = kernfs_ns_enabled(parent);
421 422 423
	struct sysfs_inode_attrs *ps_iattr;
	int ret;

424
	if (has_ns != (bool)kn->ns) {
425
		WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
426
		     has_ns ? "required" : "invalid", parent->name, kn->name);
427 428 429
		return -EINVAL;
	}

430
	if (sysfs_type(parent) != SYSFS_DIR)
431 432
		return -EINVAL;

433 434
	kn->hash = sysfs_name_hash(kn->name, kn->ns);
	kn->parent = parent;
435
	kernfs_get(parent);
436

437
	ret = sysfs_link_sibling(kn);
438 439 440 441
	if (ret)
		return ret;

	/* Update timestamps on the parent */
442
	ps_iattr = parent->iattr;
443 444 445 446 447 448
	if (ps_iattr) {
		struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
		ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
	}

	/* Mark the entry added into directory tree */
449
	kn->flags &= ~SYSFS_FLAG_REMOVED;
450 451 452 453 454

	return 0;
}

/**
455
 *	sysfs_remove_one - remove kernfs_node from parent
456
 *	@acxt: addrm context to use
457
 *	@kn: kernfs_node to be removed
458
 *
459 460
 *	Mark @kn removed and drop nlink of parent inode if @kn is a
 *	directory.  @kn is unlinked from the children list.
461 462 463 464 465 466 467 468 469
 *
 *	This function should be called between calls to
 *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
 *	passed the same @acxt as passed to sysfs_addrm_start().
 *
 *	LOCKING:
 *	Determined by sysfs_addrm_start().
 */
static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
470
			     struct kernfs_node *kn)
471 472 473 474 475 476 477
{
	struct sysfs_inode_attrs *ps_iattr;

	/*
	 * Removal can be called multiple times on the same node.  Only the
	 * first invocation is effective and puts the base ref.
	 */
478
	if (kn->flags & SYSFS_FLAG_REMOVED)
479 480
		return;

481
	if (kn->parent) {
482
		sysfs_unlink_sibling(kn);
483

484
		/* Update timestamps on the parent */
485
		ps_iattr = kn->parent->iattr;
486 487 488 489
		if (ps_iattr) {
			ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
			ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
		}
490 491
	}

492
	kn->flags |= SYSFS_FLAG_REMOVED;
493 494
	kn->u.removed_list = acxt->removed;
	acxt->removed = kn;
495 496 497
}

/**
498
 *	sysfs_addrm_finish - finish up kernfs_node add/remove
499 500
 *	@acxt: addrm context to finish up
 *
501 502
 *	Finish up kernfs_node add/remove.  Resources acquired by
 *	sysfs_addrm_start() are released and removed kernfs_nodes are
503 504 505 506 507 508 509 510 511 512 513
 *	cleaned up.
 *
 *	LOCKING:
 *	sysfs_mutex is released.
 */
void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
	__releases(sysfs_mutex)
{
	/* release resources acquired by sysfs_addrm_start() */
	mutex_unlock(&sysfs_mutex);

514
	/* kill removed kernfs_nodes */
515
	while (acxt->removed) {
516
		struct kernfs_node *kn = acxt->removed;
517

518
		acxt->removed = kn->u.removed_list;
519

520 521 522
		sysfs_deactivate(kn);
		sysfs_unmap_bin_file(kn);
		kernfs_put(kn);
523 524 525 526
	}
}

/**
527 528
 * kernfs_find_ns - find kernfs_node with the given name
 * @parent: kernfs_node to search under
529 530 531
 * @name: name to look for
 * @ns: the namespace tag to use
 *
532 533
 * Look for kernfs_node with name @name under @parent.  Returns pointer to
 * the found kernfs_node on success, %NULL on failure.
534
 */
535 536 537
static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
					  const unsigned char *name,
					  const void *ns)
538
{
539
	struct rb_node *node = parent->dir.children.rb_node;
540
	bool has_ns = kernfs_ns_enabled(parent);
541 542 543 544 545 546
	unsigned int hash;

	lockdep_assert_held(&sysfs_mutex);

	if (has_ns != (bool)ns) {
		WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
547
		     has_ns ? "required" : "invalid", parent->name, name);
548 549 550 551 552
		return NULL;
	}

	hash = sysfs_name_hash(name, ns);
	while (node) {
553
		struct kernfs_node *kn;
554 555
		int result;

556 557
		kn = rb_to_kn(node);
		result = sysfs_name_compare(hash, name, ns, kn);
558 559 560 561 562
		if (result < 0)
			node = node->rb_left;
		else if (result > 0)
			node = node->rb_right;
		else
563
			return kn;
564 565 566 567 568
	}
	return NULL;
}

/**
569 570
 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
 * @parent: kernfs_node to search under
571 572 573
 * @name: name to look for
 * @ns: the namespace tag to use
 *
574
 * Look for kernfs_node with name @name under @parent and get a reference
575
 * if found.  This function may sleep and returns pointer to the found
576
 * kernfs_node on success, %NULL on failure.
577
 */
578 579
struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
					   const char *name, const void *ns)
580
{
581
	struct kernfs_node *kn;
582 583

	mutex_lock(&sysfs_mutex);
584 585
	kn = kernfs_find_ns(parent, name, ns);
	kernfs_get(kn);
586 587
	mutex_unlock(&sysfs_mutex);

588
	return kn;
589 590 591
}
EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);

592 593 594 595 596 597 598 599 600 601
/**
 * kernfs_create_root - create a new kernfs hierarchy
 * @priv: opaque data associated with the new directory
 *
 * Returns the root of the new hierarchy on success, ERR_PTR() value on
 * failure.
 */
struct kernfs_root *kernfs_create_root(void *priv)
{
	struct kernfs_root *root;
602
	struct kernfs_node *kn;
603 604 605 606 607

	root = kzalloc(sizeof(*root), GFP_KERNEL);
	if (!root)
		return ERR_PTR(-ENOMEM);

608 609
	ida_init(&root->ino_ida);

610 611
	kn = sysfs_new_dirent(root, "", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR);
	if (!kn) {
612
		ida_destroy(&root->ino_ida);
613 614 615 616
		kfree(root);
		return ERR_PTR(-ENOMEM);
	}

617
	kn->flags &= ~SYSFS_FLAG_REMOVED;
618
	kn->priv = priv;
619
	kn->dir.root = root;
620

621
	root->kn = kn;
622 623 624 625 626 627 628 629 630 631 632 633 634

	return root;
}

/**
 * kernfs_destroy_root - destroy a kernfs hierarchy
 * @root: root of the hierarchy to destroy
 *
 * Destroy the hierarchy anchored at @root by removing all existing
 * directories and destroying @root.
 */
void kernfs_destroy_root(struct kernfs_root *root)
{
635
	kernfs_remove(root->kn);	/* will also free @root */
636 637
}

638 639 640 641 642 643 644 645 646
/**
 * kernfs_create_dir_ns - create a directory
 * @parent: parent in which to create a new directory
 * @name: name of the new directory
 * @priv: opaque data associated with the new directory
 * @ns: optional namespace tag of the directory
 *
 * Returns the created node on success, ERR_PTR() value on failure.
 */
647 648 649
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
					 const char *name, void *priv,
					 const void *ns)
650 651 652
{
	umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
	struct sysfs_addrm_cxt acxt;
653
	struct kernfs_node *kn;
654 655 656
	int rc;

	/* allocate */
657 658
	kn = sysfs_new_dirent(kernfs_root(parent), name, mode, SYSFS_DIR);
	if (!kn)
659 660
		return ERR_PTR(-ENOMEM);

661 662
	kn->dir.root = parent->dir.root;
	kn->ns = ns;
663
	kn->priv = priv;
664 665 666

	/* link in */
	sysfs_addrm_start(&acxt);
667
	rc = sysfs_add_one(&acxt, kn, parent);
668 669 670
	sysfs_addrm_finish(&acxt);

	if (!rc)
671
		return kn;
672

673
	kernfs_put(kn);
674 675 676 677 678 679 680
	return ERR_PTR(rc);
}

static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
				   unsigned int flags)
{
	struct dentry *ret = NULL;
681 682
	struct kernfs_node *parent = dentry->d_parent->d_fsdata;
	struct kernfs_node *kn;
683 684 685 686 687
	struct inode *inode;
	const void *ns = NULL;

	mutex_lock(&sysfs_mutex);

688
	if (kernfs_ns_enabled(parent))
689 690
		ns = sysfs_info(dir->i_sb)->ns;

691
	kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
692 693

	/* no such entry */
694
	if (!kn) {
695 696 697
		ret = ERR_PTR(-ENOENT);
		goto out_unlock;
	}
698 699
	kernfs_get(kn);
	dentry->d_fsdata = kn;
700 701

	/* attach dentry and inode */
702
	inode = sysfs_get_inode(dir->i_sb, kn);
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	if (!inode) {
		ret = ERR_PTR(-ENOMEM);
		goto out_unlock;
	}

	/* instantiate and hash dentry */
	ret = d_materialise_unique(dentry, inode);
 out_unlock:
	mutex_unlock(&sysfs_mutex);
	return ret;
}

const struct inode_operations sysfs_dir_inode_operations = {
	.lookup		= sysfs_lookup,
	.permission	= sysfs_permission,
	.setattr	= sysfs_setattr,
	.getattr	= sysfs_getattr,
	.setxattr	= sysfs_setxattr,
721 722 723
	.removexattr	= sysfs_removexattr,
	.getxattr	= sysfs_getxattr,
	.listxattr	= sysfs_listxattr,
724 725
};

726
static struct kernfs_node *sysfs_leftmost_descendant(struct kernfs_node *pos)
727
{
728
	struct kernfs_node *last;
729 730 731 732 733 734 735 736 737

	while (true) {
		struct rb_node *rbn;

		last = pos;

		if (sysfs_type(pos) != SYSFS_DIR)
			break;

738
		rbn = rb_first(&pos->dir.children);
739 740 741
		if (!rbn)
			break;

742
		pos = rb_to_kn(rbn);
743 744 745 746 747 748 749 750
	}

	return last;
}

/**
 * sysfs_next_descendant_post - find the next descendant for post-order walk
 * @pos: the current position (%NULL to initiate traversal)
751
 * @root: kernfs_node whose descendants to walk
752 753 754 755 756
 *
 * Find the next descendant to visit for post-order traversal of @root's
 * descendants.  @root is included in the iteration and the last node to be
 * visited.
 */
757 758
static struct kernfs_node *sysfs_next_descendant_post(struct kernfs_node *pos,
						      struct kernfs_node *root)
759 760 761 762 763 764 765 766 767 768 769 770 771 772
{
	struct rb_node *rbn;

	lockdep_assert_held(&sysfs_mutex);

	/* if first iteration, visit leftmost descendant which may be root */
	if (!pos)
		return sysfs_leftmost_descendant(root);

	/* if we visited @root, we're done */
	if (pos == root)
		return NULL;

	/* if there's an unvisited sibling, visit its leftmost descendant */
773
	rbn = rb_next(&pos->rb);
774
	if (rbn)
775
		return sysfs_leftmost_descendant(rb_to_kn(rbn));
776 777

	/* no sibling left, visit parent */
778
	return pos->parent;
779 780 781
}

static void __kernfs_remove(struct sysfs_addrm_cxt *acxt,
782
			    struct kernfs_node *kn)
783
{
784
	struct kernfs_node *pos, *next;
785

786
	if (!kn)
787 788
		return;

789
	pr_debug("sysfs %s: removing\n", kn->name);
790 791 792 793

	next = NULL;
	do {
		pos = next;
794
		next = sysfs_next_descendant_post(pos, kn);
795 796 797 798 799 800
		if (pos)
			sysfs_remove_one(acxt, pos);
	} while (next);
}

/**
801 802
 * kernfs_remove - remove a kernfs_node recursively
 * @kn: the kernfs_node to remove
803
 *
804
 * Remove @kn along with all its subdirectories and files.
805
 */
806
void kernfs_remove(struct kernfs_node *kn)
807 808 809 810
{
	struct sysfs_addrm_cxt acxt;

	sysfs_addrm_start(&acxt);
811
	__kernfs_remove(&acxt, kn);
812 813 814 815
	sysfs_addrm_finish(&acxt);
}

/**
816 817 818 819
 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
 * @parent: parent of the target
 * @name: name of the kernfs_node to remove
 * @ns: namespace tag of the kernfs_node to remove
820
 *
821 822
 * Look for the kernfs_node with @name and @ns under @parent and remove it.
 * Returns 0 on success, -ENOENT if such entry doesn't exist.
823
 */
824
int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
825 826 827
			     const void *ns)
{
	struct sysfs_addrm_cxt acxt;
828
	struct kernfs_node *kn;
829

830
	if (!parent) {
831 832 833 834 835 836 837
		WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
			name);
		return -ENOENT;
	}

	sysfs_addrm_start(&acxt);

838 839 840
	kn = kernfs_find_ns(parent, name, ns);
	if (kn)
		__kernfs_remove(&acxt, kn);
841 842 843

	sysfs_addrm_finish(&acxt);

844
	if (kn)
845 846 847 848 849 850 851
		return 0;
	else
		return -ENOENT;
}

/**
 * kernfs_rename_ns - move and rename a kernfs_node
852
 * @kn: target node
853 854 855 856
 * @new_parent: new parent to put @sd under
 * @new_name: new name
 * @new_ns: new namespace tag
 */
857
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
858 859 860 861 862 863 864
		     const char *new_name, const void *new_ns)
{
	int error;

	mutex_lock(&sysfs_mutex);

	error = 0;
865 866
	if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
	    (strcmp(kn->name, new_name) == 0))
867 868 869 870 871 872
		goto out;	/* nothing to rename */

	error = -EEXIST;
	if (kernfs_find_ns(new_parent, new_name, new_ns))
		goto out;

873
	/* rename kernfs_node */
874
	if (strcmp(kn->name, new_name) != 0) {
875 876 877 878 879
		error = -ENOMEM;
		new_name = kstrdup(new_name, GFP_KERNEL);
		if (!new_name)
			goto out;

880 881
		kfree(kn->name);
		kn->name = new_name;
882 883 884 885 886
	}

	/*
	 * Move to the appropriate place in the appropriate directories rbtree.
	 */
887
	sysfs_unlink_sibling(kn);
888
	kernfs_get(new_parent);
889 890 891 892
	kernfs_put(kn->parent);
	kn->ns = new_ns;
	kn->hash = sysfs_name_hash(kn->name, kn->ns);
	kn->parent = new_parent;
893
	sysfs_link_sibling(kn);
894 895 896 897 898 899 900 901

	error = 0;
 out:
	mutex_unlock(&sysfs_mutex);
	return error;
}

/* Relationship between s_mode and the DT_xxx types */
902
static inline unsigned char dt_type(struct kernfs_node *kn)
903
{
904
	return (kn->mode >> 12) & 15;
905 906 907 908 909 910 911 912
}

static int sysfs_dir_release(struct inode *inode, struct file *filp)
{
	kernfs_put(filp->private_data);
	return 0;
}

913 914
static struct kernfs_node *sysfs_dir_pos(const void *ns,
	struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
915 916
{
	if (pos) {
917 918
		int valid = !(pos->flags & SYSFS_FLAG_REMOVED) &&
			pos->parent == parent && hash == pos->hash;
919 920 921 922 923
		kernfs_put(pos);
		if (!valid)
			pos = NULL;
	}
	if (!pos && (hash > 1) && (hash < INT_MAX)) {
924
		struct rb_node *node = parent->dir.children.rb_node;
925
		while (node) {
926
			pos = rb_to_kn(node);
927

928
			if (hash < pos->hash)
929
				node = node->rb_left;
930
			else if (hash > pos->hash)
931 932 933 934 935 936
				node = node->rb_right;
			else
				break;
		}
	}
	/* Skip over entries in the wrong namespace */
937 938
	while (pos && pos->ns != ns) {
		struct rb_node *node = rb_next(&pos->rb);
939 940 941
		if (!node)
			pos = NULL;
		else
942
			pos = rb_to_kn(node);
943 944 945 946
	}
	return pos;
}

947 948
static struct kernfs_node *sysfs_dir_next_pos(const void *ns,
	struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
949
{
950
	pos = sysfs_dir_pos(ns, parent, ino, pos);
951 952
	if (pos)
		do {
953
			struct rb_node *node = rb_next(&pos->rb);
954 955 956
			if (!node)
				pos = NULL;
			else
957
				pos = rb_to_kn(node);
958
		} while (pos && pos->ns != ns);
959 960 961 962 963 964
	return pos;
}

static int sysfs_readdir(struct file *file, struct dir_context *ctx)
{
	struct dentry *dentry = file->f_path.dentry;
965 966
	struct kernfs_node *parent = dentry->d_fsdata;
	struct kernfs_node *pos = file->private_data;
967 968 969 970 971 972
	const void *ns = NULL;

	if (!dir_emit_dots(file, ctx))
		return 0;
	mutex_lock(&sysfs_mutex);

973
	if (kernfs_ns_enabled(parent))
974 975
		ns = sysfs_info(dentry->d_sb)->ns;

976
	for (pos = sysfs_dir_pos(ns, parent, ctx->pos, pos);
977
	     pos;
978
	     pos = sysfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
979
		const char *name = pos->name;
980 981
		unsigned int type = dt_type(pos);
		int len = strlen(name);
982
		ino_t ino = pos->ino;
983

984
		ctx->pos = pos->hash;
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		file->private_data = pos;
		kernfs_get(pos);

		mutex_unlock(&sysfs_mutex);
		if (!dir_emit(ctx, name, len, ino, type))
			return 0;
		mutex_lock(&sysfs_mutex);
	}
	mutex_unlock(&sysfs_mutex);
	file->private_data = NULL;
	ctx->pos = INT_MAX;
	return 0;
}

static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
{
	struct inode *inode = file_inode(file);
	loff_t ret;

	mutex_lock(&inode->i_mutex);
	ret = generic_file_llseek(file, offset, whence);
	mutex_unlock(&inode->i_mutex);

	return ret;
}

const struct file_operations sysfs_dir_operations = {
	.read		= generic_read_dir,
	.iterate	= sysfs_readdir,
	.release	= sysfs_dir_release,
	.llseek		= sysfs_dir_llseek,
};