Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * fs/kernfs/dir.c - kernfs directory implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2001-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (c) 2007 SUSE Linux Products GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "kernfs-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) DEFINE_MUTEX(kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) static DEFINE_SPINLOCK(kernfs_rename_lock);	/* kn->parent and ->name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) static char kernfs_pr_cont_buf[PATH_MAX];	/* protected by rename_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) static DEFINE_SPINLOCK(kernfs_idr_lock);	/* root->ino_idr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) static bool kernfs_active(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	return atomic_read(&kn->active) >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) static bool kernfs_lockdep(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #ifdef CONFIG_DEBUG_LOCK_ALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	return kn->flags & KERNFS_LOCKDEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 		return strlcpy(buf, "(null)", buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /* kernfs_node_depth - compute depth from @from to @to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	size_t depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	while (to->parent && to != from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		to = to->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	return depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 						  struct kernfs_node *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	size_t da, db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	if (ra != rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	da = kernfs_depth(ra->kn, a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	db = kernfs_depth(rb->kn, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	while (da > db) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		a = a->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		da--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	while (db > da) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		b = b->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		db--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	/* worst case b and a will be the same at root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	while (b != a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		b = b->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		a = a->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	return a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * where kn_from is treated as root of the path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * @kn_from: kernfs node which should be treated as root for the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * @kn_to: kernfs node to which path is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * @buf: buffer to copy the path into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * @buflen: size of @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * We need to handle couple of scenarios here:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * [1] when @kn_from is an ancestor of @kn_to at some level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * kn_from: /n1/n2/n3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * kn_to:   /n1/n2/n3/n4/n5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * result:  /n4/n5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * [2] when @kn_from is on a different hierarchy and we need to find common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * ancestor between @kn_from and @kn_to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * kn_from: /n1/n2/n3/n4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * kn_to:   /n1/n2/n5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * result:  /../../n5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * kn_from: /n1/n2/n3/n4/n5   [depth=5]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * kn_to:   /n1/n2/n3         [depth=3]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * result:  /../..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * [3] when @kn_to is NULL result will be "(null)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * Returns the length of the full path.  If the full length is equal to or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * greater than @buflen, @buf contains the truncated path with the trailing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * '\0'.  On error, -errno is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 					struct kernfs_node *kn_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 					char *buf, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct kernfs_node *kn, *common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	const char parent_str[] = "/..";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	size_t depth_from, depth_to, len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (!kn_to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return strlcpy(buf, "(null)", buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (!kn_from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		kn_from = kernfs_root(kn_to)->kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (kn_from == kn_to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return strlcpy(buf, "/", buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	common = kernfs_common_ancestor(kn_from, kn_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	if (WARN_ON(!common))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	depth_to = kernfs_depth(common, kn_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	depth_from = kernfs_depth(common, kn_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	for (i = 0; i < depth_from; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		len += strlcpy(buf + len, parent_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 			       len < buflen ? buflen - len : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	/* Calculate how many bytes we need for the rest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	for (i = depth_to - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		for (kn = kn_to, j = 0; j < i; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			kn = kn->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		len += strlcpy(buf + len, "/",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			       len < buflen ? buflen - len : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		len += strlcpy(buf + len, kn->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			       len < buflen ? buflen - len : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * kernfs_name - obtain the name of a given node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * @kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  * @buf: buffer to copy @kn's name into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  * @buflen: size of @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * Copies the name of @kn into @buf of @buflen bytes.  The behavior is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * similar to strlcpy().  It returns the length of @kn's name and if @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * isn't long enough, it's filled upto @buflen-1 and nul terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * Fills buffer with "(null)" if @kn is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * This function can be called from any context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	spin_lock_irqsave(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	ret = kernfs_name_locked(kn, buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	spin_unlock_irqrestore(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  * kernfs_path_from_node - build path of node @to relative to @from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * @from: parent kernfs_node relative to which we need to build the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  * @to: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * @buf: buffer to copy @to's path into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * @buflen: size of @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * Builds @to's path relative to @from in @buf. @from and @to must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * be on the same kernfs-root. If @from is not parent of @to, then a relative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * path (which includes '..'s) as needed to reach from @from to @to is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * Returns the length of the full path.  If the full length is equal to or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * greater than @buflen, @buf contains the truncated path with the trailing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * '\0'.  On error, -errno is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			  char *buf, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	spin_lock_irqsave(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	ret = kernfs_path_from_node_locked(to, from, buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	spin_unlock_irqrestore(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) EXPORT_SYMBOL_GPL(kernfs_path_from_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  * pr_cont_kernfs_name - pr_cont name of a kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * @kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * This function can be called from any context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) void pr_cont_kernfs_name(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	spin_lock_irqsave(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	kernfs_name_locked(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	pr_cont("%s", kernfs_pr_cont_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	spin_unlock_irqrestore(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242)  * pr_cont_kernfs_path - pr_cont path of a kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243)  * @kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245)  * This function can be called from any context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) void pr_cont_kernfs_path(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	int sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	spin_lock_irqsave(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	sz = kernfs_path_from_node_locked(kn, NULL, kernfs_pr_cont_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 					  sizeof(kernfs_pr_cont_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (sz < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		pr_cont("(error)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	if (sz >= sizeof(kernfs_pr_cont_buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		pr_cont("(name too long)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	pr_cont("%s", kernfs_pr_cont_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	spin_unlock_irqrestore(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * kernfs_get_parent - determine the parent node and pin it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  * @kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * Determines @kn's parent, pins and returns it.  This function can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * called from any context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	struct kernfs_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	spin_lock_irqsave(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	parent = kn->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	kernfs_get(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	spin_unlock_irqrestore(&kernfs_rename_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  *	kernfs_name_hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  *	@name: Null terminated string to hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  *	@ns:   Namespace tag to hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  *	Returns 31 bit hash of ns + name (so it fits in an off_t )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) static unsigned int kernfs_name_hash(const char *name, const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	unsigned long hash = init_name_hash(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	unsigned int len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	while (len--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		hash = partial_name_hash(*name++, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	hash = end_name_hash(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	hash &= 0x7fffffffU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	/* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	if (hash < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		hash += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (hash >= INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		hash = INT_MAX - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static int kernfs_name_compare(unsigned int hash, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			       const void *ns, const struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (hash < kn->hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	if (hash > kn->hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (ns < kn->ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	if (ns > kn->ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	return strcmp(name, kn->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static int kernfs_sd_compare(const struct kernfs_node *left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			     const struct kernfs_node *right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	return kernfs_name_compare(left->hash, left->name, left->ns, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  *	kernfs_link_sibling - link kernfs_node into sibling rbtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  *	@kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  *	Link @kn into its sibling rbtree which starts from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  *	@kn->parent->dir.children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  *	Locking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  *	mutex_lock(kernfs_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  *	RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  *	0 on susccess -EEXIST on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static int kernfs_link_sibling(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct rb_node **node = &kn->parent->dir.children.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	while (*node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		struct kernfs_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		pos = rb_to_kn(*node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		parent = *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		result = kernfs_sd_compare(kn, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			node = &pos->rb.rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		else if (result > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			node = &pos->rb.rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	/* add new node and rebalance the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	rb_link_node(&kn->rb, parent, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	rb_insert_color(&kn->rb, &kn->parent->dir.children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	/* successfully added, account subdir number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	if (kernfs_type(kn) == KERNFS_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		kn->parent->dir.subdirs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  *	kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  *	@kn: kernfs_node of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  *	Try to unlink @kn from its sibling rbtree which starts from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  *	kn->parent->dir.children.  Returns %true if @kn was actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385)  *	removed, %false if @kn wasn't on the rbtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387)  *	Locking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388)  *	mutex_lock(kernfs_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static bool kernfs_unlink_sibling(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	if (RB_EMPTY_NODE(&kn->rb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (kernfs_type(kn) == KERNFS_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		kn->parent->dir.subdirs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	rb_erase(&kn->rb, &kn->parent->dir.children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	RB_CLEAR_NODE(&kn->rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  *	kernfs_get_active - get an active reference to kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *	@kn: kernfs_node to get an active reference to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  *	Get an active reference of @kn.  This function is noop if @kn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *	is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  *	RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  *	Pointer to @kn on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	if (unlikely(!kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (!atomic_inc_unless_negative(&kn->active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	if (kernfs_lockdep(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  *	kernfs_put_active - put an active reference to kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  *	@kn: kernfs_node to put an active reference to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *	Put an active reference to @kn.  This function is noop if @kn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  *	is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) void kernfs_put_active(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (unlikely(!kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (kernfs_lockdep(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		rwsem_release(&kn->dep_map, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	v = atomic_dec_return(&kn->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (likely(v != KN_DEACTIVATED_BIAS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	wake_up_all(&kernfs_root(kn)->deactivate_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * kernfs_drain - drain kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * @kn: kernfs_node to drain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * Drain existing usages and nuke all existing mmaps of @kn.  Mutiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * removers may invoke this function concurrently on @kn and all will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  * return after draining is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) static void kernfs_drain(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	__releases(&kernfs_mutex) __acquires(&kernfs_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	struct kernfs_root *root = kernfs_root(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	WARN_ON_ONCE(kernfs_active(kn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	if (kernfs_lockdep(kn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			lock_contended(&kn->dep_map, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	/* but everyone should wait for draining */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	wait_event(root->deactivate_waitq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		   atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (kernfs_lockdep(kn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		lock_acquired(&kn->dep_map, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		rwsem_release(&kn->dep_map, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	kernfs_drain_open_files(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * kernfs_get - get a reference count on a kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * @kn: the target kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) void kernfs_get(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	if (kn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		WARN_ON(!atomic_read(&kn->count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		atomic_inc(&kn->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) EXPORT_SYMBOL_GPL(kernfs_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  * kernfs_put - put a reference count on a kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  * @kn: the target kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504)  * Put a reference count of @kn and destroy it if it reached zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) void kernfs_put(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	struct kernfs_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	struct kernfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (!kn || !atomic_dec_and_test(&kn->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	root = kernfs_root(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	 * Moving/renaming is always done while holding reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	 * kn->parent won't change beneath us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	parent = kn->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		  "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		  parent ? parent->name : "", kn->name, atomic_read(&kn->active));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (kernfs_type(kn) == KERNFS_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		kernfs_put(kn->symlink.target_kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	kfree_const(kn->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (kn->iattr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		simple_xattrs_free(&kn->iattr->xattrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	spin_lock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	spin_unlock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	kmem_cache_free(kernfs_node_cache, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	kn = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (kn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		if (atomic_dec_and_test(&kn->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		/* just released the root kn, free @root too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		idr_destroy(&root->ino_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		kfree(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) EXPORT_SYMBOL_GPL(kernfs_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (flags & LOOKUP_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	/* Always perform fresh lookup for negatives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		goto out_bad_unlocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	kn = kernfs_dentry_node(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	/* The kernfs node has been deactivated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (!kernfs_active(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		goto out_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	/* The kernfs node has been moved? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (kernfs_dentry_node(dentry->d_parent) != kn->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		goto out_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	/* The kernfs node has been renamed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (strcmp(dentry->d_name.name, kn->name) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		goto out_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	/* The kernfs node has been moved to a different namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (kn->parent && kernfs_ns_enabled(kn->parent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	    kernfs_info(dentry->d_sb)->ns != kn->ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		goto out_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) out_bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) out_bad_unlocked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) const struct dentry_operations kernfs_dops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	.d_revalidate	= kernfs_dop_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * @dentry: the dentry in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * Return the kernfs_node associated with @dentry.  If @dentry is not a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * kernfs one, %NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * While the returned kernfs_node will stay accessible as long as @dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * is accessible, the returned node can be in any state and the caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * fully responsible for determining what's accessible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	if (dentry->d_sb->s_op == &kernfs_sops &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	    !d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		return kernfs_dentry_node(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 					     struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 					     const char *name, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 					     kuid_t uid, kgid_t gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 					     unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	u32 id_highbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	name = kstrdup_const(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		goto err_out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	spin_lock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (ret >= 0 && ret < root->last_id_lowbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		root->id_highbits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	id_highbits = root->id_highbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	root->last_id_lowbits = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	spin_unlock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		goto err_out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	kn->id = (u64)id_highbits << 32 | ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	atomic_set(&kn->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	RB_CLEAR_NODE(&kn->rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	kn->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	kn->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	kn->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		struct iattr iattr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			.ia_valid = ATTR_UID | ATTR_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			.ia_uid = uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			.ia_gid = gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		ret = __kernfs_setattr(kn, &iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			goto err_out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		ret = security_kernfs_init_security(parent, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			goto err_out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  err_out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  err_out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	kmem_cache_free(kernfs_node_cache, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677)  err_out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	kfree_const(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 				    const char *name, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 				    kuid_t uid, kgid_t gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 				    unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	kn = __kernfs_new_node(kernfs_root(parent), parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			       name, mode, uid, gid, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if (kn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		kernfs_get(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		kn->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699)  * kernfs_find_and_get_node_by_id - get kernfs_node from node id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700)  * @root: the kernfs root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701)  * @id: the target node id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  * @id's lower 32bits encode ino and upper gen.  If the gen portion is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704)  * zero, all generations are matched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * NULL on failure. Return a kernfs node with reference counter incremented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 						   u64 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	ino_t ino = kernfs_id_ino(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	u32 gen = kernfs_id_gen(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	spin_lock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	kn = idr_find(&root->ino_idr, (u32)ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	if (sizeof(ino_t) >= sizeof(u64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		/* we looked up with the low 32bits, compare the whole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		if (kernfs_ino(kn) != ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		/* 0 matches all generations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		if (unlikely(gen && kernfs_gen(kn) != gen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 * ACTIVATED is protected with kernfs_mutex but it was clear when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	 * @kn was added to idr and we just wanna see it set.  No need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	 * grab kernfs_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (unlikely(!(kn->flags & KERNFS_ACTIVATED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		     !atomic_inc_not_zero(&kn->count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	spin_unlock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	spin_unlock(&kernfs_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749)  *	kernfs_add_one - add kernfs_node to parent without warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)  *	@kn: kernfs_node to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  *	The caller must already have initialized @kn->parent.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  *	function increments nlink of the parent's inode if @kn is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  *	directory and link into the children list of the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  *	RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  *	0 on success, -EEXIST if entry with the given name already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  *	exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) int kernfs_add_one(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	struct kernfs_node *parent = kn->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	struct kernfs_iattrs *ps_iattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	bool has_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	has_ns = kernfs_ns_enabled(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		 has_ns ? "required" : "invalid", parent->name, kn->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (kernfs_type(parent) != KERNFS_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (parent->flags & KERNFS_EMPTY_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	kn->hash = kernfs_name_hash(kn->name, kn->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	ret = kernfs_link_sibling(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	/* Update timestamps on the parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	ps_iattr = parent->iattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (ps_iattr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		ktime_get_real_ts64(&ps_iattr->ia_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		ps_iattr->ia_mtime = ps_iattr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	 * Activate the new node unless CREATE_DEACTIVATED is requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	 * If not activated here, the kernfs user is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	 * activating the node with kernfs_activate().  A node which hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	 * been activated is not visible to userland and its removal won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	 * trigger deactivation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		kernfs_activate(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * kernfs_find_ns - find kernfs_node with the given name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * @parent: kernfs_node to search under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  * @name: name to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * @ns: the namespace tag to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * Look for kernfs_node with name @name under @parent.  Returns pointer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  * the found kernfs_node on success, %NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 					  const unsigned char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 					  const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct rb_node *node = parent->dir.children.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	bool has_ns = kernfs_ns_enabled(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	unsigned int hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (has_ns != (bool)ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		     has_ns ? "required" : "invalid", parent->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	hash = kernfs_name_hash(name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		kn = rb_to_kn(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		result = kernfs_name_compare(hash, name, ns, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		else if (result > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 					  const unsigned char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 					  const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	char *p, *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	/* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	spin_lock_irq(&kernfs_rename_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (len >= sizeof(kernfs_pr_cont_buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		spin_unlock_irq(&kernfs_rename_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	p = kernfs_pr_cont_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	while ((name = strsep(&p, "/")) && parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (*name == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		parent = kernfs_find_ns(parent, name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	spin_unlock_irq(&kernfs_rename_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  * kernfs_find_and_get_ns - find and get kernfs_node with the given name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892)  * @parent: kernfs_node to search under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893)  * @name: name to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894)  * @ns: the namespace tag to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  * Look for kernfs_node with name @name under @parent and get a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  * if found.  This function may sleep and returns pointer to the found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * kernfs_node on success, %NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 					   const char *name, const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	kn = kernfs_find_ns(parent, name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	kernfs_get(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  * @parent: kernfs_node to search under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * @path: path to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * @ns: the namespace tag to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  * Look for kernfs_node with path @path under @parent and get a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  * if found.  This function may sleep and returns pointer to the found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  * kernfs_node on success, %NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 					   const char *path, const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	kn = kernfs_walk_ns(parent, path, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	kernfs_get(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  * kernfs_create_root - create a new kernfs hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  * @scops: optional syscall operations for the hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * @flags: KERNFS_ROOT_* flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  * @priv: opaque data associated with the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  * Returns the root of the new hierarchy on success, ERR_PTR() value on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944)  * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				       unsigned int flags, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct kernfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	root = kzalloc(sizeof(*root), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	idr_init(&root->ino_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	INIT_LIST_HEAD(&root->supers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	 * On 64bit ino setups, id is ino.  On 32bit, low 32bits are ino.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	 * High bits generation.  The starting value for both ino and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	 * genenration is 1.  Initialize upper 32bit allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	 * accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (sizeof(ino_t) >= sizeof(u64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		root->id_highbits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		root->id_highbits = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			       GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			       KERNFS_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	if (!kn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		idr_destroy(&root->ino_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		kfree(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	kn->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	kn->dir.root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	root->syscall_ops = scops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	root->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	root->kn = kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	init_waitqueue_head(&root->deactivate_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		kernfs_activate(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	return root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * kernfs_destroy_root - destroy a kernfs hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @root: root of the hierarchy to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * Destroy the hierarchy anchored at @root by removing all existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * directories and destroying @root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) void kernfs_destroy_root(struct kernfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	kernfs_remove(root->kn);	/* will also free @root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * kernfs_create_dir_ns - create a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  * @parent: parent in which to create a new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * @name: name of the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  * @mode: mode of the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * @uid: uid of the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  * @gid: gid of the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  * @priv: opaque data associated with the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)  * @ns: optional namespace tag of the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  * Returns the created node on success, ERR_PTR() value on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 					 const char *name, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 					 kuid_t uid, kgid_t gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 					 void *priv, const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	/* allocate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	kn = kernfs_new_node(parent, name, mode | S_IFDIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			     uid, gid, KERNFS_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	kn->dir.root = parent->dir.root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	kn->ns = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	kn->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/* link in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	rc = kernfs_add_one(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	kernfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)  * kernfs_create_empty_dir - create an always empty directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  * @parent: parent in which to create a new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  * @name: name of the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)  * Returns the created node on success, ERR_PTR() value on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 					    const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	/* allocate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			     GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	kn->flags |= KERNFS_EMPTY_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	kn->dir.root = parent->dir.root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	kn->ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	kn->priv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	/* link in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	rc = kernfs_add_one(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		return kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	kernfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static struct dentry *kernfs_iop_lookup(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 					struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 					unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct dentry *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	struct kernfs_node *parent = dir->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	const void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (kernfs_ns_enabled(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		ns = kernfs_info(dir->i_sb)->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	/* no such entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (!kn || !kernfs_active(kn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	/* attach dentry and inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	inode = kernfs_get_inode(dir->i_sb, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	/* instantiate and hash dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	ret = d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			    umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	struct kernfs_node *parent = dir->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (!scops || !scops->mkdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (!kernfs_get_active(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	ret = scops->mkdir(parent, dentry->d_name.name, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	kernfs_put_active(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	struct kernfs_node *kn  = kernfs_dentry_node(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (!scops || !scops->rmdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (!kernfs_get_active(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	ret = scops->rmdir(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	kernfs_put_active(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			     struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			     unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct kernfs_node *new_parent = new_dir->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	if (!scops || !scops->rename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	if (!kernfs_get_active(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	if (!kernfs_get_active(new_parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		kernfs_put_active(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	kernfs_put_active(new_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	kernfs_put_active(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) const struct inode_operations kernfs_dir_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	.lookup		= kernfs_iop_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	.permission	= kernfs_iop_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	.setattr	= kernfs_iop_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	.getattr	= kernfs_iop_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	.listxattr	= kernfs_iop_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	.mkdir		= kernfs_iop_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	.rmdir		= kernfs_iop_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	.rename		= kernfs_iop_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	struct kernfs_node *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		struct rb_node *rbn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		last = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		if (kernfs_type(pos) != KERNFS_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		rbn = rb_first(&pos->dir.children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		if (!rbn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		pos = rb_to_kn(rbn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	return last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  * kernfs_next_descendant_post - find the next descendant for post-order walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  * @pos: the current position (%NULL to initiate traversal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  * @root: kernfs_node whose descendants to walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)  * Find the next descendant to visit for post-order traversal of @root's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)  * descendants.  @root is included in the iteration and the last node to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)  * visited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 						       struct kernfs_node *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	struct rb_node *rbn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	/* if first iteration, visit leftmost descendant which may be root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		return kernfs_leftmost_descendant(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	/* if we visited @root, we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	if (pos == root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	/* if there's an unvisited sibling, visit its leftmost descendant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	rbn = rb_next(&pos->rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	if (rbn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		return kernfs_leftmost_descendant(rb_to_kn(rbn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	/* no sibling left, visit parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	return pos->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  * kernfs_activate - activate a node which started deactivated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  * @kn: kernfs_node whose subtree is to be activated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  * needs to be explicitly activated.  A node which hasn't been activated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  * isn't visible to userland and deactivation is skipped during its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)  * removal.  This is useful to construct atomic init sequences where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)  * creation of multiple nodes should either succeed or fail atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)  * The caller is responsible for ensuring that this function is not called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)  * after kernfs_remove*() is invoked on @kn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) void kernfs_activate(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	struct kernfs_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	while ((pos = kernfs_next_descendant_post(pos, kn))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		if (pos->flags & KERNFS_ACTIVATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		pos->flags |= KERNFS_ACTIVATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) static void __kernfs_remove(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	struct kernfs_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	lockdep_assert_held(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	 * Short-circuit if non-root @kn has already finished removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	 * This is for kernfs_remove_self() which plays with active ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	 * after removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	pr_debug("kernfs %s: removing\n", kn->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	/* prevent any new usage under @kn by deactivating all nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	while ((pos = kernfs_next_descendant_post(pos, kn)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		if (kernfs_active(pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	/* deactivate and unlink the subtree node-by-node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		pos = kernfs_leftmost_descendant(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		 * base ref could have been put by someone else by the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		 * the function returns.  Make sure it doesn't go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		 * underneath us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		kernfs_get(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		 * Drain iff @kn was activated.  This avoids draining and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		 * its lockdep annotations for nodes which have never been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		 * activated and allows embedding kernfs_remove() in create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		 * error paths without worrying about draining.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		if (kn->flags & KERNFS_ACTIVATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			kernfs_drain(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		 * kernfs_unlink_sibling() succeeds once per node.  Use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		 * to decide who's responsible for cleanups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		if (!pos->parent || kernfs_unlink_sibling(pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			struct kernfs_iattrs *ps_iattr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				pos->parent ? pos->parent->iattr : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			/* update timestamps on the parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			if (ps_iattr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 				ktime_get_real_ts64(&ps_iattr->ia_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 				ps_iattr->ia_mtime = ps_iattr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			kernfs_put(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		kernfs_put(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	} while (pos != kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)  * kernfs_remove - remove a kernfs_node recursively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)  * @kn: the kernfs_node to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)  * Remove @kn along with all its subdirectories and files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) void kernfs_remove(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	__kernfs_remove(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)  * kernfs_break_active_protection - break out of active protection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)  * @kn: the self kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)  * The caller must be running off of a kernfs operation which is invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * with an active reference - e.g. one of kernfs_ops.  Each invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * this function must also be matched with an invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * kernfs_unbreak_active_protection().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)  * This function releases the active reference of @kn the caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)  * holding.  Once this function is called, @kn may be removed at any point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)  * and the caller is solely responsible for ensuring that the objects it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)  * dereferences are accessible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) void kernfs_break_active_protection(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	 * Take out ourself out of the active ref dependency chain.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	 * we're called without an active ref, lockdep will complain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	kernfs_put_active(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385)  * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)  * @kn: the self kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)  * If kernfs_break_active_protection() was called, this function must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)  * invoked before finishing the kernfs operation.  Note that while this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)  * function restores the active reference, it doesn't and can't actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)  * restore the active protection - @kn may already or be in the process of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)  * being removed.  Once kernfs_break_active_protection() is invoked, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)  * protection is irreversibly gone for the kernfs operation instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  * While this function may be called at any point after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)  * kernfs_break_active_protection() is invoked, its most useful location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  * would be right before the enclosing kernfs operation returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) void kernfs_unbreak_active_protection(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 * @kn->active could be in any state; however, the increment we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 * here will be undone as soon as the enclosing kernfs operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	 * finishes and this temporary bump can't break anything.  If @kn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	 * is alive, nothing changes.  If @kn is being deactivated, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	 * soon-to-follow put will either finish deactivation or restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	 * deactivated state.  If @kn is already removed, the temporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	 * bump is guaranteed to be gone before @kn is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	atomic_inc(&kn->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	if (kernfs_lockdep(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)  * kernfs_remove_self - remove a kernfs_node from its own method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)  * @kn: the self kernfs_node to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)  * The caller must be running off of a kernfs operation which is invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)  * with an active reference - e.g. one of kernfs_ops.  This can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)  * implement a file operation which deletes itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)  * For example, the "delete" file for a sysfs device directory can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)  * implemented by invoking kernfs_remove_self() on the "delete" file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)  * itself.  This function breaks the circular dependency of trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)  * deactivate self while holding an active ref itself.  It isn't necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)  * to modify the usual removal path to use kernfs_remove_self().  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)  * "delete" implementation can simply invoke kernfs_remove_self() on self
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)  * before proceeding with the usual removal path.  kernfs will ignore later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)  * kernfs_remove() on self.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)  * kernfs_remove_self() can be called multiple times concurrently on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)  * same kernfs_node.  Only the first one actually performs removal and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)  * returns %true.  All others will wait until the kernfs operation which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)  * won self-removal finishes and return %false.  Note that the losers wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)  * for the completion of not only the winning kernfs_remove_self() but also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  * the whole kernfs_ops which won the arbitration.  This can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)  * guarantee, for example, all concurrent writes to a "delete" file to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)  * finish only after the whole operation is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) bool kernfs_remove_self(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	kernfs_break_active_protection(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	 * SUICIDAL is used to arbitrate among competing invocations.  Only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	 * the first one will actually perform removal.  When the removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	 * is complete, SUICIDED is set and the active ref is restored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	 * while holding kernfs_mutex.  The ones which lost arbitration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	 * waits for SUICDED && drained which can happen only after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	 * enclosing kernfs operation which executed the winning instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * of kernfs_remove_self() finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	if (!(kn->flags & KERNFS_SUICIDAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		kn->flags |= KERNFS_SUICIDAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		__kernfs_remove(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		kn->flags |= KERNFS_SUICIDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 			if ((kn->flags & KERNFS_SUICIDED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 			    atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 			schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		finish_wait(waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	 * This must be done while holding kernfs_mutex; otherwise, waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	 * for SUICIDED && deactivated could finish prematurely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	kernfs_unbreak_active_protection(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)  * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)  * @parent: parent of the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)  * @name: name of the kernfs_node to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)  * @ns: namespace tag of the kernfs_node to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)  * Look for the kernfs_node with @name and @ns under @parent and remove it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)  * Returns 0 on success, -ENOENT if such entry doesn't exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 			     const void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	if (!parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	kn = kernfs_find_ns(parent, name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	if (kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		__kernfs_remove(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	if (kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  * kernfs_rename_ns - move and rename a kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  * @kn: target node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)  * @new_parent: new parent to put @sd under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530)  * @new_name: new name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)  * @new_ns: new namespace tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		     const char *new_name, const void *new_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	struct kernfs_node *old_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	const char *old_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	/* can't move or rename root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (!kn->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	    (new_parent->flags & KERNFS_EMPTY_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	    (strcmp(kn->name, new_name) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		goto out;	/* nothing to rename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	if (kernfs_find_ns(new_parent, new_name, new_ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	/* rename kernfs_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (strcmp(kn->name, new_name) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		new_name = kstrdup_const(new_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		if (!new_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		new_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	 * Move to the appropriate place in the appropriate directories rbtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	kernfs_unlink_sibling(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	kernfs_get(new_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	/* rename_lock protects ->parent and ->name accessors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	spin_lock_irq(&kernfs_rename_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	old_parent = kn->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	kn->parent = new_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	kn->ns = new_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (new_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		old_name = kn->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		kn->name = new_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	spin_unlock_irq(&kernfs_rename_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	kn->hash = kernfs_name_hash(kn->name, kn->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	kernfs_link_sibling(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	kernfs_put(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	kfree_const(old_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) /* Relationship between s_mode and the DT_xxx types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) static inline unsigned char dt_type(struct kernfs_node *kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	return (kn->mode >> 12) & 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	kernfs_put(filp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) static struct kernfs_node *kernfs_dir_pos(const void *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	if (pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		int valid = kernfs_active(pos) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			pos->parent == parent && hash == pos->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		kernfs_put(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		if (!valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 			pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (!pos && (hash > 1) && (hash < INT_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		struct rb_node *node = parent->dir.children.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 			pos = rb_to_kn(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 			if (hash < pos->hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 				node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 			else if (hash > pos->hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 				node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	/* Skip over entries which are dying/dead or in the wrong namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		struct rb_node *node = rb_next(&pos->rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			pos = rb_to_kn(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	pos = kernfs_dir_pos(ns, parent, ino, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	if (pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			struct rb_node *node = rb_next(&pos->rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 				pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 				pos = rb_to_kn(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		} while (pos && (!kernfs_active(pos) || pos->ns != ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	struct dentry *dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	struct kernfs_node *parent = kernfs_dentry_node(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	struct kernfs_node *pos = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	const void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	if (kernfs_ns_enabled(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		ns = kernfs_info(dentry->d_sb)->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	     pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	     pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		const char *name = pos->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		unsigned int type = dt_type(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		int len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		ino_t ino = kernfs_ino(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		ctx->pos = pos->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		file->private_data = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		kernfs_get(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		if (!dir_emit(ctx, name, len, ino, type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	ctx->pos = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) const struct file_operations kernfs_dir_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	.iterate_shared	= kernfs_fop_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	.release	= kernfs_dir_fop_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) };