dir.c 23.9 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

#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"

20
DEFINE_MUTEX(kernfs_mutex);
21

22
#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
23 24

/**
25
 *	kernfs_name_hash
26 27 28 29 30
 *	@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 )
 */
31
static unsigned int kernfs_name_hash(const char *name, const void *ns)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	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;
}

47 48
static int kernfs_name_compare(unsigned int hash, const char *name,
			       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 kernfs_sd_compare(const struct kernfs_node *left,
			     const struct kernfs_node *right)
59
{
60
	return kernfs_name_compare(left->hash, left->name, left->ns, right);
61 62 63
}

/**
64
 *	kernfs_link_sibling - link kernfs_node into sibling rbtree
65
 *	@kn: kernfs_node of interest
66
 *
67
 *	Link @kn into its sibling rbtree which starts from
68
 *	@kn->parent->dir.children.
69 70
 *
 *	Locking:
71
 *	mutex_lock(kernfs_mutex)
72 73 74 75
 *
 *	RETURNS:
 *	0 on susccess -EEXIST on failure.
 */
76
static int kernfs_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;

T
Tejun Heo 已提交
81
	if (kernfs_type(kn) == KERNFS_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 = kernfs_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
 *	kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106
 *	@kn: kernfs_node of interest
107
 *
108
 *	Unlink @kn from its sibling rbtree which starts from
109
 *	kn->parent->dir.children.
110 111
 *
 *	Locking:
112
 *	mutex_lock(kernfs_mutex)
113
 */
114
static void kernfs_unlink_sibling(struct kernfs_node *kn)
115
{
T
Tejun Heo 已提交
116
	if (kernfs_type(kn) == KERNFS_DIR)
117
		kn->parent->dir.subdirs--;
118

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

/**
123
 *	kernfs_get_active - get an active reference to kernfs_node
124
 *	@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 *kernfs_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;

T
Tejun Heo 已提交
140
	if (kn->flags & KERNFS_LOCKDEP)
141 142
		rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
	return kn;
143 144 145
}

/**
146
 *	kernfs_put_active - put an active reference to kernfs_node
147
 *	@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 kernfs_put_active(struct kernfs_node *kn)
153 154 155
{
	int v;

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

T
Tejun Heo 已提交
159
	if (kn->flags & KERNFS_LOCKDEP)
160
		rwsem_release(&kn->dep_map, 1, _RET_IP_);
161
	v = atomic_dec_return(&kn->active);
T
Tejun Heo 已提交
162
	if (likely(v != KN_DEACTIVATED_BIAS))
163 164
		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
 *	kernfs_deactivate - deactivate kernfs_node
174
 *	@kn: kernfs_node to deactivate
175 176 177
 *
 *	Deny new active references and drain existing ones.
 */
178
static void kernfs_deactivate(struct kernfs_node *kn)
179 180 181 182
{
	DECLARE_COMPLETION_ONSTACK(wait);
	int v;

T
Tejun Heo 已提交
183
	BUG_ON(!(kn->flags & KERNFS_REMOVED));
184

T
Tejun Heo 已提交
185
	if (!(kernfs_type(kn) & KERNFS_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
	 */
T
Tejun Heo 已提交
194
	v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
195

T
Tejun Heo 已提交
196
	if (v != KN_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 239
	WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
	     parent ? parent->name : "", kn->name);
240

T
Tejun Heo 已提交
241
	if (kernfs_type(kn) == KERNFS_LINK)
242
		kernfs_put(kn->symlink.target_kn);
243
	if (!(kn->flags & KERNFS_STATIC_NAME))
244 245 246 247 248 249
		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);
250
	}
251 252
	kfree(kn->iattr);
	ida_simple_remove(&root->ino_ida, kn->ino);
253
	kmem_cache_free(kernfs_node_cache, kn);
254

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

267
static int kernfs_dop_delete(const struct dentry *dentry)
268
{
269
	struct kernfs_node *kn = dentry->d_fsdata;
T
Tejun Heo 已提交
270
	return !(kn && !(kn->flags & KERNFS_REMOVED));
271 272
}

273
static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
274
{
275
	struct kernfs_node *kn;
276 277 278 279

	if (flags & LOOKUP_RCU)
		return -ECHILD;

280
	kn = dentry->d_fsdata;
281
	mutex_lock(&kernfs_mutex);
282

283
	/* The kernfs node has been deleted */
T
Tejun Heo 已提交
284
	if (kn->flags & KERNFS_REMOVED)
285 286
		goto out_bad;

287
	/* The kernfs node has been moved? */
288
	if (dentry->d_parent->d_fsdata != kn->parent)
289 290
		goto out_bad;

291
	/* The kernfs node has been renamed */
292
	if (strcmp(dentry->d_name.name, kn->name) != 0)
293 294
		goto out_bad;

295
	/* The kernfs node has been moved to a different namespace */
296
	if (kn->parent && kernfs_ns_enabled(kn->parent) &&
297
	    kernfs_info(dentry->d_sb)->ns != kn->ns)
298 299
		goto out_bad;

300
	mutex_unlock(&kernfs_mutex);
301 302 303
out_valid:
	return 1;
out_bad:
304 305
	/*
	 * Remove the dentry from the dcache hashes.
306
	 * If this is a deleted dentry we use d_drop instead of d_delete
307
	 * so kernfs doesn't need to cope with negative dentries.
308 309 310 311 312 313 314
	 *
	 * 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.
	 */
315
	mutex_unlock(&kernfs_mutex);
316 317 318 319 320 321 322 323 324 325 326

	/* 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;
}

327
static void kernfs_dop_release(struct dentry *dentry)
328 329 330 331
{
	kernfs_put(dentry->d_fsdata);
}

332
const struct dentry_operations kernfs_dops = {
333 334 335
	.d_revalidate	= kernfs_dop_revalidate,
	.d_delete	= kernfs_dop_delete,
	.d_release	= kernfs_dop_release,
336 337
};

338
struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
339
				    umode_t mode, unsigned flags)
340 341
{
	char *dup_name = NULL;
342
	struct kernfs_node *kn;
343
	int ret;
344

345
	if (!(flags & KERNFS_STATIC_NAME)) {
346 347 348 349 350
		name = dup_name = kstrdup(name, GFP_KERNEL);
		if (!name)
			return NULL;
	}

351
	kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
352
	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
	kn->name = name;
	kn->mode = mode;
365
	kn->flags = flags | KERNFS_REMOVED;
366

367
	return kn;
368 369

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

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

393
	mutex_lock(&kernfs_mutex);
394 395 396
}

/**
397
 *	kernfs_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
 *
 *	This function should be called between calls to
407 408
 *	kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
 *	the same @acxt as passed to kernfs_addrm_start().
409 410
 *
 *	LOCKING:
411
 *	Determined by kernfs_addrm_start().
412 413 414 415 416
 *
 *	RETURNS:
 *	0 on success, -EEXIST if entry with the given name already
 *	exists.
 */
417
int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
418
		  struct kernfs_node *parent)
419
{
420
	bool has_ns = kernfs_ns_enabled(parent);
421
	struct kernfs_iattrs *ps_iattr;
422 423
	int ret;

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

T
Tejun Heo 已提交
430
	if (kernfs_type(parent) != KERNFS_DIR)
431 432
		return -EINVAL;

433 434 435
	if (parent->flags & KERNFS_REMOVED)
		return -ENOENT;

436
	kn->hash = kernfs_name_hash(kn->name, kn->ns);
437
	kn->parent = parent;
438
	kernfs_get(parent);
439

440
	ret = kernfs_link_sibling(kn);
441 442 443 444
	if (ret)
		return ret;

	/* Update timestamps on the parent */
445
	ps_iattr = parent->iattr;
446 447 448 449 450 451
	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 */
T
Tejun Heo 已提交
452
	kn->flags &= ~KERNFS_REMOVED;
453 454 455 456 457

	return 0;
}

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

	/*
	 * Removal can be called multiple times on the same node.  Only the
	 * first invocation is effective and puts the base ref.
	 */
T
Tejun Heo 已提交
481
	if (kn->flags & KERNFS_REMOVED)
482 483
		return;

484
	if (kn->parent) {
485
		kernfs_unlink_sibling(kn);
486

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

T
Tejun Heo 已提交
495
	kn->flags |= KERNFS_REMOVED;
496 497
	kn->u.removed_list = acxt->removed;
	acxt->removed = kn;
498 499 500
}

/**
501
 *	kernfs_addrm_finish - finish up kernfs_node add/remove
502 503
 *	@acxt: addrm context to finish up
 *
504
 *	Finish up kernfs_node add/remove.  Resources acquired by
505
 *	kernfs_addrm_start() are released and removed kernfs_nodes are
506 507 508
 *	cleaned up.
 *
 *	LOCKING:
509
 *	kernfs_mutex is released.
510
 */
511
void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
512
	__releases(kernfs_mutex)
513
{
514
	/* release resources acquired by kernfs_addrm_start() */
515
	mutex_unlock(&kernfs_mutex);
516

517
	/* kill removed kernfs_nodes */
518
	while (acxt->removed) {
519
		struct kernfs_node *kn = acxt->removed;
520

521
		acxt->removed = kn->u.removed_list;
522

523 524
		kernfs_deactivate(kn);
		kernfs_unmap_bin_file(kn);
525
		kernfs_put(kn);
526 527 528 529
	}
}

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

546
	lockdep_assert_held(&kernfs_mutex);
547 548

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

554
	hash = kernfs_name_hash(name, ns);
555
	while (node) {
556
		struct kernfs_node *kn;
557 558
		int result;

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

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

586
	mutex_lock(&kernfs_mutex);
587 588
	kn = kernfs_find_ns(parent, name, ns);
	kernfs_get(kn);
589
	mutex_unlock(&kernfs_mutex);
590

591
	return kn;
592 593 594
}
EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);

595 596 597 598 599 600 601 602 603 604
/**
 * 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;
605
	struct kernfs_node *kn;
606 607 608 609 610

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

611 612
	ida_init(&root->ino_ida);

613
	kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
614
	if (!kn) {
615
		ida_destroy(&root->ino_ida);
616 617 618 619
		kfree(root);
		return ERR_PTR(-ENOMEM);
	}

T
Tejun Heo 已提交
620
	kn->flags &= ~KERNFS_REMOVED;
621
	kn->priv = priv;
622
	kn->dir.root = root;
623

624
	root->kn = kn;
625 626 627 628 629 630 631 632 633 634 635 636 637

	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)
{
638
	kernfs_remove(root->kn);	/* will also free @root */
639 640
}

641 642 643 644
/**
 * kernfs_create_dir_ns - create a directory
 * @parent: parent in which to create a new directory
 * @name: name of the new directory
645
 * @mode: mode of the new directory
646 647 648 649 650
 * @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.
 */
651
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
652 653
					 const char *name, umode_t mode,
					 void *priv, const void *ns)
654
{
655
	struct kernfs_addrm_cxt acxt;
656
	struct kernfs_node *kn;
657 658 659
	int rc;

	/* allocate */
660 661
	kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
			     KERNFS_DIR);
662
	if (!kn)
663 664
		return ERR_PTR(-ENOMEM);

665 666
	kn->dir.root = parent->dir.root;
	kn->ns = ns;
667
	kn->priv = priv;
668 669

	/* link in */
670 671 672
	kernfs_addrm_start(&acxt);
	rc = kernfs_add_one(&acxt, kn, parent);
	kernfs_addrm_finish(&acxt);
673 674

	if (!rc)
675
		return kn;
676

677
	kernfs_put(kn);
678 679 680
	return ERR_PTR(rc);
}

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

691
	mutex_lock(&kernfs_mutex);
692

693
	if (kernfs_ns_enabled(parent))
694
		ns = kernfs_info(dir->i_sb)->ns;
695

696
	kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
697 698

	/* no such entry */
699
	if (!kn) {
700 701 702
		ret = ERR_PTR(-ENOENT);
		goto out_unlock;
	}
703 704
	kernfs_get(kn);
	dentry->d_fsdata = kn;
705 706

	/* attach dentry and inode */
707
	inode = kernfs_get_inode(dir->i_sb, kn);
708 709 710 711 712 713 714 715
	if (!inode) {
		ret = ERR_PTR(-ENOMEM);
		goto out_unlock;
	}

	/* instantiate and hash dentry */
	ret = d_materialise_unique(dentry, inode);
 out_unlock:
716
	mutex_unlock(&kernfs_mutex);
717 718 719
	return ret;
}

720
const struct inode_operations kernfs_dir_iops = {
721 722 723 724 725 726 727 728
	.lookup		= kernfs_iop_lookup,
	.permission	= kernfs_iop_permission,
	.setattr	= kernfs_iop_setattr,
	.getattr	= kernfs_iop_getattr,
	.setxattr	= kernfs_iop_setxattr,
	.removexattr	= kernfs_iop_removexattr,
	.getxattr	= kernfs_iop_getxattr,
	.listxattr	= kernfs_iop_listxattr,
729 730
};

731
static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
732
{
733
	struct kernfs_node *last;
734 735 736 737 738 739

	while (true) {
		struct rb_node *rbn;

		last = pos;

T
Tejun Heo 已提交
740
		if (kernfs_type(pos) != KERNFS_DIR)
741 742
			break;

743
		rbn = rb_first(&pos->dir.children);
744 745 746
		if (!rbn)
			break;

747
		pos = rb_to_kn(rbn);
748 749 750 751 752 753
	}

	return last;
}

/**
754
 * kernfs_next_descendant_post - find the next descendant for post-order walk
755
 * @pos: the current position (%NULL to initiate traversal)
756
 * @root: kernfs_node whose descendants to walk
757 758 759 760 761
 *
 * 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.
 */
762 763
static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
						       struct kernfs_node *root)
764 765 766
{
	struct rb_node *rbn;

767
	lockdep_assert_held(&kernfs_mutex);
768 769 770

	/* if first iteration, visit leftmost descendant which may be root */
	if (!pos)
771
		return kernfs_leftmost_descendant(root);
772 773 774 775 776 777

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

	/* if there's an unvisited sibling, visit its leftmost descendant */
778
	rbn = rb_next(&pos->rb);
779
	if (rbn)
780
		return kernfs_leftmost_descendant(rb_to_kn(rbn));
781 782

	/* no sibling left, visit parent */
783
	return pos->parent;
784 785
}

786
static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
787
			    struct kernfs_node *kn)
788
{
789
	struct kernfs_node *pos, *next;
790

791
	if (!kn)
792 793
		return;

794
	pr_debug("kernfs %s: removing\n", kn->name);
795 796 797 798

	next = NULL;
	do {
		pos = next;
799
		next = kernfs_next_descendant_post(pos, kn);
800
		if (pos)
801
			kernfs_remove_one(acxt, pos);
802 803 804 805
	} while (next);
}

/**
806 807
 * kernfs_remove - remove a kernfs_node recursively
 * @kn: the kernfs_node to remove
808
 *
809
 * Remove @kn along with all its subdirectories and files.
810
 */
811
void kernfs_remove(struct kernfs_node *kn)
812
{
813
	struct kernfs_addrm_cxt acxt;
814

815
	kernfs_addrm_start(&acxt);
816
	__kernfs_remove(&acxt, kn);
817
	kernfs_addrm_finish(&acxt);
818 819 820
}

/**
821 822 823 824
 * 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
825
 *
826 827
 * 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.
828
 */
829
int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
830 831
			     const void *ns)
{
832
	struct kernfs_addrm_cxt acxt;
833
	struct kernfs_node *kn;
834

835
	if (!parent) {
836
		WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
837 838 839 840
			name);
		return -ENOENT;
	}

841
	kernfs_addrm_start(&acxt);
842

843 844 845
	kn = kernfs_find_ns(parent, name, ns);
	if (kn)
		__kernfs_remove(&acxt, kn);
846

847
	kernfs_addrm_finish(&acxt);
848

849
	if (kn)
850 851 852 853 854 855 856
		return 0;
	else
		return -ENOENT;
}

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

867
	mutex_lock(&kernfs_mutex);
868

869 870 871 872
	error = -ENOENT;
	if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
		goto out;

873
	error = 0;
874 875
	if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
	    (strcmp(kn->name, new_name) == 0))
876 877 878 879 880 881
		goto out;	/* nothing to rename */

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

882
	/* rename kernfs_node */
883
	if (strcmp(kn->name, new_name) != 0) {
884 885 886 887 888
		error = -ENOMEM;
		new_name = kstrdup(new_name, GFP_KERNEL);
		if (!new_name)
			goto out;

889 890 891 892 893
		if (kn->flags & KERNFS_STATIC_NAME)
			kn->flags &= ~KERNFS_STATIC_NAME;
		else
			kfree(kn->name);

894
		kn->name = new_name;
895 896 897 898 899
	}

	/*
	 * Move to the appropriate place in the appropriate directories rbtree.
	 */
900
	kernfs_unlink_sibling(kn);
901
	kernfs_get(new_parent);
902 903
	kernfs_put(kn->parent);
	kn->ns = new_ns;
904
	kn->hash = kernfs_name_hash(kn->name, kn->ns);
905
	kn->parent = new_parent;
906
	kernfs_link_sibling(kn);
907 908 909

	error = 0;
 out:
910
	mutex_unlock(&kernfs_mutex);
911 912 913 914
	return error;
}

/* Relationship between s_mode and the DT_xxx types */
915
static inline unsigned char dt_type(struct kernfs_node *kn)
916
{
917
	return (kn->mode >> 12) & 15;
918 919
}

920
static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
921 922 923 924 925
{
	kernfs_put(filp->private_data);
	return 0;
}

926
static struct kernfs_node *kernfs_dir_pos(const void *ns,
927
	struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
928 929
{
	if (pos) {
T
Tejun Heo 已提交
930
		int valid = !(pos->flags & KERNFS_REMOVED) &&
931
			pos->parent == parent && hash == pos->hash;
932 933 934 935 936
		kernfs_put(pos);
		if (!valid)
			pos = NULL;
	}
	if (!pos && (hash > 1) && (hash < INT_MAX)) {
937
		struct rb_node *node = parent->dir.children.rb_node;
938
		while (node) {
939
			pos = rb_to_kn(node);
940

941
			if (hash < pos->hash)
942
				node = node->rb_left;
943
			else if (hash > pos->hash)
944 945 946 947 948 949
				node = node->rb_right;
			else
				break;
		}
	}
	/* Skip over entries in the wrong namespace */
950 951
	while (pos && pos->ns != ns) {
		struct rb_node *node = rb_next(&pos->rb);
952 953 954
		if (!node)
			pos = NULL;
		else
955
			pos = rb_to_kn(node);
956 957 958 959
	}
	return pos;
}

960
static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
961
	struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
962
{
963
	pos = kernfs_dir_pos(ns, parent, ino, pos);
964 965
	if (pos)
		do {
966
			struct rb_node *node = rb_next(&pos->rb);
967 968 969
			if (!node)
				pos = NULL;
			else
970
				pos = rb_to_kn(node);
971
		} while (pos && pos->ns != ns);
972 973 974
	return pos;
}

975
static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
976 977
{
	struct dentry *dentry = file->f_path.dentry;
978 979
	struct kernfs_node *parent = dentry->d_fsdata;
	struct kernfs_node *pos = file->private_data;
980 981 982 983
	const void *ns = NULL;

	if (!dir_emit_dots(file, ctx))
		return 0;
984
	mutex_lock(&kernfs_mutex);
985

986
	if (kernfs_ns_enabled(parent))
987
		ns = kernfs_info(dentry->d_sb)->ns;
988

989
	for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
990
	     pos;
991
	     pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
992
		const char *name = pos->name;
993 994
		unsigned int type = dt_type(pos);
		int len = strlen(name);
995
		ino_t ino = pos->ino;
996

997
		ctx->pos = pos->hash;
998 999 1000
		file->private_data = pos;
		kernfs_get(pos);

1001
		mutex_unlock(&kernfs_mutex);
1002 1003
		if (!dir_emit(ctx, name, len, ino, type))
			return 0;
1004
		mutex_lock(&kernfs_mutex);
1005
	}
1006
	mutex_unlock(&kernfs_mutex);
1007 1008 1009 1010 1011
	file->private_data = NULL;
	ctx->pos = INT_MAX;
	return 0;
}

1012 1013
static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
				    int whence)
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
{
	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;
}

1025
const struct file_operations kernfs_dir_fops = {
1026
	.read		= generic_read_dir,
1027 1028 1029
	.iterate	= kernfs_fop_readdir,
	.release	= kernfs_dir_fop_release,
	.llseek		= kernfs_dir_fop_llseek,
1030
};