155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b8441ed2STejun Heo /*
3b8441ed2STejun Heo * fs/kernfs/dir.c - kernfs directory implementation
4b8441ed2STejun Heo *
5b8441ed2STejun Heo * Copyright (c) 2001-3 Patrick Mochel
6b8441ed2STejun Heo * Copyright (c) 2007 SUSE Linux Products GmbH
7b8441ed2STejun Heo * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8b8441ed2STejun Heo */
9fd7b9f7bSTejun Heo
10abd54f02STejun Heo #include <linux/sched.h>
11fd7b9f7bSTejun Heo #include <linux/fs.h>
12fd7b9f7bSTejun Heo #include <linux/namei.h>
13fd7b9f7bSTejun Heo #include <linux/idr.h>
14fd7b9f7bSTejun Heo #include <linux/slab.h>
15fd7b9f7bSTejun Heo #include <linux/security.h>
16fd7b9f7bSTejun Heo #include <linux/hash.h>
17fd7b9f7bSTejun Heo
18fd7b9f7bSTejun Heo #include "kernfs-internal.h"
19fd7b9f7bSTejun Heo
2006fb4736SImran Khan static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */
211a702dc8SHao Luo /*
221a702dc8SHao Luo * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to
231a702dc8SHao Luo * call pr_cont() while holding rename_lock. Because sometimes pr_cont()
241a702dc8SHao Luo * will perform wakeups when releasing console_sem. Holding rename_lock
251a702dc8SHao Luo * will introduce deadlock if the scheduler reads the kernfs_name in the
261a702dc8SHao Luo * wakeup path.
271a702dc8SHao Luo */
281a702dc8SHao Luo static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
291a702dc8SHao Luo static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */
305f93225dSTejun Heo static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */
31fd7b9f7bSTejun Heo
32adc5e8b5STejun Heo #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
33fd7b9f7bSTejun Heo
__kernfs_active(struct kernfs_node * kn)341edfe4eaSTejun Heo static bool __kernfs_active(struct kernfs_node *kn)
351edfe4eaSTejun Heo {
361edfe4eaSTejun Heo return atomic_read(&kn->active) >= 0;
371edfe4eaSTejun Heo }
381edfe4eaSTejun Heo
kernfs_active(struct kernfs_node * kn)3981c173cbSTejun Heo static bool kernfs_active(struct kernfs_node *kn)
4081c173cbSTejun Heo {
41393c3714SMinchan Kim lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
421edfe4eaSTejun Heo return __kernfs_active(kn);
4381c173cbSTejun Heo }
4481c173cbSTejun Heo
kernfs_lockdep(struct kernfs_node * kn)45182fd64bSTejun Heo static bool kernfs_lockdep(struct kernfs_node *kn)
46182fd64bSTejun Heo {
47182fd64bSTejun Heo #ifdef CONFIG_DEBUG_LOCK_ALLOC
48182fd64bSTejun Heo return kn->flags & KERNFS_LOCKDEP;
49182fd64bSTejun Heo #else
50182fd64bSTejun Heo return false;
51182fd64bSTejun Heo #endif
52182fd64bSTejun Heo }
53182fd64bSTejun Heo
kernfs_name_locked(struct kernfs_node * kn,char * buf,size_t buflen)543eef34adSTejun Heo static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
553eef34adSTejun Heo {
5617627157SKonstantin Khlebnikov if (!kn)
5717627157SKonstantin Khlebnikov return strlcpy(buf, "(null)", buflen);
5817627157SKonstantin Khlebnikov
593eef34adSTejun Heo return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
603eef34adSTejun Heo }
613eef34adSTejun Heo
629f6df573SAditya Kali /* kernfs_node_depth - compute depth from @from to @to */
kernfs_depth(struct kernfs_node * from,struct kernfs_node * to)639f6df573SAditya Kali static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
643eef34adSTejun Heo {
659f6df573SAditya Kali size_t depth = 0;
663eef34adSTejun Heo
679f6df573SAditya Kali while (to->parent && to != from) {
689f6df573SAditya Kali depth++;
699f6df573SAditya Kali to = to->parent;
703eef34adSTejun Heo }
719f6df573SAditya Kali return depth;
729f6df573SAditya Kali }
733eef34adSTejun Heo
kernfs_common_ancestor(struct kernfs_node * a,struct kernfs_node * b)749f6df573SAditya Kali static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
759f6df573SAditya Kali struct kernfs_node *b)
769f6df573SAditya Kali {
779f6df573SAditya Kali size_t da, db;
789f6df573SAditya Kali struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
799f6df573SAditya Kali
809f6df573SAditya Kali if (ra != rb)
819f6df573SAditya Kali return NULL;
829f6df573SAditya Kali
839f6df573SAditya Kali da = kernfs_depth(ra->kn, a);
849f6df573SAditya Kali db = kernfs_depth(rb->kn, b);
859f6df573SAditya Kali
869f6df573SAditya Kali while (da > db) {
879f6df573SAditya Kali a = a->parent;
889f6df573SAditya Kali da--;
899f6df573SAditya Kali }
909f6df573SAditya Kali while (db > da) {
919f6df573SAditya Kali b = b->parent;
929f6df573SAditya Kali db--;
939f6df573SAditya Kali }
949f6df573SAditya Kali
959f6df573SAditya Kali /* worst case b and a will be the same at root */
969f6df573SAditya Kali while (b != a) {
979f6df573SAditya Kali b = b->parent;
989f6df573SAditya Kali a = a->parent;
999f6df573SAditya Kali }
1009f6df573SAditya Kali
1019f6df573SAditya Kali return a;
1029f6df573SAditya Kali }
1039f6df573SAditya Kali
1049f6df573SAditya Kali /**
1059f6df573SAditya Kali * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
1069f6df573SAditya Kali * where kn_from is treated as root of the path.
1079f6df573SAditya Kali * @kn_from: kernfs node which should be treated as root for the path
1089f6df573SAditya Kali * @kn_to: kernfs node to which path is needed
1099f6df573SAditya Kali * @buf: buffer to copy the path into
1109f6df573SAditya Kali * @buflen: size of @buf
1119f6df573SAditya Kali *
1129f6df573SAditya Kali * We need to handle couple of scenarios here:
1139f6df573SAditya Kali * [1] when @kn_from is an ancestor of @kn_to at some level
1149f6df573SAditya Kali * kn_from: /n1/n2/n3
1159f6df573SAditya Kali * kn_to: /n1/n2/n3/n4/n5
1169f6df573SAditya Kali * result: /n4/n5
1179f6df573SAditya Kali *
1189f6df573SAditya Kali * [2] when @kn_from is on a different hierarchy and we need to find common
1199f6df573SAditya Kali * ancestor between @kn_from and @kn_to.
1209f6df573SAditya Kali * kn_from: /n1/n2/n3/n4
1219f6df573SAditya Kali * kn_to: /n1/n2/n5
1229f6df573SAditya Kali * result: /../../n5
1239f6df573SAditya Kali * OR
1249f6df573SAditya Kali * kn_from: /n1/n2/n3/n4/n5 [depth=5]
1259f6df573SAditya Kali * kn_to: /n1/n2/n3 [depth=3]
1269f6df573SAditya Kali * result: /../..
1279f6df573SAditya Kali *
12824b3e3ddSRandy Dunlap * [3] when @kn_to is %NULL result will be "(null)"
12917627157SKonstantin Khlebnikov *
130*aea95c68SKees Cook * Return: the length of the constructed path. If the path would have been
1313abb1d90STejun Heo * greater than @buflen, @buf contains the truncated path with the trailing
1323abb1d90STejun Heo * '\0'. On error, -errno is returned.
1339f6df573SAditya Kali */
kernfs_path_from_node_locked(struct kernfs_node * kn_to,struct kernfs_node * kn_from,char * buf,size_t buflen)1349f6df573SAditya Kali static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
1359f6df573SAditya Kali struct kernfs_node *kn_from,
1369f6df573SAditya Kali char *buf, size_t buflen)
1379f6df573SAditya Kali {
1389f6df573SAditya Kali struct kernfs_node *kn, *common;
1399f6df573SAditya Kali const char parent_str[] = "/..";
1403abb1d90STejun Heo size_t depth_from, depth_to, len = 0;
141*aea95c68SKees Cook ssize_t copied;
1423abb1d90STejun Heo int i, j;
1439f6df573SAditya Kali
14417627157SKonstantin Khlebnikov if (!kn_to)
145*aea95c68SKees Cook return strscpy(buf, "(null)", buflen);
14617627157SKonstantin Khlebnikov
1479f6df573SAditya Kali if (!kn_from)
1489f6df573SAditya Kali kn_from = kernfs_root(kn_to)->kn;
1499f6df573SAditya Kali
1509f6df573SAditya Kali if (kn_from == kn_to)
151*aea95c68SKees Cook return strscpy(buf, "/", buflen);
1529f6df573SAditya Kali
1539f6df573SAditya Kali common = kernfs_common_ancestor(kn_from, kn_to);
1549f6df573SAditya Kali if (WARN_ON(!common))
1553abb1d90STejun Heo return -EINVAL;
1569f6df573SAditya Kali
1579f6df573SAditya Kali depth_to = kernfs_depth(common, kn_to);
1589f6df573SAditya Kali depth_from = kernfs_depth(common, kn_from);
1599f6df573SAditya Kali
1609f6df573SAditya Kali buf[0] = '\0';
1619f6df573SAditya Kali
162*aea95c68SKees Cook for (i = 0; i < depth_from; i++) {
163*aea95c68SKees Cook copied = strscpy(buf + len, parent_str, buflen - len);
164*aea95c68SKees Cook if (copied < 0)
165*aea95c68SKees Cook return copied;
166*aea95c68SKees Cook len += copied;
167*aea95c68SKees Cook }
1689f6df573SAditya Kali
1699f6df573SAditya Kali /* Calculate how many bytes we need for the rest */
1703abb1d90STejun Heo for (i = depth_to - 1; i >= 0; i--) {
1713abb1d90STejun Heo for (kn = kn_to, j = 0; j < i; j++)
1723abb1d90STejun Heo kn = kn->parent;
173*aea95c68SKees Cook
174*aea95c68SKees Cook len += scnprintf(buf + len, buflen - len, "/%s", kn->name);
1759f6df573SAditya Kali }
1769f6df573SAditya Kali
1773abb1d90STejun Heo return len;
1783eef34adSTejun Heo }
1793eef34adSTejun Heo
1803eef34adSTejun Heo /**
1813eef34adSTejun Heo * kernfs_name - obtain the name of a given node
1823eef34adSTejun Heo * @kn: kernfs_node of interest
1833eef34adSTejun Heo * @buf: buffer to copy @kn's name into
1843eef34adSTejun Heo * @buflen: size of @buf
1853eef34adSTejun Heo *
1863eef34adSTejun Heo * Copies the name of @kn into @buf of @buflen bytes. The behavior is
18724b3e3ddSRandy Dunlap * similar to strlcpy().
1883eef34adSTejun Heo *
18924b3e3ddSRandy Dunlap * Fills buffer with "(null)" if @kn is %NULL.
19024b3e3ddSRandy Dunlap *
19124b3e3ddSRandy Dunlap * Return: the length of @kn's name and if @buf isn't long enough,
19224b3e3ddSRandy Dunlap * it's filled up to @buflen-1 and nul terminated.
19317627157SKonstantin Khlebnikov *
1943eef34adSTejun Heo * This function can be called from any context.
1953eef34adSTejun Heo */
kernfs_name(struct kernfs_node * kn,char * buf,size_t buflen)1963eef34adSTejun Heo int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
1973eef34adSTejun Heo {
1983eef34adSTejun Heo unsigned long flags;
1993eef34adSTejun Heo int ret;
2003eef34adSTejun Heo
20106fb4736SImran Khan read_lock_irqsave(&kernfs_rename_lock, flags);
2023eef34adSTejun Heo ret = kernfs_name_locked(kn, buf, buflen);
20306fb4736SImran Khan read_unlock_irqrestore(&kernfs_rename_lock, flags);
2043eef34adSTejun Heo return ret;
2053eef34adSTejun Heo }
2063eef34adSTejun Heo
2073eef34adSTejun Heo /**
2089f6df573SAditya Kali * kernfs_path_from_node - build path of node @to relative to @from.
2099f6df573SAditya Kali * @from: parent kernfs_node relative to which we need to build the path
2109f6df573SAditya Kali * @to: kernfs_node of interest
2119f6df573SAditya Kali * @buf: buffer to copy @to's path into
2129f6df573SAditya Kali * @buflen: size of @buf
2139f6df573SAditya Kali *
2149f6df573SAditya Kali * Builds @to's path relative to @from in @buf. @from and @to must
2159f6df573SAditya Kali * be on the same kernfs-root. If @from is not parent of @to, then a relative
2169f6df573SAditya Kali * path (which includes '..'s) as needed to reach from @from to @to is
2179f6df573SAditya Kali * returned.
2189f6df573SAditya Kali *
219*aea95c68SKees Cook * Return: the length of the constructed path. If the path would have been
2203abb1d90STejun Heo * greater than @buflen, @buf contains the truncated path with the trailing
2213abb1d90STejun Heo * '\0'. On error, -errno is returned.
2229f6df573SAditya Kali */
kernfs_path_from_node(struct kernfs_node * to,struct kernfs_node * from,char * buf,size_t buflen)2239f6df573SAditya Kali int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
2249f6df573SAditya Kali char *buf, size_t buflen)
2259f6df573SAditya Kali {
2269f6df573SAditya Kali unsigned long flags;
2279f6df573SAditya Kali int ret;
2289f6df573SAditya Kali
22906fb4736SImran Khan read_lock_irqsave(&kernfs_rename_lock, flags);
2309f6df573SAditya Kali ret = kernfs_path_from_node_locked(to, from, buf, buflen);
23106fb4736SImran Khan read_unlock_irqrestore(&kernfs_rename_lock, flags);
2329f6df573SAditya Kali return ret;
2339f6df573SAditya Kali }
2349f6df573SAditya Kali EXPORT_SYMBOL_GPL(kernfs_path_from_node);
2359f6df573SAditya Kali
2369f6df573SAditya Kali /**
2373eef34adSTejun Heo * pr_cont_kernfs_name - pr_cont name of a kernfs_node
2383eef34adSTejun Heo * @kn: kernfs_node of interest
2393eef34adSTejun Heo *
2403eef34adSTejun Heo * This function can be called from any context.
2413eef34adSTejun Heo */
pr_cont_kernfs_name(struct kernfs_node * kn)2423eef34adSTejun Heo void pr_cont_kernfs_name(struct kernfs_node *kn)
2433eef34adSTejun Heo {
2443eef34adSTejun Heo unsigned long flags;
2453eef34adSTejun Heo
2461a702dc8SHao Luo spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
2473eef34adSTejun Heo
2481a702dc8SHao Luo kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
2493eef34adSTejun Heo pr_cont("%s", kernfs_pr_cont_buf);
2503eef34adSTejun Heo
2511a702dc8SHao Luo spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
2523eef34adSTejun Heo }
2533eef34adSTejun Heo
2543eef34adSTejun Heo /**
2553eef34adSTejun Heo * pr_cont_kernfs_path - pr_cont path of a kernfs_node
2563eef34adSTejun Heo * @kn: kernfs_node of interest
2573eef34adSTejun Heo *
2583eef34adSTejun Heo * This function can be called from any context.
2593eef34adSTejun Heo */
pr_cont_kernfs_path(struct kernfs_node * kn)2603eef34adSTejun Heo void pr_cont_kernfs_path(struct kernfs_node *kn)
2613eef34adSTejun Heo {
2623eef34adSTejun Heo unsigned long flags;
2639f6df573SAditya Kali int sz;
2643eef34adSTejun Heo
2651a702dc8SHao Luo spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
2663eef34adSTejun Heo
2671a702dc8SHao Luo sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf,
2683eef34adSTejun Heo sizeof(kernfs_pr_cont_buf));
2699f6df573SAditya Kali if (sz < 0) {
270*aea95c68SKees Cook if (sz == -E2BIG)
2719f6df573SAditya Kali pr_cont("(name too long)");
272*aea95c68SKees Cook else
273*aea95c68SKees Cook pr_cont("(error)");
2749f6df573SAditya Kali goto out;
2759f6df573SAditya Kali }
2769f6df573SAditya Kali
2779f6df573SAditya Kali pr_cont("%s", kernfs_pr_cont_buf);
2789f6df573SAditya Kali
2799f6df573SAditya Kali out:
2801a702dc8SHao Luo spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
2813eef34adSTejun Heo }
2823eef34adSTejun Heo
2833eef34adSTejun Heo /**
2843eef34adSTejun Heo * kernfs_get_parent - determine the parent node and pin it
2853eef34adSTejun Heo * @kn: kernfs_node of interest
2863eef34adSTejun Heo *
2873eef34adSTejun Heo * Determines @kn's parent, pins and returns it. This function can be
2883eef34adSTejun Heo * called from any context.
28924b3e3ddSRandy Dunlap *
29024b3e3ddSRandy Dunlap * Return: parent node of @kn
2913eef34adSTejun Heo */
kernfs_get_parent(struct kernfs_node * kn)2923eef34adSTejun Heo struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
2933eef34adSTejun Heo {
2943eef34adSTejun Heo struct kernfs_node *parent;
2953eef34adSTejun Heo unsigned long flags;
2963eef34adSTejun Heo
29706fb4736SImran Khan read_lock_irqsave(&kernfs_rename_lock, flags);
2983eef34adSTejun Heo parent = kn->parent;
2993eef34adSTejun Heo kernfs_get(parent);
30006fb4736SImran Khan read_unlock_irqrestore(&kernfs_rename_lock, flags);
3013eef34adSTejun Heo
3023eef34adSTejun Heo return parent;
3033eef34adSTejun Heo }
3043eef34adSTejun Heo
305fd7b9f7bSTejun Heo /**
30624b3e3ddSRandy Dunlap * kernfs_name_hash - calculate hash of @ns + @name
307fd7b9f7bSTejun Heo * @name: Null terminated string to hash
308fd7b9f7bSTejun Heo * @ns: Namespace tag to hash
309fd7b9f7bSTejun Heo *
31024b3e3ddSRandy Dunlap * Return: 31-bit hash of ns + name (so it fits in an off_t)
311fd7b9f7bSTejun Heo */
kernfs_name_hash(const char * name,const void * ns)312c637b8acSTejun Heo static unsigned int kernfs_name_hash(const char *name, const void *ns)
313fd7b9f7bSTejun Heo {
3148387ff25SLinus Torvalds unsigned long hash = init_name_hash(ns);
315fd7b9f7bSTejun Heo unsigned int len = strlen(name);
316fd7b9f7bSTejun Heo while (len--)
317fd7b9f7bSTejun Heo hash = partial_name_hash(*name++, hash);
3188387ff25SLinus Torvalds hash = end_name_hash(hash);
319fd7b9f7bSTejun Heo hash &= 0x7fffffffU;
320fd7b9f7bSTejun Heo /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
32188391d49SRichard Cochran if (hash < 2)
322fd7b9f7bSTejun Heo hash += 2;
323fd7b9f7bSTejun Heo if (hash >= INT_MAX)
324fd7b9f7bSTejun Heo hash = INT_MAX - 1;
325fd7b9f7bSTejun Heo return hash;
326fd7b9f7bSTejun Heo }
327fd7b9f7bSTejun Heo
kernfs_name_compare(unsigned int hash,const char * name,const void * ns,const struct kernfs_node * kn)328c637b8acSTejun Heo static int kernfs_name_compare(unsigned int hash, const char *name,
329324a56e1STejun Heo const void *ns, const struct kernfs_node *kn)
330fd7b9f7bSTejun Heo {
33172392ed0SRasmus Villemoes if (hash < kn->hash)
33272392ed0SRasmus Villemoes return -1;
33372392ed0SRasmus Villemoes if (hash > kn->hash)
33472392ed0SRasmus Villemoes return 1;
33572392ed0SRasmus Villemoes if (ns < kn->ns)
33672392ed0SRasmus Villemoes return -1;
33772392ed0SRasmus Villemoes if (ns > kn->ns)
33872392ed0SRasmus Villemoes return 1;
339adc5e8b5STejun Heo return strcmp(name, kn->name);
340fd7b9f7bSTejun Heo }
341fd7b9f7bSTejun Heo
kernfs_sd_compare(const struct kernfs_node * left,const struct kernfs_node * right)342c637b8acSTejun Heo static int kernfs_sd_compare(const struct kernfs_node *left,
343324a56e1STejun Heo const struct kernfs_node *right)
344fd7b9f7bSTejun Heo {
345c637b8acSTejun Heo return kernfs_name_compare(left->hash, left->name, left->ns, right);
346fd7b9f7bSTejun Heo }
347fd7b9f7bSTejun Heo
348fd7b9f7bSTejun Heo /**
349c637b8acSTejun Heo * kernfs_link_sibling - link kernfs_node into sibling rbtree
350324a56e1STejun Heo * @kn: kernfs_node of interest
351fd7b9f7bSTejun Heo *
352324a56e1STejun Heo * Link @kn into its sibling rbtree which starts from
353adc5e8b5STejun Heo * @kn->parent->dir.children.
354fd7b9f7bSTejun Heo *
355fd7b9f7bSTejun Heo * Locking:
3567ba0273bSIan Kent * kernfs_rwsem held exclusive
357fd7b9f7bSTejun Heo *
35824b3e3ddSRandy Dunlap * Return:
35924b3e3ddSRandy Dunlap * %0 on success, -EEXIST on failure.
360fd7b9f7bSTejun Heo */
kernfs_link_sibling(struct kernfs_node * kn)361c637b8acSTejun Heo static int kernfs_link_sibling(struct kernfs_node *kn)
362fd7b9f7bSTejun Heo {
363adc5e8b5STejun Heo struct rb_node **node = &kn->parent->dir.children.rb_node;
364fd7b9f7bSTejun Heo struct rb_node *parent = NULL;
365fd7b9f7bSTejun Heo
366fd7b9f7bSTejun Heo while (*node) {
367324a56e1STejun Heo struct kernfs_node *pos;
368fd7b9f7bSTejun Heo int result;
369fd7b9f7bSTejun Heo
370324a56e1STejun Heo pos = rb_to_kn(*node);
371fd7b9f7bSTejun Heo parent = *node;
372c637b8acSTejun Heo result = kernfs_sd_compare(kn, pos);
373fd7b9f7bSTejun Heo if (result < 0)
374adc5e8b5STejun Heo node = &pos->rb.rb_left;
375fd7b9f7bSTejun Heo else if (result > 0)
376adc5e8b5STejun Heo node = &pos->rb.rb_right;
377fd7b9f7bSTejun Heo else
378fd7b9f7bSTejun Heo return -EEXIST;
379fd7b9f7bSTejun Heo }
380c1befb88SJianyu Zhan
381fd7b9f7bSTejun Heo /* add new node and rebalance the tree */
382adc5e8b5STejun Heo rb_link_node(&kn->rb, parent, node);
383adc5e8b5STejun Heo rb_insert_color(&kn->rb, &kn->parent->dir.children);
384c1befb88SJianyu Zhan
385c1befb88SJianyu Zhan /* successfully added, account subdir number */
3860559f630SIan Kent down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
387c1befb88SJianyu Zhan if (kernfs_type(kn) == KERNFS_DIR)
388c1befb88SJianyu Zhan kn->parent->dir.subdirs++;
389895adbecSIan Kent kernfs_inc_rev(kn->parent);
3900559f630SIan Kent up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
391c1befb88SJianyu Zhan
392fd7b9f7bSTejun Heo return 0;
393fd7b9f7bSTejun Heo }
394fd7b9f7bSTejun Heo
395fd7b9f7bSTejun Heo /**
396c637b8acSTejun Heo * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
397324a56e1STejun Heo * @kn: kernfs_node of interest
398fd7b9f7bSTejun Heo *
39935beab06STejun Heo * Try to unlink @kn from its sibling rbtree which starts from
40024b3e3ddSRandy Dunlap * kn->parent->dir.children.
40124b3e3ddSRandy Dunlap *
40224b3e3ddSRandy Dunlap * Return: %true if @kn was actually removed,
40324b3e3ddSRandy Dunlap * %false if @kn wasn't on the rbtree.
404fd7b9f7bSTejun Heo *
405fd7b9f7bSTejun Heo * Locking:
4067ba0273bSIan Kent * kernfs_rwsem held exclusive
407fd7b9f7bSTejun Heo */
kernfs_unlink_sibling(struct kernfs_node * kn)40835beab06STejun Heo static bool kernfs_unlink_sibling(struct kernfs_node *kn)
409fd7b9f7bSTejun Heo {
41035beab06STejun Heo if (RB_EMPTY_NODE(&kn->rb))
41135beab06STejun Heo return false;
41235beab06STejun Heo
4130559f630SIan Kent down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
414df23fc39STejun Heo if (kernfs_type(kn) == KERNFS_DIR)
415adc5e8b5STejun Heo kn->parent->dir.subdirs--;
416895adbecSIan Kent kernfs_inc_rev(kn->parent);
4170559f630SIan Kent up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
418fd7b9f7bSTejun Heo
419adc5e8b5STejun Heo rb_erase(&kn->rb, &kn->parent->dir.children);
42035beab06STejun Heo RB_CLEAR_NODE(&kn->rb);
42135beab06STejun Heo return true;
422fd7b9f7bSTejun Heo }
423fd7b9f7bSTejun Heo
424fd7b9f7bSTejun Heo /**
425c637b8acSTejun Heo * kernfs_get_active - get an active reference to kernfs_node
426324a56e1STejun Heo * @kn: kernfs_node to get an active reference to
427fd7b9f7bSTejun Heo *
428324a56e1STejun Heo * Get an active reference of @kn. This function is noop if @kn
42924b3e3ddSRandy Dunlap * is %NULL.
430fd7b9f7bSTejun Heo *
43124b3e3ddSRandy Dunlap * Return:
43224b3e3ddSRandy Dunlap * Pointer to @kn on success, %NULL on failure.
433fd7b9f7bSTejun Heo */
kernfs_get_active(struct kernfs_node * kn)434c637b8acSTejun Heo struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
435fd7b9f7bSTejun Heo {
436324a56e1STejun Heo if (unlikely(!kn))
437fd7b9f7bSTejun Heo return NULL;
438fd7b9f7bSTejun Heo
439f4b3e631SGreg Kroah-Hartman if (!atomic_inc_unless_negative(&kn->active))
440f4b3e631SGreg Kroah-Hartman return NULL;
441f4b3e631SGreg Kroah-Hartman
442182fd64bSTejun Heo if (kernfs_lockdep(kn))
443324a56e1STejun Heo rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
444324a56e1STejun Heo return kn;
445fd7b9f7bSTejun Heo }
446fd7b9f7bSTejun Heo
447fd7b9f7bSTejun Heo /**
448c637b8acSTejun Heo * kernfs_put_active - put an active reference to kernfs_node
449324a56e1STejun Heo * @kn: kernfs_node to put an active reference to
450fd7b9f7bSTejun Heo *
451324a56e1STejun Heo * Put an active reference to @kn. This function is noop if @kn
45224b3e3ddSRandy Dunlap * is %NULL.
453fd7b9f7bSTejun Heo */
kernfs_put_active(struct kernfs_node * kn)454c637b8acSTejun Heo void kernfs_put_active(struct kernfs_node *kn)
455fd7b9f7bSTejun Heo {
456fd7b9f7bSTejun Heo int v;
457fd7b9f7bSTejun Heo
458324a56e1STejun Heo if (unlikely(!kn))
459fd7b9f7bSTejun Heo return;
460fd7b9f7bSTejun Heo
461182fd64bSTejun Heo if (kernfs_lockdep(kn))
4625facae4fSQian Cai rwsem_release(&kn->dep_map, _RET_IP_);
463adc5e8b5STejun Heo v = atomic_dec_return(&kn->active);
464df23fc39STejun Heo if (likely(v != KN_DEACTIVATED_BIAS))
465fd7b9f7bSTejun Heo return;
466fd7b9f7bSTejun Heo
4672fd60da4SPeng Wang wake_up_all(&kernfs_root(kn)->deactivate_waitq);
468fd7b9f7bSTejun Heo }
469fd7b9f7bSTejun Heo
470fd7b9f7bSTejun Heo /**
47181c173cbSTejun Heo * kernfs_drain - drain kernfs_node
47281c173cbSTejun Heo * @kn: kernfs_node to drain
473fd7b9f7bSTejun Heo *
47424b3e3ddSRandy Dunlap * Drain existing usages and nuke all existing mmaps of @kn. Multiple
47581c173cbSTejun Heo * removers may invoke this function concurrently on @kn and all will
47681c173cbSTejun Heo * return after draining is complete.
477fd7b9f7bSTejun Heo */
kernfs_drain(struct kernfs_node * kn)47881c173cbSTejun Heo static void kernfs_drain(struct kernfs_node *kn)
479393c3714SMinchan Kim __releases(&kernfs_root(kn)->kernfs_rwsem)
480393c3714SMinchan Kim __acquires(&kernfs_root(kn)->kernfs_rwsem)
481fd7b9f7bSTejun Heo {
482abd54f02STejun Heo struct kernfs_root *root = kernfs_root(kn);
483fd7b9f7bSTejun Heo
484393c3714SMinchan Kim lockdep_assert_held_write(&root->kernfs_rwsem);
48581c173cbSTejun Heo WARN_ON_ONCE(kernfs_active(kn));
486abd54f02STejun Heo
4872d7f9f8cSTejun Heo /*
4882d7f9f8cSTejun Heo * Skip draining if already fully drained. This avoids draining and its
4892d7f9f8cSTejun Heo * lockdep annotations for nodes which have never been activated
4902d7f9f8cSTejun Heo * allowing embedding kernfs_remove() in create error paths without
4912d7f9f8cSTejun Heo * worrying about draining.
4922d7f9f8cSTejun Heo */
4932d7f9f8cSTejun Heo if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS &&
4942d7f9f8cSTejun Heo !kernfs_should_drain_open_files(kn))
4952d7f9f8cSTejun Heo return;
4962d7f9f8cSTejun Heo
497393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
498abd54f02STejun Heo
499182fd64bSTejun Heo if (kernfs_lockdep(kn)) {
50035beab06STejun Heo rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
50135beab06STejun Heo if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
50235beab06STejun Heo lock_contended(&kn->dep_map, _RET_IP_);
50335beab06STejun Heo }
50435beab06STejun Heo
505abd54f02STejun Heo wait_event(root->deactivate_waitq,
506abd54f02STejun Heo atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
507fd7b9f7bSTejun Heo
508182fd64bSTejun Heo if (kernfs_lockdep(kn)) {
509324a56e1STejun Heo lock_acquired(&kn->dep_map, _RET_IP_);
5105facae4fSQian Cai rwsem_release(&kn->dep_map, _RET_IP_);
511fd7b9f7bSTejun Heo }
51235beab06STejun Heo
513bdb2fd7fSTejun Heo if (kernfs_should_drain_open_files(kn))
5140e67db2fSTejun Heo kernfs_drain_open_files(kn);
515ccf02aafSTejun Heo
516393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
517a6607930STejun Heo }
518fd7b9f7bSTejun Heo
519fd7b9f7bSTejun Heo /**
520324a56e1STejun Heo * kernfs_get - get a reference count on a kernfs_node
521324a56e1STejun Heo * @kn: the target kernfs_node
522fd7b9f7bSTejun Heo */
kernfs_get(struct kernfs_node * kn)523324a56e1STejun Heo void kernfs_get(struct kernfs_node *kn)
524fd7b9f7bSTejun Heo {
525324a56e1STejun Heo if (kn) {
526adc5e8b5STejun Heo WARN_ON(!atomic_read(&kn->count));
527adc5e8b5STejun Heo atomic_inc(&kn->count);
528fd7b9f7bSTejun Heo }
529fd7b9f7bSTejun Heo }
530fd7b9f7bSTejun Heo EXPORT_SYMBOL_GPL(kernfs_get);
531fd7b9f7bSTejun Heo
kernfs_free_rcu(struct rcu_head * rcu)5324b1f991bSTejun Heo static void kernfs_free_rcu(struct rcu_head *rcu)
5334b1f991bSTejun Heo {
5344b1f991bSTejun Heo struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
5354b1f991bSTejun Heo
5364b1f991bSTejun Heo kfree_const(kn->name);
5374b1f991bSTejun Heo
5384b1f991bSTejun Heo if (kn->iattr) {
5394b1f991bSTejun Heo simple_xattrs_free(&kn->iattr->xattrs, NULL);
5404b1f991bSTejun Heo kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
5414b1f991bSTejun Heo }
5424b1f991bSTejun Heo
5434b1f991bSTejun Heo kmem_cache_free(kernfs_node_cache, kn);
5444b1f991bSTejun Heo }
5454b1f991bSTejun Heo
546fd7b9f7bSTejun Heo /**
547324a56e1STejun Heo * kernfs_put - put a reference count on a kernfs_node
548324a56e1STejun Heo * @kn: the target kernfs_node
549fd7b9f7bSTejun Heo *
550324a56e1STejun Heo * Put a reference count of @kn and destroy it if it reached zero.
551fd7b9f7bSTejun Heo */
kernfs_put(struct kernfs_node * kn)552324a56e1STejun Heo void kernfs_put(struct kernfs_node *kn)
553fd7b9f7bSTejun Heo {
554324a56e1STejun Heo struct kernfs_node *parent;
555ba7443bcSTejun Heo struct kernfs_root *root;
556fd7b9f7bSTejun Heo
557adc5e8b5STejun Heo if (!kn || !atomic_dec_and_test(&kn->count))
558fd7b9f7bSTejun Heo return;
559324a56e1STejun Heo root = kernfs_root(kn);
560fd7b9f7bSTejun Heo repeat:
56181c173cbSTejun Heo /*
56281c173cbSTejun Heo * Moving/renaming is always done while holding reference.
563adc5e8b5STejun Heo * kn->parent won't change beneath us.
564fd7b9f7bSTejun Heo */
565adc5e8b5STejun Heo parent = kn->parent;
566fd7b9f7bSTejun Heo
56781c173cbSTejun Heo WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
56881c173cbSTejun Heo "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
56981c173cbSTejun Heo parent ? parent->name : "", kn->name, atomic_read(&kn->active));
570fd7b9f7bSTejun Heo
571df23fc39STejun Heo if (kernfs_type(kn) == KERNFS_LINK)
572adc5e8b5STejun Heo kernfs_put(kn->symlink.target_kn);
573dfeb0750STejun Heo
5745f93225dSTejun Heo spin_lock(&kernfs_idr_lock);
57540430452STejun Heo idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
5765f93225dSTejun Heo spin_unlock(&kernfs_idr_lock);
5774b1f991bSTejun Heo
5784b1f991bSTejun Heo call_rcu(&kn->rcu, kernfs_free_rcu);
579fd7b9f7bSTejun Heo
580324a56e1STejun Heo kn = parent;
581324a56e1STejun Heo if (kn) {
582adc5e8b5STejun Heo if (atomic_dec_and_test(&kn->count))
583fd7b9f7bSTejun Heo goto repeat;
584ba7443bcSTejun Heo } else {
585324a56e1STejun Heo /* just released the root kn, free @root too */
5867d35079fSShaohua Li idr_destroy(&root->ino_idr);
5874b1f991bSTejun Heo kfree_rcu(root, rcu);
588ba7443bcSTejun Heo }
589fd7b9f7bSTejun Heo }
590fd7b9f7bSTejun Heo EXPORT_SYMBOL_GPL(kernfs_put);
591fd7b9f7bSTejun Heo
5920c23b225STejun Heo /**
5930c23b225STejun Heo * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
5940c23b225STejun Heo * @dentry: the dentry in question
5950c23b225STejun Heo *
59624b3e3ddSRandy Dunlap * Return: the kernfs_node associated with @dentry. If @dentry is not a
5970c23b225STejun Heo * kernfs one, %NULL is returned.
5980c23b225STejun Heo *
5990c23b225STejun Heo * While the returned kernfs_node will stay accessible as long as @dentry
6000c23b225STejun Heo * is accessible, the returned node can be in any state and the caller is
6010c23b225STejun Heo * fully responsible for determining what's accessible.
6020c23b225STejun Heo */
kernfs_node_from_dentry(struct dentry * dentry)6030c23b225STejun Heo struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
6040c23b225STejun Heo {
6050288e7faSHui Su if (dentry->d_sb->s_op == &kernfs_sops)
606319ba91dSShaohua Li return kernfs_dentry_node(dentry);
6070c23b225STejun Heo return NULL;
6080c23b225STejun Heo }
6090c23b225STejun Heo
__kernfs_new_node(struct kernfs_root * root,struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,unsigned flags)610db4aad20STejun Heo static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
611e19dfdc8SOndrej Mosnacek struct kernfs_node *parent,
612db4aad20STejun Heo const char *name, umode_t mode,
613488dee96SDmitry Torokhov kuid_t uid, kgid_t gid,
614db4aad20STejun Heo unsigned flags)
615fd7b9f7bSTejun Heo {
616324a56e1STejun Heo struct kernfs_node *kn;
61740430452STejun Heo u32 id_highbits;
618bc755553STejun Heo int ret;
619fd7b9f7bSTejun Heo
620dfeb0750STejun Heo name = kstrdup_const(name, GFP_KERNEL);
621fd7b9f7bSTejun Heo if (!name)
622fd7b9f7bSTejun Heo return NULL;
623fd7b9f7bSTejun Heo
624a797bfc3STejun Heo kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
625324a56e1STejun Heo if (!kn)
626fd7b9f7bSTejun Heo goto err_out1;
627fd7b9f7bSTejun Heo
6287d35079fSShaohua Li idr_preload(GFP_KERNEL);
6295f93225dSTejun Heo spin_lock(&kernfs_idr_lock);
6304a3ef68aSShaohua Li ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
63140430452STejun Heo if (ret >= 0 && ret < root->last_id_lowbits)
63240430452STejun Heo root->id_highbits++;
63340430452STejun Heo id_highbits = root->id_highbits;
63440430452STejun Heo root->last_id_lowbits = ret;
6355f93225dSTejun Heo spin_unlock(&kernfs_idr_lock);
6367d35079fSShaohua Li idr_preload_end();
637bc755553STejun Heo if (ret < 0)
638fd7b9f7bSTejun Heo goto err_out2;
63967c0496eSTejun Heo
64040430452STejun Heo kn->id = (u64)id_highbits << 32 | ret;
641fd7b9f7bSTejun Heo
642b680b081STejun Heo atomic_set(&kn->count, 1);
64381c173cbSTejun Heo atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
64435beab06STejun Heo RB_CLEAR_NODE(&kn->rb);
645fd7b9f7bSTejun Heo
646adc5e8b5STejun Heo kn->name = name;
647adc5e8b5STejun Heo kn->mode = mode;
64881c173cbSTejun Heo kn->flags = flags;
649fd7b9f7bSTejun Heo
650488dee96SDmitry Torokhov if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
651488dee96SDmitry Torokhov struct iattr iattr = {
652488dee96SDmitry Torokhov .ia_valid = ATTR_UID | ATTR_GID,
653488dee96SDmitry Torokhov .ia_uid = uid,
654488dee96SDmitry Torokhov .ia_gid = gid,
655488dee96SDmitry Torokhov };
656488dee96SDmitry Torokhov
657488dee96SDmitry Torokhov ret = __kernfs_setattr(kn, &iattr);
658488dee96SDmitry Torokhov if (ret < 0)
659488dee96SDmitry Torokhov goto err_out3;
660488dee96SDmitry Torokhov }
661488dee96SDmitry Torokhov
662e19dfdc8SOndrej Mosnacek if (parent) {
663e19dfdc8SOndrej Mosnacek ret = security_kernfs_init_security(parent, kn);
664e19dfdc8SOndrej Mosnacek if (ret)
665e19dfdc8SOndrej Mosnacek goto err_out3;
666e19dfdc8SOndrej Mosnacek }
667e19dfdc8SOndrej Mosnacek
668324a56e1STejun Heo return kn;
669fd7b9f7bSTejun Heo
670488dee96SDmitry Torokhov err_out3:
6715f93225dSTejun Heo spin_lock(&kernfs_idr_lock);
67240430452STejun Heo idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
6735f93225dSTejun Heo spin_unlock(&kernfs_idr_lock);
674fd7b9f7bSTejun Heo err_out2:
675a797bfc3STejun Heo kmem_cache_free(kernfs_node_cache, kn);
676fd7b9f7bSTejun Heo err_out1:
677dfeb0750STejun Heo kfree_const(name);
678fd7b9f7bSTejun Heo return NULL;
679fd7b9f7bSTejun Heo }
680fd7b9f7bSTejun Heo
kernfs_new_node(struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,unsigned flags)681db4aad20STejun Heo struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
682db4aad20STejun Heo const char *name, umode_t mode,
683488dee96SDmitry Torokhov kuid_t uid, kgid_t gid,
684db4aad20STejun Heo unsigned flags)
685db4aad20STejun Heo {
686db4aad20STejun Heo struct kernfs_node *kn;
687db4aad20STejun Heo
688952d0cbdSMax Kellermann if (parent->mode & S_ISGID) {
689952d0cbdSMax Kellermann /* this code block imitates inode_init_owner() for
690952d0cbdSMax Kellermann * kernfs
691952d0cbdSMax Kellermann */
692952d0cbdSMax Kellermann
693952d0cbdSMax Kellermann if (parent->iattr)
694952d0cbdSMax Kellermann gid = parent->iattr->ia_gid;
695952d0cbdSMax Kellermann
696952d0cbdSMax Kellermann if (flags & KERNFS_DIR)
697952d0cbdSMax Kellermann mode |= S_ISGID;
698952d0cbdSMax Kellermann }
699952d0cbdSMax Kellermann
700e19dfdc8SOndrej Mosnacek kn = __kernfs_new_node(kernfs_root(parent), parent,
701488dee96SDmitry Torokhov name, mode, uid, gid, flags);
702db4aad20STejun Heo if (kn) {
703db4aad20STejun Heo kernfs_get(parent);
704db4aad20STejun Heo kn->parent = parent;
705db4aad20STejun Heo }
706db4aad20STejun Heo return kn;
707db4aad20STejun Heo }
708db4aad20STejun Heo
709ba16b284SShaohua Li /*
710fe0f726cSTejun Heo * kernfs_find_and_get_node_by_id - get kernfs_node from node id
711ba16b284SShaohua Li * @root: the kernfs root
712fe0f726cSTejun Heo * @id: the target node id
713fe0f726cSTejun Heo *
714fe0f726cSTejun Heo * @id's lower 32bits encode ino and upper gen. If the gen portion is
715fe0f726cSTejun Heo * zero, all generations are matched.
716ba16b284SShaohua Li *
71724b3e3ddSRandy Dunlap * Return: %NULL on failure,
71824b3e3ddSRandy Dunlap * otherwise a kernfs node with reference counter incremented.
719ba16b284SShaohua Li */
kernfs_find_and_get_node_by_id(struct kernfs_root * root,u64 id)720fe0f726cSTejun Heo struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
721fe0f726cSTejun Heo u64 id)
722ba16b284SShaohua Li {
723ba16b284SShaohua Li struct kernfs_node *kn;
724fe0f726cSTejun Heo ino_t ino = kernfs_id_ino(id);
725fe0f726cSTejun Heo u32 gen = kernfs_id_gen(id);
726ba16b284SShaohua Li
7274b1f991bSTejun Heo rcu_read_lock();
728b680b081STejun Heo
72940430452STejun Heo kn = idr_find(&root->ino_idr, (u32)ino);
730ba16b284SShaohua Li if (!kn)
731b680b081STejun Heo goto err_unlock;
732ba16b284SShaohua Li
73340430452STejun Heo if (sizeof(ino_t) >= sizeof(u64)) {
73440430452STejun Heo /* we looked up with the low 32bits, compare the whole */
73540430452STejun Heo if (kernfs_ino(kn) != ino)
73640430452STejun Heo goto err_unlock;
73740430452STejun Heo } else {
738fe0f726cSTejun Heo /* 0 matches all generations */
739fe0f726cSTejun Heo if (unlikely(gen && kernfs_gen(kn) != gen))
740fe0f726cSTejun Heo goto err_unlock;
74140430452STejun Heo }
742fe0f726cSTejun Heo
7431edfe4eaSTejun Heo /*
7441edfe4eaSTejun Heo * We should fail if @kn has never been activated and guarantee success
7451edfe4eaSTejun Heo * if the caller knows that @kn is active. Both can be achieved by
7461edfe4eaSTejun Heo * __kernfs_active() which tests @kn->active without kernfs_rwsem.
7471edfe4eaSTejun Heo */
7481edfe4eaSTejun Heo if (unlikely(!__kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
749b680b081STejun Heo goto err_unlock;
750ba16b284SShaohua Li
7514b1f991bSTejun Heo rcu_read_unlock();
752ba16b284SShaohua Li return kn;
753b680b081STejun Heo err_unlock:
7544b1f991bSTejun Heo rcu_read_unlock();
755ba16b284SShaohua Li return NULL;
756ba16b284SShaohua Li }
757ba16b284SShaohua Li
758fd7b9f7bSTejun Heo /**
759c637b8acSTejun Heo * kernfs_add_one - add kernfs_node to parent without warning
760324a56e1STejun Heo * @kn: kernfs_node to be added
761fd7b9f7bSTejun Heo *
762db4aad20STejun Heo * The caller must already have initialized @kn->parent. This
763db4aad20STejun Heo * function increments nlink of the parent's inode if @kn is a
764db4aad20STejun Heo * directory and link into the children list of the parent.
765fd7b9f7bSTejun Heo *
76624b3e3ddSRandy Dunlap * Return:
76724b3e3ddSRandy Dunlap * %0 on success, -EEXIST if entry with the given name already
768fd7b9f7bSTejun Heo * exists.
769fd7b9f7bSTejun Heo */
kernfs_add_one(struct kernfs_node * kn)770988cd7afSTejun Heo int kernfs_add_one(struct kernfs_node *kn)
771fd7b9f7bSTejun Heo {
772db4aad20STejun Heo struct kernfs_node *parent = kn->parent;
773393c3714SMinchan Kim struct kernfs_root *root = kernfs_root(parent);
774c525aaddSTejun Heo struct kernfs_iattrs *ps_iattr;
775988cd7afSTejun Heo bool has_ns;
776fd7b9f7bSTejun Heo int ret;
777fd7b9f7bSTejun Heo
778393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
779988cd7afSTejun Heo
780988cd7afSTejun Heo ret = -EINVAL;
781988cd7afSTejun Heo has_ns = kernfs_ns_enabled(parent);
782988cd7afSTejun Heo if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
783988cd7afSTejun Heo has_ns ? "required" : "invalid", parent->name, kn->name))
784988cd7afSTejun Heo goto out_unlock;
785fd7b9f7bSTejun Heo
786df23fc39STejun Heo if (kernfs_type(parent) != KERNFS_DIR)
787988cd7afSTejun Heo goto out_unlock;
788fd7b9f7bSTejun Heo
789988cd7afSTejun Heo ret = -ENOENT;
790c2549174STejun Heo if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
791988cd7afSTejun Heo goto out_unlock;
792798c75a0SGreg Kroah-Hartman
793c637b8acSTejun Heo kn->hash = kernfs_name_hash(kn->name, kn->ns);
794fd7b9f7bSTejun Heo
795c637b8acSTejun Heo ret = kernfs_link_sibling(kn);
796fd7b9f7bSTejun Heo if (ret)
797988cd7afSTejun Heo goto out_unlock;
798fd7b9f7bSTejun Heo
799fd7b9f7bSTejun Heo /* Update timestamps on the parent */
8009caf6961SImran Khan down_write(&root->kernfs_iattr_rwsem);
8019caf6961SImran Khan
802adc5e8b5STejun Heo ps_iattr = parent->iattr;
803fd7b9f7bSTejun Heo if (ps_iattr) {
80405895219SOndrej Mosnacek ktime_get_real_ts64(&ps_iattr->ia_ctime);
80505895219SOndrej Mosnacek ps_iattr->ia_mtime = ps_iattr->ia_ctime;
806fd7b9f7bSTejun Heo }
807fd7b9f7bSTejun Heo
8089caf6961SImran Khan up_write(&root->kernfs_iattr_rwsem);
809393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
810d35258efSTejun Heo
811d35258efSTejun Heo /*
812d35258efSTejun Heo * Activate the new node unless CREATE_DEACTIVATED is requested.
813d35258efSTejun Heo * If not activated here, the kernfs user is responsible for
814d35258efSTejun Heo * activating the node with kernfs_activate(). A node which hasn't
815d35258efSTejun Heo * been activated is not visible to userland and its removal won't
816d35258efSTejun Heo * trigger deactivation.
817d35258efSTejun Heo */
818d35258efSTejun Heo if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
819d35258efSTejun Heo kernfs_activate(kn);
820d35258efSTejun Heo return 0;
821d35258efSTejun Heo
822988cd7afSTejun Heo out_unlock:
823393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
824988cd7afSTejun Heo return ret;
825fd7b9f7bSTejun Heo }
826fd7b9f7bSTejun Heo
827fd7b9f7bSTejun Heo /**
828324a56e1STejun Heo * kernfs_find_ns - find kernfs_node with the given name
829324a56e1STejun Heo * @parent: kernfs_node to search under
830fd7b9f7bSTejun Heo * @name: name to look for
831fd7b9f7bSTejun Heo * @ns: the namespace tag to use
832fd7b9f7bSTejun Heo *
83324b3e3ddSRandy Dunlap * Look for kernfs_node with name @name under @parent.
83424b3e3ddSRandy Dunlap *
83524b3e3ddSRandy Dunlap * Return: pointer to the found kernfs_node on success, %NULL on failure.
836fd7b9f7bSTejun Heo */
kernfs_find_ns(struct kernfs_node * parent,const unsigned char * name,const void * ns)837324a56e1STejun Heo static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
838fd7b9f7bSTejun Heo const unsigned char *name,
839fd7b9f7bSTejun Heo const void *ns)
840fd7b9f7bSTejun Heo {
841adc5e8b5STejun Heo struct rb_node *node = parent->dir.children.rb_node;
842ac9bba03STejun Heo bool has_ns = kernfs_ns_enabled(parent);
843fd7b9f7bSTejun Heo unsigned int hash;
844fd7b9f7bSTejun Heo
845393c3714SMinchan Kim lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem);
846fd7b9f7bSTejun Heo
847fd7b9f7bSTejun Heo if (has_ns != (bool)ns) {
848c637b8acSTejun Heo WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
849adc5e8b5STejun Heo has_ns ? "required" : "invalid", parent->name, name);
850fd7b9f7bSTejun Heo return NULL;
851fd7b9f7bSTejun Heo }
852fd7b9f7bSTejun Heo
853c637b8acSTejun Heo hash = kernfs_name_hash(name, ns);
854fd7b9f7bSTejun Heo while (node) {
855324a56e1STejun Heo struct kernfs_node *kn;
856fd7b9f7bSTejun Heo int result;
857fd7b9f7bSTejun Heo
858324a56e1STejun Heo kn = rb_to_kn(node);
859c637b8acSTejun Heo result = kernfs_name_compare(hash, name, ns, kn);
860fd7b9f7bSTejun Heo if (result < 0)
861fd7b9f7bSTejun Heo node = node->rb_left;
862fd7b9f7bSTejun Heo else if (result > 0)
863fd7b9f7bSTejun Heo node = node->rb_right;
864fd7b9f7bSTejun Heo else
865324a56e1STejun Heo return kn;
866fd7b9f7bSTejun Heo }
867fd7b9f7bSTejun Heo return NULL;
868fd7b9f7bSTejun Heo }
869fd7b9f7bSTejun Heo
kernfs_walk_ns(struct kernfs_node * parent,const unsigned char * path,const void * ns)870bd96f76aSTejun Heo static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
871bd96f76aSTejun Heo const unsigned char *path,
872bd96f76aSTejun Heo const void *ns)
873bd96f76aSTejun Heo {
874e56ed358STejun Heo size_t len;
875e56ed358STejun Heo char *p, *name;
876bd96f76aSTejun Heo
877393c3714SMinchan Kim lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
878bd96f76aSTejun Heo
8791a702dc8SHao Luo spin_lock_irq(&kernfs_pr_cont_lock);
880e56ed358STejun Heo
881e56ed358STejun Heo len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
882e56ed358STejun Heo
883e56ed358STejun Heo if (len >= sizeof(kernfs_pr_cont_buf)) {
8841a702dc8SHao Luo spin_unlock_irq(&kernfs_pr_cont_lock);
885bd96f76aSTejun Heo return NULL;
886e56ed358STejun Heo }
887e56ed358STejun Heo
888e56ed358STejun Heo p = kernfs_pr_cont_buf;
889bd96f76aSTejun Heo
890bd96f76aSTejun Heo while ((name = strsep(&p, "/")) && parent) {
891bd96f76aSTejun Heo if (*name == '\0')
892bd96f76aSTejun Heo continue;
893bd96f76aSTejun Heo parent = kernfs_find_ns(parent, name, ns);
894bd96f76aSTejun Heo }
895bd96f76aSTejun Heo
8961a702dc8SHao Luo spin_unlock_irq(&kernfs_pr_cont_lock);
897e56ed358STejun Heo
898bd96f76aSTejun Heo return parent;
899bd96f76aSTejun Heo }
900bd96f76aSTejun Heo
901fd7b9f7bSTejun Heo /**
902324a56e1STejun Heo * kernfs_find_and_get_ns - find and get kernfs_node with the given name
903324a56e1STejun Heo * @parent: kernfs_node to search under
904fd7b9f7bSTejun Heo * @name: name to look for
905fd7b9f7bSTejun Heo * @ns: the namespace tag to use
906fd7b9f7bSTejun Heo *
907324a56e1STejun Heo * Look for kernfs_node with name @name under @parent and get a reference
90824b3e3ddSRandy Dunlap * if found. This function may sleep.
90924b3e3ddSRandy Dunlap *
91024b3e3ddSRandy Dunlap * Return: pointer to the found kernfs_node on success, %NULL on failure.
911fd7b9f7bSTejun Heo */
kernfs_find_and_get_ns(struct kernfs_node * parent,const char * name,const void * ns)912324a56e1STejun Heo struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
913fd7b9f7bSTejun Heo const char *name, const void *ns)
914fd7b9f7bSTejun Heo {
915324a56e1STejun Heo struct kernfs_node *kn;
916393c3714SMinchan Kim struct kernfs_root *root = kernfs_root(parent);
917fd7b9f7bSTejun Heo
918393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
919324a56e1STejun Heo kn = kernfs_find_ns(parent, name, ns);
920324a56e1STejun Heo kernfs_get(kn);
921393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
922fd7b9f7bSTejun Heo
923324a56e1STejun Heo return kn;
924fd7b9f7bSTejun Heo }
925fd7b9f7bSTejun Heo EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
926fd7b9f7bSTejun Heo
927fd7b9f7bSTejun Heo /**
928bd96f76aSTejun Heo * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
929bd96f76aSTejun Heo * @parent: kernfs_node to search under
930bd96f76aSTejun Heo * @path: path to look for
931bd96f76aSTejun Heo * @ns: the namespace tag to use
932bd96f76aSTejun Heo *
933bd96f76aSTejun Heo * Look for kernfs_node with path @path under @parent and get a reference
93424b3e3ddSRandy Dunlap * if found. This function may sleep.
93524b3e3ddSRandy Dunlap *
93624b3e3ddSRandy Dunlap * Return: pointer to the found kernfs_node on success, %NULL on failure.
937bd96f76aSTejun Heo */
kernfs_walk_and_get_ns(struct kernfs_node * parent,const char * path,const void * ns)938bd96f76aSTejun Heo struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
939bd96f76aSTejun Heo const char *path, const void *ns)
940bd96f76aSTejun Heo {
941bd96f76aSTejun Heo struct kernfs_node *kn;
942393c3714SMinchan Kim struct kernfs_root *root = kernfs_root(parent);
943bd96f76aSTejun Heo
944393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
945bd96f76aSTejun Heo kn = kernfs_walk_ns(parent, path, ns);
946bd96f76aSTejun Heo kernfs_get(kn);
947393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
948bd96f76aSTejun Heo
949bd96f76aSTejun Heo return kn;
950bd96f76aSTejun Heo }
951bd96f76aSTejun Heo
952bd96f76aSTejun Heo /**
953ba7443bcSTejun Heo * kernfs_create_root - create a new kernfs hierarchy
95490c07c89STejun Heo * @scops: optional syscall operations for the hierarchy
955d35258efSTejun Heo * @flags: KERNFS_ROOT_* flags
956ba7443bcSTejun Heo * @priv: opaque data associated with the new directory
957ba7443bcSTejun Heo *
95824b3e3ddSRandy Dunlap * Return: the root of the new hierarchy on success, ERR_PTR() value on
959ba7443bcSTejun Heo * failure.
960ba7443bcSTejun Heo */
kernfs_create_root(struct kernfs_syscall_ops * scops,unsigned int flags,void * priv)96190c07c89STejun Heo struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
962d35258efSTejun Heo unsigned int flags, void *priv)
963ba7443bcSTejun Heo {
964ba7443bcSTejun Heo struct kernfs_root *root;
965324a56e1STejun Heo struct kernfs_node *kn;
966ba7443bcSTejun Heo
967ba7443bcSTejun Heo root = kzalloc(sizeof(*root), GFP_KERNEL);
968ba7443bcSTejun Heo if (!root)
969ba7443bcSTejun Heo return ERR_PTR(-ENOMEM);
970ba7443bcSTejun Heo
9717d35079fSShaohua Li idr_init(&root->ino_idr);
972393c3714SMinchan Kim init_rwsem(&root->kernfs_rwsem);
9739caf6961SImran Khan init_rwsem(&root->kernfs_iattr_rwsem);
974c9f2dfb7SImran Khan init_rwsem(&root->kernfs_supers_rwsem);
9757d568a83STejun Heo INIT_LIST_HEAD(&root->supers);
97640430452STejun Heo
97740430452STejun Heo /*
97840430452STejun Heo * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino.
97940430452STejun Heo * High bits generation. The starting value for both ino and
98040430452STejun Heo * genenration is 1. Initialize upper 32bit allocation
98140430452STejun Heo * accordingly.
98240430452STejun Heo */
98340430452STejun Heo if (sizeof(ino_t) >= sizeof(u64))
98440430452STejun Heo root->id_highbits = 0;
98540430452STejun Heo else
98640430452STejun Heo root->id_highbits = 1;
987bc755553STejun Heo
988e19dfdc8SOndrej Mosnacek kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
989488dee96SDmitry Torokhov GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
990db4aad20STejun Heo KERNFS_DIR);
991324a56e1STejun Heo if (!kn) {
9927d35079fSShaohua Li idr_destroy(&root->ino_idr);
993ba7443bcSTejun Heo kfree(root);
994ba7443bcSTejun Heo return ERR_PTR(-ENOMEM);
995ba7443bcSTejun Heo }
996ba7443bcSTejun Heo
997324a56e1STejun Heo kn->priv = priv;
998adc5e8b5STejun Heo kn->dir.root = root;
999ba7443bcSTejun Heo
100090c07c89STejun Heo root->syscall_ops = scops;
1001d35258efSTejun Heo root->flags = flags;
1002324a56e1STejun Heo root->kn = kn;
1003abd54f02STejun Heo init_waitqueue_head(&root->deactivate_waitq);
1004ba7443bcSTejun Heo
1005d35258efSTejun Heo if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
1006d35258efSTejun Heo kernfs_activate(kn);
1007d35258efSTejun Heo
1008ba7443bcSTejun Heo return root;
1009ba7443bcSTejun Heo }
1010ba7443bcSTejun Heo
1011ba7443bcSTejun Heo /**
1012ba7443bcSTejun Heo * kernfs_destroy_root - destroy a kernfs hierarchy
1013ba7443bcSTejun Heo * @root: root of the hierarchy to destroy
1014ba7443bcSTejun Heo *
1015ba7443bcSTejun Heo * Destroy the hierarchy anchored at @root by removing all existing
1016ba7443bcSTejun Heo * directories and destroying @root.
1017ba7443bcSTejun Heo */
kernfs_destroy_root(struct kernfs_root * root)1018ba7443bcSTejun Heo void kernfs_destroy_root(struct kernfs_root *root)
1019ba7443bcSTejun Heo {
1020555a0ce4SMinchan Kim /*
1021555a0ce4SMinchan Kim * kernfs_remove holds kernfs_rwsem from the root so the root
1022555a0ce4SMinchan Kim * shouldn't be freed during the operation.
1023555a0ce4SMinchan Kim */
1024555a0ce4SMinchan Kim kernfs_get(root->kn);
1025555a0ce4SMinchan Kim kernfs_remove(root->kn);
1026555a0ce4SMinchan Kim kernfs_put(root->kn); /* will also free @root */
1027ba7443bcSTejun Heo }
1028ba7443bcSTejun Heo
1029ba7443bcSTejun Heo /**
1030f2eb478fSGreg Kroah-Hartman * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
1031f2eb478fSGreg Kroah-Hartman * @root: root to use to lookup
103224b3e3ddSRandy Dunlap *
103324b3e3ddSRandy Dunlap * Return: @root's kernfs_node
1034f2eb478fSGreg Kroah-Hartman */
kernfs_root_to_node(struct kernfs_root * root)1035f2eb478fSGreg Kroah-Hartman struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root)
1036f2eb478fSGreg Kroah-Hartman {
1037f2eb478fSGreg Kroah-Hartman return root->kn;
1038f2eb478fSGreg Kroah-Hartman }
1039f2eb478fSGreg Kroah-Hartman
1040f2eb478fSGreg Kroah-Hartman /**
1041fd7b9f7bSTejun Heo * kernfs_create_dir_ns - create a directory
1042fd7b9f7bSTejun Heo * @parent: parent in which to create a new directory
1043fd7b9f7bSTejun Heo * @name: name of the new directory
1044bb8b9d09STejun Heo * @mode: mode of the new directory
1045488dee96SDmitry Torokhov * @uid: uid of the new directory
1046488dee96SDmitry Torokhov * @gid: gid of the new directory
1047fd7b9f7bSTejun Heo * @priv: opaque data associated with the new directory
1048fd7b9f7bSTejun Heo * @ns: optional namespace tag of the directory
1049fd7b9f7bSTejun Heo *
105024b3e3ddSRandy Dunlap * Return: the created node on success, ERR_PTR() value on failure.
1051fd7b9f7bSTejun Heo */
kernfs_create_dir_ns(struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,void * priv,const void * ns)1052324a56e1STejun Heo struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
1053bb8b9d09STejun Heo const char *name, umode_t mode,
1054488dee96SDmitry Torokhov kuid_t uid, kgid_t gid,
1055bb8b9d09STejun Heo void *priv, const void *ns)
1056fd7b9f7bSTejun Heo {
1057324a56e1STejun Heo struct kernfs_node *kn;
1058fd7b9f7bSTejun Heo int rc;
1059fd7b9f7bSTejun Heo
1060fd7b9f7bSTejun Heo /* allocate */
1061488dee96SDmitry Torokhov kn = kernfs_new_node(parent, name, mode | S_IFDIR,
1062488dee96SDmitry Torokhov uid, gid, KERNFS_DIR);
1063324a56e1STejun Heo if (!kn)
1064fd7b9f7bSTejun Heo return ERR_PTR(-ENOMEM);
1065fd7b9f7bSTejun Heo
1066adc5e8b5STejun Heo kn->dir.root = parent->dir.root;
1067adc5e8b5STejun Heo kn->ns = ns;
1068324a56e1STejun Heo kn->priv = priv;
1069fd7b9f7bSTejun Heo
1070fd7b9f7bSTejun Heo /* link in */
1071988cd7afSTejun Heo rc = kernfs_add_one(kn);
1072fd7b9f7bSTejun Heo if (!rc)
1073324a56e1STejun Heo return kn;
1074fd7b9f7bSTejun Heo
1075324a56e1STejun Heo kernfs_put(kn);
1076fd7b9f7bSTejun Heo return ERR_PTR(rc);
1077fd7b9f7bSTejun Heo }
1078fd7b9f7bSTejun Heo
1079ea015218SEric W. Biederman /**
1080ea015218SEric W. Biederman * kernfs_create_empty_dir - create an always empty directory
1081ea015218SEric W. Biederman * @parent: parent in which to create a new directory
1082ea015218SEric W. Biederman * @name: name of the new directory
1083ea015218SEric W. Biederman *
108424b3e3ddSRandy Dunlap * Return: the created node on success, ERR_PTR() value on failure.
1085ea015218SEric W. Biederman */
kernfs_create_empty_dir(struct kernfs_node * parent,const char * name)1086ea015218SEric W. Biederman struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
1087ea015218SEric W. Biederman const char *name)
1088ea015218SEric W. Biederman {
1089ea015218SEric W. Biederman struct kernfs_node *kn;
1090ea015218SEric W. Biederman int rc;
1091ea015218SEric W. Biederman
1092ea015218SEric W. Biederman /* allocate */
1093488dee96SDmitry Torokhov kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
1094488dee96SDmitry Torokhov GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
1095ea015218SEric W. Biederman if (!kn)
1096ea015218SEric W. Biederman return ERR_PTR(-ENOMEM);
1097ea015218SEric W. Biederman
1098ea015218SEric W. Biederman kn->flags |= KERNFS_EMPTY_DIR;
1099ea015218SEric W. Biederman kn->dir.root = parent->dir.root;
1100ea015218SEric W. Biederman kn->ns = NULL;
1101ea015218SEric W. Biederman kn->priv = NULL;
1102ea015218SEric W. Biederman
1103ea015218SEric W. Biederman /* link in */
1104ea015218SEric W. Biederman rc = kernfs_add_one(kn);
1105ea015218SEric W. Biederman if (!rc)
1106ea015218SEric W. Biederman return kn;
1107ea015218SEric W. Biederman
1108ea015218SEric W. Biederman kernfs_put(kn);
1109ea015218SEric W. Biederman return ERR_PTR(rc);
1110ea015218SEric W. Biederman }
1111ea015218SEric W. Biederman
kernfs_dop_revalidate(struct dentry * dentry,unsigned int flags)1112d826e036SIan Kent static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
1113d826e036SIan Kent {
1114d826e036SIan Kent struct kernfs_node *kn;
1115393c3714SMinchan Kim struct kernfs_root *root;
1116d826e036SIan Kent
1117d826e036SIan Kent if (flags & LOOKUP_RCU)
1118d826e036SIan Kent return -ECHILD;
1119d826e036SIan Kent
1120c7e7c042SIan Kent /* Negative hashed dentry? */
1121c7e7c042SIan Kent if (d_really_is_negative(dentry)) {
1122c7e7c042SIan Kent struct kernfs_node *parent;
1123c7e7c042SIan Kent
1124c7e7c042SIan Kent /* If the kernfs parent node has changed discard and
1125c7e7c042SIan Kent * proceed to ->lookup.
112692b57842SIan Kent *
112792b57842SIan Kent * There's nothing special needed here when getting the
112892b57842SIan Kent * dentry parent, even if a concurrent rename is in
112992b57842SIan Kent * progress. That's because the dentry is negative so
113092b57842SIan Kent * it can only be the target of the rename and it will
113192b57842SIan Kent * be doing a d_move() not a replace. Consequently the
113292b57842SIan Kent * dentry d_parent won't change over the d_move().
113392b57842SIan Kent *
113492b57842SIan Kent * Also kernfs negative dentries transitioning from
113592b57842SIan Kent * negative to positive during revalidate won't happen
113692b57842SIan Kent * because they are invalidated on containing directory
113792b57842SIan Kent * changes and the lookup re-done so that a new positive
113892b57842SIan Kent * dentry can be properly created.
1139c7e7c042SIan Kent */
114092b57842SIan Kent root = kernfs_root_from_sb(dentry->d_sb);
114192b57842SIan Kent down_read(&root->kernfs_rwsem);
1142c7e7c042SIan Kent parent = kernfs_dentry_node(dentry->d_parent);
1143c7e7c042SIan Kent if (parent) {
1144393c3714SMinchan Kim if (kernfs_dir_changed(parent, dentry)) {
1145393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1146c7e7c042SIan Kent return 0;
1147c7e7c042SIan Kent }
114892b57842SIan Kent }
1149393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1150c7e7c042SIan Kent
1151c7e7c042SIan Kent /* The kernfs parent node hasn't changed, leave the
1152c7e7c042SIan Kent * dentry negative and return success.
1153c7e7c042SIan Kent */
1154c7e7c042SIan Kent return 1;
1155c7e7c042SIan Kent }
1156d826e036SIan Kent
1157d826e036SIan Kent kn = kernfs_dentry_node(dentry);
1158393c3714SMinchan Kim root = kernfs_root(kn);
1159393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
1160d826e036SIan Kent
1161d826e036SIan Kent /* The kernfs node has been deactivated */
1162d826e036SIan Kent if (!kernfs_active(kn))
1163d826e036SIan Kent goto out_bad;
1164d826e036SIan Kent
1165d826e036SIan Kent /* The kernfs node has been moved? */
1166d826e036SIan Kent if (kernfs_dentry_node(dentry->d_parent) != kn->parent)
1167d826e036SIan Kent goto out_bad;
1168d826e036SIan Kent
1169d826e036SIan Kent /* The kernfs node has been renamed */
1170d826e036SIan Kent if (strcmp(dentry->d_name.name, kn->name) != 0)
1171d826e036SIan Kent goto out_bad;
1172d826e036SIan Kent
1173d826e036SIan Kent /* The kernfs node has been moved to a different namespace */
1174d826e036SIan Kent if (kn->parent && kernfs_ns_enabled(kn->parent) &&
1175d826e036SIan Kent kernfs_info(dentry->d_sb)->ns != kn->ns)
1176d826e036SIan Kent goto out_bad;
1177d826e036SIan Kent
1178393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1179d826e036SIan Kent return 1;
1180d826e036SIan Kent out_bad:
1181393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1182d826e036SIan Kent return 0;
1183d826e036SIan Kent }
1184d826e036SIan Kent
1185d826e036SIan Kent const struct dentry_operations kernfs_dops = {
1186d826e036SIan Kent .d_revalidate = kernfs_dop_revalidate,
1187d826e036SIan Kent };
1188d826e036SIan Kent
kernfs_iop_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1189c637b8acSTejun Heo static struct dentry *kernfs_iop_lookup(struct inode *dir,
1190c637b8acSTejun Heo struct dentry *dentry,
1191fd7b9f7bSTejun Heo unsigned int flags)
1192fd7b9f7bSTejun Heo {
1193319ba91dSShaohua Li struct kernfs_node *parent = dir->i_private;
1194324a56e1STejun Heo struct kernfs_node *kn;
1195393c3714SMinchan Kim struct kernfs_root *root;
1196c7e7c042SIan Kent struct inode *inode = NULL;
1197fd7b9f7bSTejun Heo const void *ns = NULL;
1198fd7b9f7bSTejun Heo
1199393c3714SMinchan Kim root = kernfs_root(parent);
1200393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
1201324a56e1STejun Heo if (kernfs_ns_enabled(parent))
1202c525aaddSTejun Heo ns = kernfs_info(dir->i_sb)->ns;
1203fd7b9f7bSTejun Heo
1204324a56e1STejun Heo kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
1205fd7b9f7bSTejun Heo /* attach dentry and inode */
1206410d591aSIan Kent if (kn) {
1207410d591aSIan Kent /* Inactive nodes are invisible to the VFS so don't
1208410d591aSIan Kent * create a negative.
1209410d591aSIan Kent */
1210410d591aSIan Kent if (!kernfs_active(kn)) {
1211393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1212410d591aSIan Kent return NULL;
1213410d591aSIan Kent }
1214c637b8acSTejun Heo inode = kernfs_get_inode(dir->i_sb, kn);
1215c7e7c042SIan Kent if (!inode)
1216c7e7c042SIan Kent inode = ERR_PTR(-ENOMEM);
1217fd7b9f7bSTejun Heo }
1218df38d852SHou Tao /*
1219df38d852SHou Tao * Needed for negative dentry validation.
1220df38d852SHou Tao * The negative dentry can be created in kernfs_iop_lookup()
1221df38d852SHou Tao * or transforms from positive dentry in dentry_unlink_inode()
1222df38d852SHou Tao * called from vfs_rmdir().
1223df38d852SHou Tao */
1224df38d852SHou Tao if (!IS_ERR(inode))
1225c7e7c042SIan Kent kernfs_set_rev(parent, dentry);
1226393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1227c7e7c042SIan Kent
1228df6192f4SIan Kent /* instantiate and hash (possibly negative) dentry */
1229df6192f4SIan Kent return d_splice_alias(inode, dentry);
1230fd7b9f7bSTejun Heo }
1231fd7b9f7bSTejun Heo
kernfs_iop_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)1232c54bd91eSChristian Brauner static int kernfs_iop_mkdir(struct mnt_idmap *idmap,
1233549c7297SChristian Brauner struct inode *dir, struct dentry *dentry,
123480b9bbefSTejun Heo umode_t mode)
123580b9bbefSTejun Heo {
123680b9bbefSTejun Heo struct kernfs_node *parent = dir->i_private;
123790c07c89STejun Heo struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
123807c7530dSTejun Heo int ret;
123980b9bbefSTejun Heo
124090c07c89STejun Heo if (!scops || !scops->mkdir)
124180b9bbefSTejun Heo return -EPERM;
124280b9bbefSTejun Heo
124307c7530dSTejun Heo if (!kernfs_get_active(parent))
124407c7530dSTejun Heo return -ENODEV;
124507c7530dSTejun Heo
124690c07c89STejun Heo ret = scops->mkdir(parent, dentry->d_name.name, mode);
124707c7530dSTejun Heo
124807c7530dSTejun Heo kernfs_put_active(parent);
124907c7530dSTejun Heo return ret;
125080b9bbefSTejun Heo }
125180b9bbefSTejun Heo
kernfs_iop_rmdir(struct inode * dir,struct dentry * dentry)125280b9bbefSTejun Heo static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
125380b9bbefSTejun Heo {
1254319ba91dSShaohua Li struct kernfs_node *kn = kernfs_dentry_node(dentry);
125590c07c89STejun Heo struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
125607c7530dSTejun Heo int ret;
125780b9bbefSTejun Heo
125890c07c89STejun Heo if (!scops || !scops->rmdir)
125980b9bbefSTejun Heo return -EPERM;
126080b9bbefSTejun Heo
126107c7530dSTejun Heo if (!kernfs_get_active(kn))
126207c7530dSTejun Heo return -ENODEV;
126307c7530dSTejun Heo
126490c07c89STejun Heo ret = scops->rmdir(kn);
126507c7530dSTejun Heo
126607c7530dSTejun Heo kernfs_put_active(kn);
126707c7530dSTejun Heo return ret;
126880b9bbefSTejun Heo }
126980b9bbefSTejun Heo
kernfs_iop_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1270e18275aeSChristian Brauner static int kernfs_iop_rename(struct mnt_idmap *idmap,
1271549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry,
12721cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry,
12731cd66c93SMiklos Szeredi unsigned int flags)
127480b9bbefSTejun Heo {
1275319ba91dSShaohua Li struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
127680b9bbefSTejun Heo struct kernfs_node *new_parent = new_dir->i_private;
127790c07c89STejun Heo struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
127807c7530dSTejun Heo int ret;
127980b9bbefSTejun Heo
12801cd66c93SMiklos Szeredi if (flags)
12811cd66c93SMiklos Szeredi return -EINVAL;
12821cd66c93SMiklos Szeredi
128390c07c89STejun Heo if (!scops || !scops->rename)
128480b9bbefSTejun Heo return -EPERM;
128580b9bbefSTejun Heo
128607c7530dSTejun Heo if (!kernfs_get_active(kn))
128707c7530dSTejun Heo return -ENODEV;
128807c7530dSTejun Heo
128907c7530dSTejun Heo if (!kernfs_get_active(new_parent)) {
129007c7530dSTejun Heo kernfs_put_active(kn);
129107c7530dSTejun Heo return -ENODEV;
129207c7530dSTejun Heo }
129307c7530dSTejun Heo
129490c07c89STejun Heo ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
129507c7530dSTejun Heo
129607c7530dSTejun Heo kernfs_put_active(new_parent);
129707c7530dSTejun Heo kernfs_put_active(kn);
129807c7530dSTejun Heo return ret;
129980b9bbefSTejun Heo }
130080b9bbefSTejun Heo
1301a797bfc3STejun Heo const struct inode_operations kernfs_dir_iops = {
1302c637b8acSTejun Heo .lookup = kernfs_iop_lookup,
1303c637b8acSTejun Heo .permission = kernfs_iop_permission,
1304c637b8acSTejun Heo .setattr = kernfs_iop_setattr,
1305c637b8acSTejun Heo .getattr = kernfs_iop_getattr,
1306c637b8acSTejun Heo .listxattr = kernfs_iop_listxattr,
130780b9bbefSTejun Heo
130880b9bbefSTejun Heo .mkdir = kernfs_iop_mkdir,
130980b9bbefSTejun Heo .rmdir = kernfs_iop_rmdir,
131080b9bbefSTejun Heo .rename = kernfs_iop_rename,
1311fd7b9f7bSTejun Heo };
1312fd7b9f7bSTejun Heo
kernfs_leftmost_descendant(struct kernfs_node * pos)1313c637b8acSTejun Heo static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
1314fd7b9f7bSTejun Heo {
1315324a56e1STejun Heo struct kernfs_node *last;
1316fd7b9f7bSTejun Heo
1317fd7b9f7bSTejun Heo while (true) {
1318fd7b9f7bSTejun Heo struct rb_node *rbn;
1319fd7b9f7bSTejun Heo
1320fd7b9f7bSTejun Heo last = pos;
1321fd7b9f7bSTejun Heo
1322df23fc39STejun Heo if (kernfs_type(pos) != KERNFS_DIR)
1323fd7b9f7bSTejun Heo break;
1324fd7b9f7bSTejun Heo
1325adc5e8b5STejun Heo rbn = rb_first(&pos->dir.children);
1326fd7b9f7bSTejun Heo if (!rbn)
1327fd7b9f7bSTejun Heo break;
1328fd7b9f7bSTejun Heo
1329324a56e1STejun Heo pos = rb_to_kn(rbn);
1330fd7b9f7bSTejun Heo }
1331fd7b9f7bSTejun Heo
1332fd7b9f7bSTejun Heo return last;
1333fd7b9f7bSTejun Heo }
1334fd7b9f7bSTejun Heo
1335fd7b9f7bSTejun Heo /**
1336c637b8acSTejun Heo * kernfs_next_descendant_post - find the next descendant for post-order walk
1337fd7b9f7bSTejun Heo * @pos: the current position (%NULL to initiate traversal)
1338324a56e1STejun Heo * @root: kernfs_node whose descendants to walk
1339fd7b9f7bSTejun Heo *
1340fd7b9f7bSTejun Heo * Find the next descendant to visit for post-order traversal of @root's
1341fd7b9f7bSTejun Heo * descendants. @root is included in the iteration and the last node to be
1342fd7b9f7bSTejun Heo * visited.
134324b3e3ddSRandy Dunlap *
134424b3e3ddSRandy Dunlap * Return: the next descendant to visit or %NULL when done.
1345fd7b9f7bSTejun Heo */
kernfs_next_descendant_post(struct kernfs_node * pos,struct kernfs_node * root)1346c637b8acSTejun Heo static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
1347324a56e1STejun Heo struct kernfs_node *root)
1348fd7b9f7bSTejun Heo {
1349fd7b9f7bSTejun Heo struct rb_node *rbn;
1350fd7b9f7bSTejun Heo
1351393c3714SMinchan Kim lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem);
1352fd7b9f7bSTejun Heo
1353fd7b9f7bSTejun Heo /* if first iteration, visit leftmost descendant which may be root */
1354fd7b9f7bSTejun Heo if (!pos)
1355c637b8acSTejun Heo return kernfs_leftmost_descendant(root);
1356fd7b9f7bSTejun Heo
1357fd7b9f7bSTejun Heo /* if we visited @root, we're done */
1358fd7b9f7bSTejun Heo if (pos == root)
1359fd7b9f7bSTejun Heo return NULL;
1360fd7b9f7bSTejun Heo
1361fd7b9f7bSTejun Heo /* if there's an unvisited sibling, visit its leftmost descendant */
1362adc5e8b5STejun Heo rbn = rb_next(&pos->rb);
1363fd7b9f7bSTejun Heo if (rbn)
1364c637b8acSTejun Heo return kernfs_leftmost_descendant(rb_to_kn(rbn));
1365fd7b9f7bSTejun Heo
1366fd7b9f7bSTejun Heo /* no sibling left, visit parent */
1367adc5e8b5STejun Heo return pos->parent;
1368fd7b9f7bSTejun Heo }
1369fd7b9f7bSTejun Heo
kernfs_activate_one(struct kernfs_node * kn)1370f8eb145eSTejun Heo static void kernfs_activate_one(struct kernfs_node *kn)
1371f8eb145eSTejun Heo {
1372f8eb145eSTejun Heo lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1373f8eb145eSTejun Heo
1374f8eb145eSTejun Heo kn->flags |= KERNFS_ACTIVATED;
1375f8eb145eSTejun Heo
1376783bd07dSTejun Heo if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
1377f8eb145eSTejun Heo return;
1378f8eb145eSTejun Heo
1379f8eb145eSTejun Heo WARN_ON_ONCE(kn->parent && RB_EMPTY_NODE(&kn->rb));
1380f8eb145eSTejun Heo WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
1381f8eb145eSTejun Heo
1382f8eb145eSTejun Heo atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
1383f8eb145eSTejun Heo }
1384f8eb145eSTejun Heo
1385d35258efSTejun Heo /**
1386d35258efSTejun Heo * kernfs_activate - activate a node which started deactivated
1387d35258efSTejun Heo * @kn: kernfs_node whose subtree is to be activated
1388d35258efSTejun Heo *
1389d35258efSTejun Heo * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
1390d35258efSTejun Heo * needs to be explicitly activated. A node which hasn't been activated
1391d35258efSTejun Heo * isn't visible to userland and deactivation is skipped during its
1392d35258efSTejun Heo * removal. This is useful to construct atomic init sequences where
1393d35258efSTejun Heo * creation of multiple nodes should either succeed or fail atomically.
1394d35258efSTejun Heo *
1395d35258efSTejun Heo * The caller is responsible for ensuring that this function is not called
1396d35258efSTejun Heo * after kernfs_remove*() is invoked on @kn.
1397d35258efSTejun Heo */
kernfs_activate(struct kernfs_node * kn)1398d35258efSTejun Heo void kernfs_activate(struct kernfs_node *kn)
1399d35258efSTejun Heo {
1400d35258efSTejun Heo struct kernfs_node *pos;
1401393c3714SMinchan Kim struct kernfs_root *root = kernfs_root(kn);
1402d35258efSTejun Heo
1403393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
1404d35258efSTejun Heo
1405d35258efSTejun Heo pos = NULL;
1406f8eb145eSTejun Heo while ((pos = kernfs_next_descendant_post(pos, kn)))
1407f8eb145eSTejun Heo kernfs_activate_one(pos);
1408d35258efSTejun Heo
1409393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
1410d35258efSTejun Heo }
1411d35258efSTejun Heo
1412783bd07dSTejun Heo /**
1413783bd07dSTejun Heo * kernfs_show - show or hide a node
1414783bd07dSTejun Heo * @kn: kernfs_node to show or hide
1415783bd07dSTejun Heo * @show: whether to show or hide
1416783bd07dSTejun Heo *
1417783bd07dSTejun Heo * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
1418783bd07dSTejun Heo * ignored in future activaitons. If %true, the mark is removed and activation
1419783bd07dSTejun Heo * state is restored. This function won't implicitly activate a new node in a
1420783bd07dSTejun Heo * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
1421783bd07dSTejun Heo *
1422783bd07dSTejun Heo * To avoid recursion complexities, directories aren't supported for now.
1423783bd07dSTejun Heo */
kernfs_show(struct kernfs_node * kn,bool show)1424783bd07dSTejun Heo void kernfs_show(struct kernfs_node *kn, bool show)
1425783bd07dSTejun Heo {
1426783bd07dSTejun Heo struct kernfs_root *root = kernfs_root(kn);
1427783bd07dSTejun Heo
1428783bd07dSTejun Heo if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR))
1429783bd07dSTejun Heo return;
1430783bd07dSTejun Heo
1431783bd07dSTejun Heo down_write(&root->kernfs_rwsem);
1432783bd07dSTejun Heo
1433783bd07dSTejun Heo if (show) {
1434783bd07dSTejun Heo kn->flags &= ~KERNFS_HIDDEN;
1435783bd07dSTejun Heo if (kn->flags & KERNFS_ACTIVATED)
1436783bd07dSTejun Heo kernfs_activate_one(kn);
1437783bd07dSTejun Heo } else {
1438783bd07dSTejun Heo kn->flags |= KERNFS_HIDDEN;
1439783bd07dSTejun Heo if (kernfs_active(kn))
1440783bd07dSTejun Heo atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
1441783bd07dSTejun Heo kernfs_drain(kn);
1442783bd07dSTejun Heo }
1443783bd07dSTejun Heo
1444783bd07dSTejun Heo up_write(&root->kernfs_rwsem);
1445783bd07dSTejun Heo }
1446783bd07dSTejun Heo
__kernfs_remove(struct kernfs_node * kn)1447988cd7afSTejun Heo static void __kernfs_remove(struct kernfs_node *kn)
1448fd7b9f7bSTejun Heo {
144935beab06STejun Heo struct kernfs_node *pos;
145035beab06STejun Heo
145172b5d5aeSYushan Zhou /* Short-circuit if non-root @kn has already finished removal. */
145272b5d5aeSYushan Zhou if (!kn)
145372b5d5aeSYushan Zhou return;
145472b5d5aeSYushan Zhou
1455393c3714SMinchan Kim lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1456fd7b9f7bSTejun Heo
14576b0afc2aSTejun Heo /*
14586b0afc2aSTejun Heo * This is for kernfs_remove_self() which plays with active ref
14596b0afc2aSTejun Heo * after removal.
14606b0afc2aSTejun Heo */
146172b5d5aeSYushan Zhou if (kn->parent && RB_EMPTY_NODE(&kn->rb))
1462ce9b499cSGreg Kroah-Hartman return;
1463ce9b499cSGreg Kroah-Hartman
1464c637b8acSTejun Heo pr_debug("kernfs %s: removing\n", kn->name);
1465fd7b9f7bSTejun Heo
1466c2549174STejun Heo /* prevent new usage by marking all nodes removing and deactivating */
146735beab06STejun Heo pos = NULL;
1468c2549174STejun Heo while ((pos = kernfs_next_descendant_post(pos, kn))) {
1469c2549174STejun Heo pos->flags |= KERNFS_REMOVING;
147081c173cbSTejun Heo if (kernfs_active(pos))
147181c173cbSTejun Heo atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
1472c2549174STejun Heo }
147335beab06STejun Heo
147435beab06STejun Heo /* deactivate and unlink the subtree node-by-node */
1475fd7b9f7bSTejun Heo do {
147635beab06STejun Heo pos = kernfs_leftmost_descendant(kn);
147735beab06STejun Heo
147835beab06STejun Heo /*
14792d7f9f8cSTejun Heo * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
148081c173cbSTejun Heo * base ref could have been put by someone else by the time
148181c173cbSTejun Heo * the function returns. Make sure it doesn't go away
148281c173cbSTejun Heo * underneath us.
148335beab06STejun Heo */
148435beab06STejun Heo kernfs_get(pos);
148535beab06STejun Heo
148681c173cbSTejun Heo kernfs_drain(pos);
148735beab06STejun Heo
148835beab06STejun Heo /*
148935beab06STejun Heo * kernfs_unlink_sibling() succeeds once per node. Use it
149035beab06STejun Heo * to decide who's responsible for cleanups.
149135beab06STejun Heo */
149235beab06STejun Heo if (!pos->parent || kernfs_unlink_sibling(pos)) {
149335beab06STejun Heo struct kernfs_iattrs *ps_iattr =
149435beab06STejun Heo pos->parent ? pos->parent->iattr : NULL;
149535beab06STejun Heo
149635beab06STejun Heo /* update timestamps on the parent */
14979caf6961SImran Khan down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
14989caf6961SImran Khan
149935beab06STejun Heo if (ps_iattr) {
150005895219SOndrej Mosnacek ktime_get_real_ts64(&ps_iattr->ia_ctime);
150105895219SOndrej Mosnacek ps_iattr->ia_mtime = ps_iattr->ia_ctime;
150235beab06STejun Heo }
150335beab06STejun Heo
15049caf6961SImran Khan up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
1505988cd7afSTejun Heo kernfs_put(pos);
150635beab06STejun Heo }
150735beab06STejun Heo
150835beab06STejun Heo kernfs_put(pos);
150935beab06STejun Heo } while (pos != kn);
1510fd7b9f7bSTejun Heo }
1511fd7b9f7bSTejun Heo
1512fd7b9f7bSTejun Heo /**
1513324a56e1STejun Heo * kernfs_remove - remove a kernfs_node recursively
1514324a56e1STejun Heo * @kn: the kernfs_node to remove
1515fd7b9f7bSTejun Heo *
1516324a56e1STejun Heo * Remove @kn along with all its subdirectories and files.
1517fd7b9f7bSTejun Heo */
kernfs_remove(struct kernfs_node * kn)1518324a56e1STejun Heo void kernfs_remove(struct kernfs_node *kn)
1519fd7b9f7bSTejun Heo {
1520ad8d8693SMinchan Kim struct kernfs_root *root;
1521ad8d8693SMinchan Kim
1522ad8d8693SMinchan Kim if (!kn)
1523ad8d8693SMinchan Kim return;
1524ad8d8693SMinchan Kim
1525ad8d8693SMinchan Kim root = kernfs_root(kn);
1526393c3714SMinchan Kim
1527393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
1528988cd7afSTejun Heo __kernfs_remove(kn);
1529393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
1530fd7b9f7bSTejun Heo }
1531fd7b9f7bSTejun Heo
1532fd7b9f7bSTejun Heo /**
15336b0afc2aSTejun Heo * kernfs_break_active_protection - break out of active protection
15346b0afc2aSTejun Heo * @kn: the self kernfs_node
15356b0afc2aSTejun Heo *
15366b0afc2aSTejun Heo * The caller must be running off of a kernfs operation which is invoked
15376b0afc2aSTejun Heo * with an active reference - e.g. one of kernfs_ops. Each invocation of
15386b0afc2aSTejun Heo * this function must also be matched with an invocation of
15396b0afc2aSTejun Heo * kernfs_unbreak_active_protection().
15406b0afc2aSTejun Heo *
15416b0afc2aSTejun Heo * This function releases the active reference of @kn the caller is
15426b0afc2aSTejun Heo * holding. Once this function is called, @kn may be removed at any point
15436b0afc2aSTejun Heo * and the caller is solely responsible for ensuring that the objects it
15446b0afc2aSTejun Heo * dereferences are accessible.
15456b0afc2aSTejun Heo */
kernfs_break_active_protection(struct kernfs_node * kn)15466b0afc2aSTejun Heo void kernfs_break_active_protection(struct kernfs_node *kn)
15476b0afc2aSTejun Heo {
15486b0afc2aSTejun Heo /*
15496b0afc2aSTejun Heo * Take out ourself out of the active ref dependency chain. If
15506b0afc2aSTejun Heo * we're called without an active ref, lockdep will complain.
15516b0afc2aSTejun Heo */
15526b0afc2aSTejun Heo kernfs_put_active(kn);
15536b0afc2aSTejun Heo }
15546b0afc2aSTejun Heo
15556b0afc2aSTejun Heo /**
15566b0afc2aSTejun Heo * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
15576b0afc2aSTejun Heo * @kn: the self kernfs_node
15586b0afc2aSTejun Heo *
15596b0afc2aSTejun Heo * If kernfs_break_active_protection() was called, this function must be
15606b0afc2aSTejun Heo * invoked before finishing the kernfs operation. Note that while this
15616b0afc2aSTejun Heo * function restores the active reference, it doesn't and can't actually
15626b0afc2aSTejun Heo * restore the active protection - @kn may already or be in the process of
15636b0afc2aSTejun Heo * being removed. Once kernfs_break_active_protection() is invoked, that
15646b0afc2aSTejun Heo * protection is irreversibly gone for the kernfs operation instance.
15656b0afc2aSTejun Heo *
15666b0afc2aSTejun Heo * While this function may be called at any point after
15676b0afc2aSTejun Heo * kernfs_break_active_protection() is invoked, its most useful location
15686b0afc2aSTejun Heo * would be right before the enclosing kernfs operation returns.
15696b0afc2aSTejun Heo */
kernfs_unbreak_active_protection(struct kernfs_node * kn)15706b0afc2aSTejun Heo void kernfs_unbreak_active_protection(struct kernfs_node *kn)
15716b0afc2aSTejun Heo {
15726b0afc2aSTejun Heo /*
15736b0afc2aSTejun Heo * @kn->active could be in any state; however, the increment we do
15746b0afc2aSTejun Heo * here will be undone as soon as the enclosing kernfs operation
15756b0afc2aSTejun Heo * finishes and this temporary bump can't break anything. If @kn
15766b0afc2aSTejun Heo * is alive, nothing changes. If @kn is being deactivated, the
15776b0afc2aSTejun Heo * soon-to-follow put will either finish deactivation or restore
15786b0afc2aSTejun Heo * deactivated state. If @kn is already removed, the temporary
15796b0afc2aSTejun Heo * bump is guaranteed to be gone before @kn is released.
15806b0afc2aSTejun Heo */
15816b0afc2aSTejun Heo atomic_inc(&kn->active);
15826b0afc2aSTejun Heo if (kernfs_lockdep(kn))
15836b0afc2aSTejun Heo rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
15846b0afc2aSTejun Heo }
15856b0afc2aSTejun Heo
15866b0afc2aSTejun Heo /**
15876b0afc2aSTejun Heo * kernfs_remove_self - remove a kernfs_node from its own method
15886b0afc2aSTejun Heo * @kn: the self kernfs_node to remove
15896b0afc2aSTejun Heo *
15906b0afc2aSTejun Heo * The caller must be running off of a kernfs operation which is invoked
15916b0afc2aSTejun Heo * with an active reference - e.g. one of kernfs_ops. This can be used to
15926b0afc2aSTejun Heo * implement a file operation which deletes itself.
15936b0afc2aSTejun Heo *
15946b0afc2aSTejun Heo * For example, the "delete" file for a sysfs device directory can be
15956b0afc2aSTejun Heo * implemented by invoking kernfs_remove_self() on the "delete" file
15966b0afc2aSTejun Heo * itself. This function breaks the circular dependency of trying to
15976b0afc2aSTejun Heo * deactivate self while holding an active ref itself. It isn't necessary
15986b0afc2aSTejun Heo * to modify the usual removal path to use kernfs_remove_self(). The
15996b0afc2aSTejun Heo * "delete" implementation can simply invoke kernfs_remove_self() on self
16006b0afc2aSTejun Heo * before proceeding with the usual removal path. kernfs will ignore later
16016b0afc2aSTejun Heo * kernfs_remove() on self.
16026b0afc2aSTejun Heo *
16036b0afc2aSTejun Heo * kernfs_remove_self() can be called multiple times concurrently on the
16046b0afc2aSTejun Heo * same kernfs_node. Only the first one actually performs removal and
16056b0afc2aSTejun Heo * returns %true. All others will wait until the kernfs operation which
16066b0afc2aSTejun Heo * won self-removal finishes and return %false. Note that the losers wait
16076b0afc2aSTejun Heo * for the completion of not only the winning kernfs_remove_self() but also
16086b0afc2aSTejun Heo * the whole kernfs_ops which won the arbitration. This can be used to
16096b0afc2aSTejun Heo * guarantee, for example, all concurrent writes to a "delete" file to
16106b0afc2aSTejun Heo * finish only after the whole operation is complete.
161124b3e3ddSRandy Dunlap *
161224b3e3ddSRandy Dunlap * Return: %true if @kn is removed by this call, otherwise %false.
16136b0afc2aSTejun Heo */
kernfs_remove_self(struct kernfs_node * kn)16146b0afc2aSTejun Heo bool kernfs_remove_self(struct kernfs_node *kn)
16156b0afc2aSTejun Heo {
16166b0afc2aSTejun Heo bool ret;
1617393c3714SMinchan Kim struct kernfs_root *root = kernfs_root(kn);
16186b0afc2aSTejun Heo
1619393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
16206b0afc2aSTejun Heo kernfs_break_active_protection(kn);
16216b0afc2aSTejun Heo
16226b0afc2aSTejun Heo /*
16236b0afc2aSTejun Heo * SUICIDAL is used to arbitrate among competing invocations. Only
16246b0afc2aSTejun Heo * the first one will actually perform removal. When the removal
16256b0afc2aSTejun Heo * is complete, SUICIDED is set and the active ref is restored
16267ba0273bSIan Kent * while kernfs_rwsem for held exclusive. The ones which lost
16277ba0273bSIan Kent * arbitration waits for SUICIDED && drained which can happen only
16287ba0273bSIan Kent * after the enclosing kernfs operation which executed the winning
16297ba0273bSIan Kent * instance of kernfs_remove_self() finished.
16306b0afc2aSTejun Heo */
16316b0afc2aSTejun Heo if (!(kn->flags & KERNFS_SUICIDAL)) {
16326b0afc2aSTejun Heo kn->flags |= KERNFS_SUICIDAL;
16336b0afc2aSTejun Heo __kernfs_remove(kn);
16346b0afc2aSTejun Heo kn->flags |= KERNFS_SUICIDED;
16356b0afc2aSTejun Heo ret = true;
16366b0afc2aSTejun Heo } else {
16376b0afc2aSTejun Heo wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
16386b0afc2aSTejun Heo DEFINE_WAIT(wait);
16396b0afc2aSTejun Heo
16406b0afc2aSTejun Heo while (true) {
16416b0afc2aSTejun Heo prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
16426b0afc2aSTejun Heo
16436b0afc2aSTejun Heo if ((kn->flags & KERNFS_SUICIDED) &&
16446b0afc2aSTejun Heo atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
16456b0afc2aSTejun Heo break;
16466b0afc2aSTejun Heo
1647393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
16486b0afc2aSTejun Heo schedule();
1649393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
16506b0afc2aSTejun Heo }
16516b0afc2aSTejun Heo finish_wait(waitq, &wait);
16526b0afc2aSTejun Heo WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
16536b0afc2aSTejun Heo ret = false;
16546b0afc2aSTejun Heo }
16556b0afc2aSTejun Heo
16566b0afc2aSTejun Heo /*
16577ba0273bSIan Kent * This must be done while kernfs_rwsem held exclusive; otherwise,
16587ba0273bSIan Kent * waiting for SUICIDED && deactivated could finish prematurely.
16596b0afc2aSTejun Heo */
16606b0afc2aSTejun Heo kernfs_unbreak_active_protection(kn);
16616b0afc2aSTejun Heo
1662393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
16636b0afc2aSTejun Heo return ret;
16646b0afc2aSTejun Heo }
16656b0afc2aSTejun Heo
16666b0afc2aSTejun Heo /**
1667324a56e1STejun Heo * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1668324a56e1STejun Heo * @parent: parent of the target
1669324a56e1STejun Heo * @name: name of the kernfs_node to remove
1670324a56e1STejun Heo * @ns: namespace tag of the kernfs_node to remove
1671fd7b9f7bSTejun Heo *
1672324a56e1STejun Heo * Look for the kernfs_node with @name and @ns under @parent and remove it.
167324b3e3ddSRandy Dunlap *
167424b3e3ddSRandy Dunlap * Return: %0 on success, -ENOENT if such entry doesn't exist.
1675fd7b9f7bSTejun Heo */
kernfs_remove_by_name_ns(struct kernfs_node * parent,const char * name,const void * ns)1676324a56e1STejun Heo int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
1677fd7b9f7bSTejun Heo const void *ns)
1678fd7b9f7bSTejun Heo {
1679324a56e1STejun Heo struct kernfs_node *kn;
1680393c3714SMinchan Kim struct kernfs_root *root;
1681fd7b9f7bSTejun Heo
1682324a56e1STejun Heo if (!parent) {
1683c637b8acSTejun Heo WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
1684fd7b9f7bSTejun Heo name);
1685fd7b9f7bSTejun Heo return -ENOENT;
1686fd7b9f7bSTejun Heo }
1687fd7b9f7bSTejun Heo
1688393c3714SMinchan Kim root = kernfs_root(parent);
1689393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
1690fd7b9f7bSTejun Heo
1691324a56e1STejun Heo kn = kernfs_find_ns(parent, name, ns);
16924abc9965SChristian A. Ehrhardt if (kn) {
16934abc9965SChristian A. Ehrhardt kernfs_get(kn);
1694988cd7afSTejun Heo __kernfs_remove(kn);
16954abc9965SChristian A. Ehrhardt kernfs_put(kn);
16964abc9965SChristian A. Ehrhardt }
1697fd7b9f7bSTejun Heo
1698393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
1699fd7b9f7bSTejun Heo
1700324a56e1STejun Heo if (kn)
1701fd7b9f7bSTejun Heo return 0;
1702fd7b9f7bSTejun Heo else
1703fd7b9f7bSTejun Heo return -ENOENT;
1704fd7b9f7bSTejun Heo }
1705fd7b9f7bSTejun Heo
1706fd7b9f7bSTejun Heo /**
1707fd7b9f7bSTejun Heo * kernfs_rename_ns - move and rename a kernfs_node
1708324a56e1STejun Heo * @kn: target node
1709fd7b9f7bSTejun Heo * @new_parent: new parent to put @sd under
1710fd7b9f7bSTejun Heo * @new_name: new name
1711fd7b9f7bSTejun Heo * @new_ns: new namespace tag
171224b3e3ddSRandy Dunlap *
171324b3e3ddSRandy Dunlap * Return: %0 on success, -errno on failure.
1714fd7b9f7bSTejun Heo */
kernfs_rename_ns(struct kernfs_node * kn,struct kernfs_node * new_parent,const char * new_name,const void * new_ns)1715324a56e1STejun Heo int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
1716fd7b9f7bSTejun Heo const char *new_name, const void *new_ns)
1717fd7b9f7bSTejun Heo {
17183eef34adSTejun Heo struct kernfs_node *old_parent;
1719393c3714SMinchan Kim struct kernfs_root *root;
17203eef34adSTejun Heo const char *old_name = NULL;
1721fd7b9f7bSTejun Heo int error;
1722fd7b9f7bSTejun Heo
17233eef34adSTejun Heo /* can't move or rename root */
17243eef34adSTejun Heo if (!kn->parent)
17253eef34adSTejun Heo return -EINVAL;
17263eef34adSTejun Heo
1727393c3714SMinchan Kim root = kernfs_root(kn);
1728393c3714SMinchan Kim down_write(&root->kernfs_rwsem);
1729d0ae3d43STejun Heo
1730798c75a0SGreg Kroah-Hartman error = -ENOENT;
1731ea015218SEric W. Biederman if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1732ea015218SEric W. Biederman (new_parent->flags & KERNFS_EMPTY_DIR))
1733798c75a0SGreg Kroah-Hartman goto out;
1734798c75a0SGreg Kroah-Hartman
1735fd7b9f7bSTejun Heo error = 0;
1736adc5e8b5STejun Heo if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1737adc5e8b5STejun Heo (strcmp(kn->name, new_name) == 0))
1738798c75a0SGreg Kroah-Hartman goto out; /* nothing to rename */
1739fd7b9f7bSTejun Heo
1740fd7b9f7bSTejun Heo error = -EEXIST;
1741fd7b9f7bSTejun Heo if (kernfs_find_ns(new_parent, new_name, new_ns))
1742798c75a0SGreg Kroah-Hartman goto out;
1743fd7b9f7bSTejun Heo
1744324a56e1STejun Heo /* rename kernfs_node */
1745adc5e8b5STejun Heo if (strcmp(kn->name, new_name) != 0) {
1746fd7b9f7bSTejun Heo error = -ENOMEM;
174775287a67SAndrzej Hajda new_name = kstrdup_const(new_name, GFP_KERNEL);
1748fd7b9f7bSTejun Heo if (!new_name)
1749798c75a0SGreg Kroah-Hartman goto out;
17503eef34adSTejun Heo } else {
17513eef34adSTejun Heo new_name = NULL;
1752fd7b9f7bSTejun Heo }
1753fd7b9f7bSTejun Heo
1754fd7b9f7bSTejun Heo /*
1755fd7b9f7bSTejun Heo * Move to the appropriate place in the appropriate directories rbtree.
1756fd7b9f7bSTejun Heo */
1757c637b8acSTejun Heo kernfs_unlink_sibling(kn);
1758fd7b9f7bSTejun Heo kernfs_get(new_parent);
17593eef34adSTejun Heo
17603eef34adSTejun Heo /* rename_lock protects ->parent and ->name accessors */
176106fb4736SImran Khan write_lock_irq(&kernfs_rename_lock);
17623eef34adSTejun Heo
17633eef34adSTejun Heo old_parent = kn->parent;
1764adc5e8b5STejun Heo kn->parent = new_parent;
17653eef34adSTejun Heo
17663eef34adSTejun Heo kn->ns = new_ns;
17673eef34adSTejun Heo if (new_name) {
17683eef34adSTejun Heo old_name = kn->name;
17693eef34adSTejun Heo kn->name = new_name;
17703eef34adSTejun Heo }
17713eef34adSTejun Heo
177206fb4736SImran Khan write_unlock_irq(&kernfs_rename_lock);
17733eef34adSTejun Heo
17749561a896STejun Heo kn->hash = kernfs_name_hash(kn->name, kn->ns);
1775c637b8acSTejun Heo kernfs_link_sibling(kn);
1776fd7b9f7bSTejun Heo
17773eef34adSTejun Heo kernfs_put(old_parent);
177875287a67SAndrzej Hajda kfree_const(old_name);
17793eef34adSTejun Heo
1780fd7b9f7bSTejun Heo error = 0;
1781ae34372eSTejun Heo out:
1782393c3714SMinchan Kim up_write(&root->kernfs_rwsem);
1783fd7b9f7bSTejun Heo return error;
1784fd7b9f7bSTejun Heo }
1785fd7b9f7bSTejun Heo
kernfs_dir_fop_release(struct inode * inode,struct file * filp)1786c637b8acSTejun Heo static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
1787fd7b9f7bSTejun Heo {
1788fd7b9f7bSTejun Heo kernfs_put(filp->private_data);
1789fd7b9f7bSTejun Heo return 0;
1790fd7b9f7bSTejun Heo }
1791fd7b9f7bSTejun Heo
kernfs_dir_pos(const void * ns,struct kernfs_node * parent,loff_t hash,struct kernfs_node * pos)1792c637b8acSTejun Heo static struct kernfs_node *kernfs_dir_pos(const void *ns,
1793324a56e1STejun Heo struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
1794fd7b9f7bSTejun Heo {
1795fd7b9f7bSTejun Heo if (pos) {
179681c173cbSTejun Heo int valid = kernfs_active(pos) &&
1797798c75a0SGreg Kroah-Hartman pos->parent == parent && hash == pos->hash;
1798fd7b9f7bSTejun Heo kernfs_put(pos);
1799fd7b9f7bSTejun Heo if (!valid)
1800fd7b9f7bSTejun Heo pos = NULL;
1801fd7b9f7bSTejun Heo }
1802fd7b9f7bSTejun Heo if (!pos && (hash > 1) && (hash < INT_MAX)) {
1803adc5e8b5STejun Heo struct rb_node *node = parent->dir.children.rb_node;
1804fd7b9f7bSTejun Heo while (node) {
1805324a56e1STejun Heo pos = rb_to_kn(node);
1806fd7b9f7bSTejun Heo
1807adc5e8b5STejun Heo if (hash < pos->hash)
1808fd7b9f7bSTejun Heo node = node->rb_left;
1809adc5e8b5STejun Heo else if (hash > pos->hash)
1810fd7b9f7bSTejun Heo node = node->rb_right;
1811fd7b9f7bSTejun Heo else
1812fd7b9f7bSTejun Heo break;
1813fd7b9f7bSTejun Heo }
1814fd7b9f7bSTejun Heo }
1815b9c9dad0STejun Heo /* Skip over entries which are dying/dead or in the wrong namespace */
1816b9c9dad0STejun Heo while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
1817adc5e8b5STejun Heo struct rb_node *node = rb_next(&pos->rb);
1818fd7b9f7bSTejun Heo if (!node)
1819fd7b9f7bSTejun Heo pos = NULL;
1820fd7b9f7bSTejun Heo else
1821324a56e1STejun Heo pos = rb_to_kn(node);
1822fd7b9f7bSTejun Heo }
1823fd7b9f7bSTejun Heo return pos;
1824fd7b9f7bSTejun Heo }
1825fd7b9f7bSTejun Heo
kernfs_dir_next_pos(const void * ns,struct kernfs_node * parent,ino_t ino,struct kernfs_node * pos)1826c637b8acSTejun Heo static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1827324a56e1STejun Heo struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1828fd7b9f7bSTejun Heo {
1829c637b8acSTejun Heo pos = kernfs_dir_pos(ns, parent, ino, pos);
1830b9c9dad0STejun Heo if (pos) {
1831fd7b9f7bSTejun Heo do {
1832adc5e8b5STejun Heo struct rb_node *node = rb_next(&pos->rb);
1833fd7b9f7bSTejun Heo if (!node)
1834fd7b9f7bSTejun Heo pos = NULL;
1835fd7b9f7bSTejun Heo else
1836324a56e1STejun Heo pos = rb_to_kn(node);
1837b9c9dad0STejun Heo } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1838b9c9dad0STejun Heo }
1839fd7b9f7bSTejun Heo return pos;
1840fd7b9f7bSTejun Heo }
1841fd7b9f7bSTejun Heo
kernfs_fop_readdir(struct file * file,struct dir_context * ctx)1842c637b8acSTejun Heo static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1843fd7b9f7bSTejun Heo {
1844fd7b9f7bSTejun Heo struct dentry *dentry = file->f_path.dentry;
1845319ba91dSShaohua Li struct kernfs_node *parent = kernfs_dentry_node(dentry);
1846324a56e1STejun Heo struct kernfs_node *pos = file->private_data;
1847393c3714SMinchan Kim struct kernfs_root *root;
1848fd7b9f7bSTejun Heo const void *ns = NULL;
1849fd7b9f7bSTejun Heo
1850fd7b9f7bSTejun Heo if (!dir_emit_dots(file, ctx))
1851fd7b9f7bSTejun Heo return 0;
1852393c3714SMinchan Kim
1853393c3714SMinchan Kim root = kernfs_root(parent);
1854393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
1855fd7b9f7bSTejun Heo
1856324a56e1STejun Heo if (kernfs_ns_enabled(parent))
1857c525aaddSTejun Heo ns = kernfs_info(dentry->d_sb)->ns;
1858fd7b9f7bSTejun Heo
1859c637b8acSTejun Heo for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1860fd7b9f7bSTejun Heo pos;
1861c637b8acSTejun Heo pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1862adc5e8b5STejun Heo const char *name = pos->name;
1863364595a6SJeff Layton unsigned int type = fs_umode_to_dtype(pos->mode);
1864fd7b9f7bSTejun Heo int len = strlen(name);
186567c0496eSTejun Heo ino_t ino = kernfs_ino(pos);
1866fd7b9f7bSTejun Heo
1867adc5e8b5STejun Heo ctx->pos = pos->hash;
1868fd7b9f7bSTejun Heo file->private_data = pos;
1869fd7b9f7bSTejun Heo kernfs_get(pos);
1870fd7b9f7bSTejun Heo
1871393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1872fd7b9f7bSTejun Heo if (!dir_emit(ctx, name, len, ino, type))
1873fd7b9f7bSTejun Heo return 0;
1874393c3714SMinchan Kim down_read(&root->kernfs_rwsem);
1875fd7b9f7bSTejun Heo }
1876393c3714SMinchan Kim up_read(&root->kernfs_rwsem);
1877fd7b9f7bSTejun Heo file->private_data = NULL;
1878fd7b9f7bSTejun Heo ctx->pos = INT_MAX;
1879fd7b9f7bSTejun Heo return 0;
1880fd7b9f7bSTejun Heo }
1881fd7b9f7bSTejun Heo
1882a797bfc3STejun Heo const struct file_operations kernfs_dir_fops = {
1883fd7b9f7bSTejun Heo .read = generic_read_dir,
18848cb0d2c1SAl Viro .iterate_shared = kernfs_fop_readdir,
1885c637b8acSTejun Heo .release = kernfs_dir_fop_release,
18868cb0d2c1SAl Viro .llseek = generic_file_llseek,
1887fd7b9f7bSTejun Heo };
1888