dir.c revision bc755553df9ab33f389c1a0a8bd0b4f4646e80ef
1/*
2 * fs/kernfs/dir.c - kernfs directory implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/idr.h>
14#include <linux/slab.h>
15#include <linux/security.h>
16#include <linux/hash.h>
17
18#include "kernfs-internal.h"
19
20DEFINE_MUTEX(sysfs_mutex);
21
22#define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
23
24/**
25 *	sysfs_name_hash
26 *	@name: Null terminated string to hash
27 *	@ns:   Namespace tag to hash
28 *
29 *	Returns 31 bit hash of ns + name (so it fits in an off_t )
30 */
31static unsigned int sysfs_name_hash(const char *name, const void *ns)
32{
33	unsigned long hash = init_name_hash();
34	unsigned int len = strlen(name);
35	while (len--)
36		hash = partial_name_hash(*name++, hash);
37	hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38	hash &= 0x7fffffffU;
39	/* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40	if (hash < 1)
41		hash += 2;
42	if (hash >= INT_MAX)
43		hash = INT_MAX - 1;
44	return hash;
45}
46
47static int sysfs_name_compare(unsigned int hash, const char *name,
48			      const void *ns, const struct sysfs_dirent *sd)
49{
50	if (hash != sd->s_hash)
51		return hash - sd->s_hash;
52	if (ns != sd->s_ns)
53		return ns - sd->s_ns;
54	return strcmp(name, sd->s_name);
55}
56
57static int sysfs_sd_compare(const struct sysfs_dirent *left,
58			    const struct sysfs_dirent *right)
59{
60	return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns,
61				  right);
62}
63
64/**
65 *	sysfs_link_sibling - link sysfs_dirent into sibling rbtree
66 *	@sd: sysfs_dirent of interest
67 *
68 *	Link @sd into its sibling rbtree which starts from
69 *	sd->s_parent->s_dir.children.
70 *
71 *	Locking:
72 *	mutex_lock(sysfs_mutex)
73 *
74 *	RETURNS:
75 *	0 on susccess -EEXIST on failure.
76 */
77static int sysfs_link_sibling(struct sysfs_dirent *sd)
78{
79	struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
80	struct rb_node *parent = NULL;
81
82	if (sysfs_type(sd) == SYSFS_DIR)
83		sd->s_parent->s_dir.subdirs++;
84
85	while (*node) {
86		struct sysfs_dirent *pos;
87		int result;
88
89		pos = to_sysfs_dirent(*node);
90		parent = *node;
91		result = sysfs_sd_compare(sd, pos);
92		if (result < 0)
93			node = &pos->s_rb.rb_left;
94		else if (result > 0)
95			node = &pos->s_rb.rb_right;
96		else
97			return -EEXIST;
98	}
99	/* add new node and rebalance the tree */
100	rb_link_node(&sd->s_rb, parent, node);
101	rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
102	return 0;
103}
104
105/**
106 *	sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
107 *	@sd: sysfs_dirent of interest
108 *
109 *	Unlink @sd from its sibling rbtree which starts from
110 *	sd->s_parent->s_dir.children.
111 *
112 *	Locking:
113 *	mutex_lock(sysfs_mutex)
114 */
115static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
116{
117	if (sysfs_type(sd) == SYSFS_DIR)
118		sd->s_parent->s_dir.subdirs--;
119
120	rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
121}
122
123/**
124 *	sysfs_get_active - get an active reference to sysfs_dirent
125 *	@sd: sysfs_dirent to get an active reference to
126 *
127 *	Get an active reference of @sd.  This function is noop if @sd
128 *	is NULL.
129 *
130 *	RETURNS:
131 *	Pointer to @sd on success, NULL on failure.
132 */
133struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
134{
135	if (unlikely(!sd))
136		return NULL;
137
138	if (!atomic_inc_unless_negative(&sd->s_active))
139		return NULL;
140
141	if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
142		rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
143	return sd;
144}
145
146/**
147 *	sysfs_put_active - put an active reference to sysfs_dirent
148 *	@sd: sysfs_dirent to put an active reference to
149 *
150 *	Put an active reference to @sd.  This function is noop if @sd
151 *	is NULL.
152 */
153void sysfs_put_active(struct sysfs_dirent *sd)
154{
155	int v;
156
157	if (unlikely(!sd))
158		return;
159
160	if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
161		rwsem_release(&sd->dep_map, 1, _RET_IP_);
162	v = atomic_dec_return(&sd->s_active);
163	if (likely(v != SD_DEACTIVATED_BIAS))
164		return;
165
166	/* atomic_dec_return() is a mb(), we'll always see the updated
167	 * sd->u.completion.
168	 */
169	complete(sd->u.completion);
170}
171
172/**
173 *	sysfs_deactivate - deactivate sysfs_dirent
174 *	@sd: sysfs_dirent to deactivate
175 *
176 *	Deny new active references and drain existing ones.
177 */
178static void sysfs_deactivate(struct sysfs_dirent *sd)
179{
180	DECLARE_COMPLETION_ONSTACK(wait);
181	int v;
182
183	BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
184
185	if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
186		return;
187
188	sd->u.completion = (void *)&wait;
189
190	rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
191	/* atomic_add_return() is a mb(), put_active() will always see
192	 * the updated sd->u.completion.
193	 */
194	v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
195
196	if (v != SD_DEACTIVATED_BIAS) {
197		lock_contended(&sd->dep_map, _RET_IP_);
198		wait_for_completion(&wait);
199	}
200
201	lock_acquired(&sd->dep_map, _RET_IP_);
202	rwsem_release(&sd->dep_map, 1, _RET_IP_);
203}
204
205/**
206 * kernfs_get - get a reference count on a sysfs_dirent
207 * @sd: the target sysfs_dirent
208 */
209void kernfs_get(struct sysfs_dirent *sd)
210{
211	if (sd) {
212		WARN_ON(!atomic_read(&sd->s_count));
213		atomic_inc(&sd->s_count);
214	}
215}
216EXPORT_SYMBOL_GPL(kernfs_get);
217
218/**
219 * kernfs_put - put a reference count on a sysfs_dirent
220 * @sd: the target sysfs_dirent
221 *
222 * Put a reference count of @sd and destroy it if it reached zero.
223 */
224void kernfs_put(struct sysfs_dirent *sd)
225{
226	struct sysfs_dirent *parent_sd;
227	struct kernfs_root *root;
228
229	if (!sd || !atomic_dec_and_test(&sd->s_count))
230		return;
231	root = kernfs_root(sd);
232 repeat:
233	/* Moving/renaming is always done while holding reference.
234	 * sd->s_parent won't change beneath us.
235	 */
236	parent_sd = sd->s_parent;
237
238	WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
239		"sysfs: free using entry: %s/%s\n",
240		parent_sd ? parent_sd->s_name : "", sd->s_name);
241
242	if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
243		kernfs_put(sd->s_symlink.target_sd);
244	if (sysfs_type(sd) & SYSFS_COPY_NAME)
245		kfree(sd->s_name);
246	if (sd->s_iattr && sd->s_iattr->ia_secdata)
247		security_release_secctx(sd->s_iattr->ia_secdata,
248					sd->s_iattr->ia_secdata_len);
249	kfree(sd->s_iattr);
250	ida_simple_remove(&root->ino_ida, sd->s_ino);
251	kmem_cache_free(sysfs_dir_cachep, sd);
252
253	sd = parent_sd;
254	if (sd) {
255		if (atomic_dec_and_test(&sd->s_count))
256			goto repeat;
257	} else {
258		/* just released the root sd, free @root too */
259		ida_destroy(&root->ino_ida);
260		kfree(root);
261	}
262}
263EXPORT_SYMBOL_GPL(kernfs_put);
264
265static int sysfs_dentry_delete(const struct dentry *dentry)
266{
267	struct sysfs_dirent *sd = dentry->d_fsdata;
268	return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
269}
270
271static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
272{
273	struct sysfs_dirent *sd;
274
275	if (flags & LOOKUP_RCU)
276		return -ECHILD;
277
278	sd = dentry->d_fsdata;
279	mutex_lock(&sysfs_mutex);
280
281	/* The sysfs dirent has been deleted */
282	if (sd->s_flags & SYSFS_FLAG_REMOVED)
283		goto out_bad;
284
285	/* The sysfs dirent has been moved? */
286	if (dentry->d_parent->d_fsdata != sd->s_parent)
287		goto out_bad;
288
289	/* The sysfs dirent has been renamed */
290	if (strcmp(dentry->d_name.name, sd->s_name) != 0)
291		goto out_bad;
292
293	/* The sysfs dirent has been moved to a different namespace */
294	if (sd->s_parent && (sd->s_parent->s_flags & SYSFS_FLAG_NS) &&
295	    sysfs_info(dentry->d_sb)->ns != sd->s_ns)
296		goto out_bad;
297
298	mutex_unlock(&sysfs_mutex);
299out_valid:
300	return 1;
301out_bad:
302	/* Remove the dentry from the dcache hashes.
303	 * If this is a deleted dentry we use d_drop instead of d_delete
304	 * so sysfs doesn't need to cope with negative dentries.
305	 *
306	 * If this is a dentry that has simply been renamed we
307	 * use d_drop to remove it from the dcache lookup on its
308	 * old parent.  If this dentry persists later when a lookup
309	 * is performed at its new name the dentry will be readded
310	 * to the dcache hashes.
311	 */
312	mutex_unlock(&sysfs_mutex);
313
314	/* If we have submounts we must allow the vfs caches
315	 * to lie about the state of the filesystem to prevent
316	 * leaks and other nasty things.
317	 */
318	if (check_submounts_and_drop(dentry) != 0)
319		goto out_valid;
320
321	return 0;
322}
323
324static void sysfs_dentry_release(struct dentry *dentry)
325{
326	kernfs_put(dentry->d_fsdata);
327}
328
329const struct dentry_operations sysfs_dentry_ops = {
330	.d_revalidate	= sysfs_dentry_revalidate,
331	.d_delete	= sysfs_dentry_delete,
332	.d_release	= sysfs_dentry_release,
333};
334
335struct sysfs_dirent *sysfs_new_dirent(struct kernfs_root *root,
336				      const char *name, umode_t mode, int type)
337{
338	char *dup_name = NULL;
339	struct sysfs_dirent *sd;
340	int ret;
341
342	if (type & SYSFS_COPY_NAME) {
343		name = dup_name = kstrdup(name, GFP_KERNEL);
344		if (!name)
345			return NULL;
346	}
347
348	sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
349	if (!sd)
350		goto err_out1;
351
352	ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
353	if (ret < 0)
354		goto err_out2;
355	sd->s_ino = ret;
356
357	atomic_set(&sd->s_count, 1);
358	atomic_set(&sd->s_active, 0);
359
360	sd->s_name = name;
361	sd->s_mode = mode;
362	sd->s_flags = type | SYSFS_FLAG_REMOVED;
363
364	return sd;
365
366 err_out2:
367	kmem_cache_free(sysfs_dir_cachep, sd);
368 err_out1:
369	kfree(dup_name);
370	return NULL;
371}
372
373/**
374 *	sysfs_addrm_start - prepare for sysfs_dirent add/remove
375 *	@acxt: pointer to sysfs_addrm_cxt to be used
376 *
377 *	This function is called when the caller is about to add or remove
378 *	sysfs_dirent.  This function acquires sysfs_mutex.  @acxt is used
379 *	to keep and pass context to other addrm functions.
380 *
381 *	LOCKING:
382 *	Kernel thread context (may sleep).  sysfs_mutex is locked on
383 *	return.
384 */
385void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
386	__acquires(sysfs_mutex)
387{
388	memset(acxt, 0, sizeof(*acxt));
389
390	mutex_lock(&sysfs_mutex);
391}
392
393/**
394 *	sysfs_add_one - add sysfs_dirent to parent without warning
395 *	@acxt: addrm context to use
396 *	@sd: sysfs_dirent to be added
397 *	@parent_sd: the parent sysfs_dirent to add @sd to
398 *
399 *	Get @parent_sd and set @sd->s_parent to it and increment nlink of
400 *	the parent inode if @sd is a directory and link into the children
401 *	list of the parent.
402 *
403 *	This function should be called between calls to
404 *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
405 *	passed the same @acxt as passed to sysfs_addrm_start().
406 *
407 *	LOCKING:
408 *	Determined by sysfs_addrm_start().
409 *
410 *	RETURNS:
411 *	0 on success, -EEXIST if entry with the given name already
412 *	exists.
413 */
414int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
415		  struct sysfs_dirent *parent_sd)
416{
417	bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS;
418	struct sysfs_inode_attrs *ps_iattr;
419	int ret;
420
421	if (has_ns != (bool)sd->s_ns) {
422		WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
423		     has_ns ? "required" : "invalid",
424		     parent_sd->s_name, sd->s_name);
425		return -EINVAL;
426	}
427
428	if (sysfs_type(parent_sd) != SYSFS_DIR)
429		return -EINVAL;
430
431	sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
432	sd->s_parent = parent_sd;
433	kernfs_get(parent_sd);
434
435	ret = sysfs_link_sibling(sd);
436	if (ret)
437		return ret;
438
439	/* Update timestamps on the parent */
440	ps_iattr = parent_sd->s_iattr;
441	if (ps_iattr) {
442		struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
443		ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
444	}
445
446	/* Mark the entry added into directory tree */
447	sd->s_flags &= ~SYSFS_FLAG_REMOVED;
448
449	return 0;
450}
451
452/**
453 *	sysfs_remove_one - remove sysfs_dirent from parent
454 *	@acxt: addrm context to use
455 *	@sd: sysfs_dirent to be removed
456 *
457 *	Mark @sd removed and drop nlink of parent inode if @sd is a
458 *	directory.  @sd is unlinked from the children list.
459 *
460 *	This function should be called between calls to
461 *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
462 *	passed the same @acxt as passed to sysfs_addrm_start().
463 *
464 *	LOCKING:
465 *	Determined by sysfs_addrm_start().
466 */
467static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
468			     struct sysfs_dirent *sd)
469{
470	struct sysfs_inode_attrs *ps_iattr;
471
472	/*
473	 * Removal can be called multiple times on the same node.  Only the
474	 * first invocation is effective and puts the base ref.
475	 */
476	if (sd->s_flags & SYSFS_FLAG_REMOVED)
477		return;
478
479	if (sd->s_parent) {
480		sysfs_unlink_sibling(sd);
481
482		/* Update timestamps on the parent */
483		ps_iattr = sd->s_parent->s_iattr;
484		if (ps_iattr) {
485			ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
486			ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
487		}
488	}
489
490	sd->s_flags |= SYSFS_FLAG_REMOVED;
491	sd->u.removed_list = acxt->removed;
492	acxt->removed = sd;
493}
494
495/**
496 *	sysfs_addrm_finish - finish up sysfs_dirent add/remove
497 *	@acxt: addrm context to finish up
498 *
499 *	Finish up sysfs_dirent add/remove.  Resources acquired by
500 *	sysfs_addrm_start() are released and removed sysfs_dirents are
501 *	cleaned up.
502 *
503 *	LOCKING:
504 *	sysfs_mutex is released.
505 */
506void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
507	__releases(sysfs_mutex)
508{
509	/* release resources acquired by sysfs_addrm_start() */
510	mutex_unlock(&sysfs_mutex);
511
512	/* kill removed sysfs_dirents */
513	while (acxt->removed) {
514		struct sysfs_dirent *sd = acxt->removed;
515
516		acxt->removed = sd->u.removed_list;
517
518		sysfs_deactivate(sd);
519		sysfs_unmap_bin_file(sd);
520		kernfs_put(sd);
521	}
522}
523
524/**
525 * kernfs_find_ns - find sysfs_dirent with the given name
526 * @parent: sysfs_dirent to search under
527 * @name: name to look for
528 * @ns: the namespace tag to use
529 *
530 * Look for sysfs_dirent with name @name under @parent.  Returns pointer to
531 * the found sysfs_dirent on success, %NULL on failure.
532 */
533static struct sysfs_dirent *kernfs_find_ns(struct sysfs_dirent *parent,
534					   const unsigned char *name,
535					   const void *ns)
536{
537	struct rb_node *node = parent->s_dir.children.rb_node;
538	bool has_ns = parent->s_flags & SYSFS_FLAG_NS;
539	unsigned int hash;
540
541	lockdep_assert_held(&sysfs_mutex);
542
543	if (has_ns != (bool)ns) {
544		WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
545		     has_ns ? "required" : "invalid",
546		     parent->s_name, name);
547		return NULL;
548	}
549
550	hash = sysfs_name_hash(name, ns);
551	while (node) {
552		struct sysfs_dirent *sd;
553		int result;
554
555		sd = to_sysfs_dirent(node);
556		result = sysfs_name_compare(hash, name, ns, sd);
557		if (result < 0)
558			node = node->rb_left;
559		else if (result > 0)
560			node = node->rb_right;
561		else
562			return sd;
563	}
564	return NULL;
565}
566
567/**
568 * kernfs_find_and_get_ns - find and get sysfs_dirent with the given name
569 * @parent: sysfs_dirent to search under
570 * @name: name to look for
571 * @ns: the namespace tag to use
572 *
573 * Look for sysfs_dirent with name @name under @parent and get a reference
574 * if found.  This function may sleep and returns pointer to the found
575 * sysfs_dirent on success, %NULL on failure.
576 */
577struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
578					    const char *name, const void *ns)
579{
580	struct sysfs_dirent *sd;
581
582	mutex_lock(&sysfs_mutex);
583	sd = kernfs_find_ns(parent, name, ns);
584	kernfs_get(sd);
585	mutex_unlock(&sysfs_mutex);
586
587	return sd;
588}
589EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
590
591/**
592 * kernfs_create_root - create a new kernfs hierarchy
593 * @priv: opaque data associated with the new directory
594 *
595 * Returns the root of the new hierarchy on success, ERR_PTR() value on
596 * failure.
597 */
598struct kernfs_root *kernfs_create_root(void *priv)
599{
600	struct kernfs_root *root;
601	struct sysfs_dirent *sd;
602
603	root = kzalloc(sizeof(*root), GFP_KERNEL);
604	if (!root)
605		return ERR_PTR(-ENOMEM);
606
607	ida_init(&root->ino_ida);
608
609	sd = sysfs_new_dirent(root, "", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR);
610	if (!sd) {
611		ida_destroy(&root->ino_ida);
612		kfree(root);
613		return ERR_PTR(-ENOMEM);
614	}
615
616	sd->s_flags &= ~SYSFS_FLAG_REMOVED;
617	sd->priv = priv;
618	sd->s_dir.root = root;
619
620	root->sd = sd;
621
622	return root;
623}
624
625/**
626 * kernfs_destroy_root - destroy a kernfs hierarchy
627 * @root: root of the hierarchy to destroy
628 *
629 * Destroy the hierarchy anchored at @root by removing all existing
630 * directories and destroying @root.
631 */
632void kernfs_destroy_root(struct kernfs_root *root)
633{
634	kernfs_remove(root->sd);	/* will also free @root */
635}
636
637/**
638 * kernfs_create_dir_ns - create a directory
639 * @parent: parent in which to create a new directory
640 * @name: name of the new directory
641 * @priv: opaque data associated with the new directory
642 * @ns: optional namespace tag of the directory
643 *
644 * Returns the created node on success, ERR_PTR() value on failure.
645 */
646struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
647					  const char *name, void *priv,
648					  const void *ns)
649{
650	umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
651	struct sysfs_addrm_cxt acxt;
652	struct sysfs_dirent *sd;
653	int rc;
654
655	/* allocate */
656	sd = sysfs_new_dirent(kernfs_root(parent), name, mode, SYSFS_DIR);
657	if (!sd)
658		return ERR_PTR(-ENOMEM);
659
660	sd->s_dir.root = parent->s_dir.root;
661	sd->s_ns = ns;
662	sd->priv = priv;
663
664	/* link in */
665	sysfs_addrm_start(&acxt);
666	rc = sysfs_add_one(&acxt, sd, parent);
667	sysfs_addrm_finish(&acxt);
668
669	if (!rc)
670		return sd;
671
672	kernfs_put(sd);
673	return ERR_PTR(rc);
674}
675
676static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
677				   unsigned int flags)
678{
679	struct dentry *ret = NULL;
680	struct dentry *parent = dentry->d_parent;
681	struct sysfs_dirent *parent_sd = parent->d_fsdata;
682	struct sysfs_dirent *sd;
683	struct inode *inode;
684	const void *ns = NULL;
685
686	mutex_lock(&sysfs_mutex);
687
688	if (parent_sd->s_flags & SYSFS_FLAG_NS)
689		ns = sysfs_info(dir->i_sb)->ns;
690
691	sd = kernfs_find_ns(parent_sd, dentry->d_name.name, ns);
692
693	/* no such entry */
694	if (!sd) {
695		ret = ERR_PTR(-ENOENT);
696		goto out_unlock;
697	}
698	kernfs_get(sd);
699	dentry->d_fsdata = sd;
700
701	/* attach dentry and inode */
702	inode = sysfs_get_inode(dir->i_sb, sd);
703	if (!inode) {
704		ret = ERR_PTR(-ENOMEM);
705		goto out_unlock;
706	}
707
708	/* instantiate and hash dentry */
709	ret = d_materialise_unique(dentry, inode);
710 out_unlock:
711	mutex_unlock(&sysfs_mutex);
712	return ret;
713}
714
715const struct inode_operations sysfs_dir_inode_operations = {
716	.lookup		= sysfs_lookup,
717	.permission	= sysfs_permission,
718	.setattr	= sysfs_setattr,
719	.getattr	= sysfs_getattr,
720	.setxattr	= sysfs_setxattr,
721};
722
723static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
724{
725	struct sysfs_dirent *last;
726
727	while (true) {
728		struct rb_node *rbn;
729
730		last = pos;
731
732		if (sysfs_type(pos) != SYSFS_DIR)
733			break;
734
735		rbn = rb_first(&pos->s_dir.children);
736		if (!rbn)
737			break;
738
739		pos = to_sysfs_dirent(rbn);
740	}
741
742	return last;
743}
744
745/**
746 * sysfs_next_descendant_post - find the next descendant for post-order walk
747 * @pos: the current position (%NULL to initiate traversal)
748 * @root: sysfs_dirent whose descendants to walk
749 *
750 * Find the next descendant to visit for post-order traversal of @root's
751 * descendants.  @root is included in the iteration and the last node to be
752 * visited.
753 */
754static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
755						       struct sysfs_dirent *root)
756{
757	struct rb_node *rbn;
758
759	lockdep_assert_held(&sysfs_mutex);
760
761	/* if first iteration, visit leftmost descendant which may be root */
762	if (!pos)
763		return sysfs_leftmost_descendant(root);
764
765	/* if we visited @root, we're done */
766	if (pos == root)
767		return NULL;
768
769	/* if there's an unvisited sibling, visit its leftmost descendant */
770	rbn = rb_next(&pos->s_rb);
771	if (rbn)
772		return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
773
774	/* no sibling left, visit parent */
775	return pos->s_parent;
776}
777
778static void __kernfs_remove(struct sysfs_addrm_cxt *acxt,
779			    struct sysfs_dirent *sd)
780{
781	struct sysfs_dirent *pos, *next;
782
783	if (!sd)
784		return;
785
786	pr_debug("sysfs %s: removing\n", sd->s_name);
787
788	next = NULL;
789	do {
790		pos = next;
791		next = sysfs_next_descendant_post(pos, sd);
792		if (pos)
793			sysfs_remove_one(acxt, pos);
794	} while (next);
795}
796
797/**
798 * kernfs_remove - remove a sysfs_dirent recursively
799 * @sd: the sysfs_dirent to remove
800 *
801 * Remove @sd along with all its subdirectories and files.
802 */
803void kernfs_remove(struct sysfs_dirent *sd)
804{
805	struct sysfs_addrm_cxt acxt;
806
807	sysfs_addrm_start(&acxt);
808	__kernfs_remove(&acxt, sd);
809	sysfs_addrm_finish(&acxt);
810}
811
812/**
813 * kernfs_remove_by_name_ns - find a sysfs_dirent by name and remove it
814 * @dir_sd: parent of the target
815 * @name: name of the sysfs_dirent to remove
816 * @ns: namespace tag of the sysfs_dirent to remove
817 *
818 * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove
819 * it.  Returns 0 on success, -ENOENT if such entry doesn't exist.
820 */
821int kernfs_remove_by_name_ns(struct sysfs_dirent *dir_sd, const char *name,
822			     const void *ns)
823{
824	struct sysfs_addrm_cxt acxt;
825	struct sysfs_dirent *sd;
826
827	if (!dir_sd) {
828		WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
829			name);
830		return -ENOENT;
831	}
832
833	sysfs_addrm_start(&acxt);
834
835	sd = kernfs_find_ns(dir_sd, name, ns);
836	if (sd)
837		__kernfs_remove(&acxt, sd);
838
839	sysfs_addrm_finish(&acxt);
840
841	if (sd)
842		return 0;
843	else
844		return -ENOENT;
845}
846
847/**
848 * kernfs_rename_ns - move and rename a kernfs_node
849 * @sd: target node
850 * @new_parent: new parent to put @sd under
851 * @new_name: new name
852 * @new_ns: new namespace tag
853 */
854int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
855		     const char *new_name, const void *new_ns)
856{
857	int error;
858
859	mutex_lock(&sysfs_mutex);
860
861	error = 0;
862	if ((sd->s_parent == new_parent) && (sd->s_ns == new_ns) &&
863	    (strcmp(sd->s_name, new_name) == 0))
864		goto out;	/* nothing to rename */
865
866	error = -EEXIST;
867	if (kernfs_find_ns(new_parent, new_name, new_ns))
868		goto out;
869
870	/* rename sysfs_dirent */
871	if (strcmp(sd->s_name, new_name) != 0) {
872		error = -ENOMEM;
873		new_name = kstrdup(new_name, GFP_KERNEL);
874		if (!new_name)
875			goto out;
876
877		kfree(sd->s_name);
878		sd->s_name = new_name;
879	}
880
881	/*
882	 * Move to the appropriate place in the appropriate directories rbtree.
883	 */
884	sysfs_unlink_sibling(sd);
885	kernfs_get(new_parent);
886	kernfs_put(sd->s_parent);
887	sd->s_ns = new_ns;
888	sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
889	sd->s_parent = new_parent;
890	sysfs_link_sibling(sd);
891
892	error = 0;
893 out:
894	mutex_unlock(&sysfs_mutex);
895	return error;
896}
897
898/**
899 * kernfs_enable_ns - enable namespace under a directory
900 * @sd: directory of interest, should be empty
901 *
902 * This is to be called right after @sd is created to enable namespace
903 * under it.  All children of @sd must have non-NULL namespace tags and
904 * only the ones which match the super_block's tag will be visible.
905 */
906void kernfs_enable_ns(struct sysfs_dirent *sd)
907{
908	WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
909	WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children));
910	sd->s_flags |= SYSFS_FLAG_NS;
911}
912
913/* Relationship between s_mode and the DT_xxx types */
914static inline unsigned char dt_type(struct sysfs_dirent *sd)
915{
916	return (sd->s_mode >> 12) & 15;
917}
918
919static int sysfs_dir_release(struct inode *inode, struct file *filp)
920{
921	kernfs_put(filp->private_data);
922	return 0;
923}
924
925static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
926	struct sysfs_dirent *parent_sd,	loff_t hash, struct sysfs_dirent *pos)
927{
928	if (pos) {
929		int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
930			pos->s_parent == parent_sd &&
931			hash == pos->s_hash;
932		kernfs_put(pos);
933		if (!valid)
934			pos = NULL;
935	}
936	if (!pos && (hash > 1) && (hash < INT_MAX)) {
937		struct rb_node *node = parent_sd->s_dir.children.rb_node;
938		while (node) {
939			pos = to_sysfs_dirent(node);
940
941			if (hash < pos->s_hash)
942				node = node->rb_left;
943			else if (hash > pos->s_hash)
944				node = node->rb_right;
945			else
946				break;
947		}
948	}
949	/* Skip over entries in the wrong namespace */
950	while (pos && pos->s_ns != ns) {
951		struct rb_node *node = rb_next(&pos->s_rb);
952		if (!node)
953			pos = NULL;
954		else
955			pos = to_sysfs_dirent(node);
956	}
957	return pos;
958}
959
960static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
961	struct sysfs_dirent *parent_sd,	ino_t ino, struct sysfs_dirent *pos)
962{
963	pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
964	if (pos)
965		do {
966			struct rb_node *node = rb_next(&pos->s_rb);
967			if (!node)
968				pos = NULL;
969			else
970				pos = to_sysfs_dirent(node);
971		} while (pos && pos->s_ns != ns);
972	return pos;
973}
974
975static int sysfs_readdir(struct file *file, struct dir_context *ctx)
976{
977	struct dentry *dentry = file->f_path.dentry;
978	struct sysfs_dirent *parent_sd = dentry->d_fsdata;
979	struct sysfs_dirent *pos = file->private_data;
980	const void *ns = NULL;
981
982	if (!dir_emit_dots(file, ctx))
983		return 0;
984	mutex_lock(&sysfs_mutex);
985
986	if (parent_sd->s_flags & SYSFS_FLAG_NS)
987		ns = sysfs_info(dentry->d_sb)->ns;
988
989	for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
990	     pos;
991	     pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
992		const char *name = pos->s_name;
993		unsigned int type = dt_type(pos);
994		int len = strlen(name);
995		ino_t ino = pos->s_ino;
996
997		ctx->pos = pos->s_hash;
998		file->private_data = pos;
999		kernfs_get(pos);
1000
1001		mutex_unlock(&sysfs_mutex);
1002		if (!dir_emit(ctx, name, len, ino, type))
1003			return 0;
1004		mutex_lock(&sysfs_mutex);
1005	}
1006	mutex_unlock(&sysfs_mutex);
1007	file->private_data = NULL;
1008	ctx->pos = INT_MAX;
1009	return 0;
1010}
1011
1012static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1013{
1014	struct inode *inode = file_inode(file);
1015	loff_t ret;
1016
1017	mutex_lock(&inode->i_mutex);
1018	ret = generic_file_llseek(file, offset, whence);
1019	mutex_unlock(&inode->i_mutex);
1020
1021	return ret;
1022}
1023
1024const struct file_operations sysfs_dir_operations = {
1025	.read		= generic_read_dir,
1026	.iterate	= sysfs_readdir,
1027	.release	= sysfs_dir_release,
1028	.llseek		= sysfs_dir_llseek,
1029};
1030