xref: /OK3568_Linux_fs/kernel/fs/dcache.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fs/dcache.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Complete reimplementation
6*4882a593Smuzhiyun  * (C) 1997 Thomas Schoebel-Theuer,
7*4882a593Smuzhiyun  * with heavy changes by Linus Torvalds
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * Notes on the allocation strategy:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * The dcache is a master of the icache - whenever a dcache entry
14*4882a593Smuzhiyun  * exists, the inode will always exist. "iput()" is done either when
15*4882a593Smuzhiyun  * the dcache entry is deleted or garbage collected.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/ratelimit.h>
19*4882a593Smuzhiyun #include <linux/string.h>
20*4882a593Smuzhiyun #include <linux/mm.h>
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/fscrypt.h>
23*4882a593Smuzhiyun #include <linux/fsnotify.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/init.h>
26*4882a593Smuzhiyun #include <linux/hash.h>
27*4882a593Smuzhiyun #include <linux/cache.h>
28*4882a593Smuzhiyun #include <linux/export.h>
29*4882a593Smuzhiyun #include <linux/security.h>
30*4882a593Smuzhiyun #include <linux/seqlock.h>
31*4882a593Smuzhiyun #include <linux/memblock.h>
32*4882a593Smuzhiyun #include <linux/bit_spinlock.h>
33*4882a593Smuzhiyun #include <linux/rculist_bl.h>
34*4882a593Smuzhiyun #include <linux/list_lru.h>
35*4882a593Smuzhiyun #include "internal.h"
36*4882a593Smuzhiyun #include "mount.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * Usage:
40*4882a593Smuzhiyun  * dcache->d_inode->i_lock protects:
41*4882a593Smuzhiyun  *   - i_dentry, d_u.d_alias, d_inode of aliases
42*4882a593Smuzhiyun  * dcache_hash_bucket lock protects:
43*4882a593Smuzhiyun  *   - the dcache hash table
44*4882a593Smuzhiyun  * s_roots bl list spinlock protects:
45*4882a593Smuzhiyun  *   - the s_roots list (see __d_drop)
46*4882a593Smuzhiyun  * dentry->d_sb->s_dentry_lru_lock protects:
47*4882a593Smuzhiyun  *   - the dcache lru lists and counters
48*4882a593Smuzhiyun  * d_lock protects:
49*4882a593Smuzhiyun  *   - d_flags
50*4882a593Smuzhiyun  *   - d_name
51*4882a593Smuzhiyun  *   - d_lru
52*4882a593Smuzhiyun  *   - d_count
53*4882a593Smuzhiyun  *   - d_unhashed()
54*4882a593Smuzhiyun  *   - d_parent and d_subdirs
55*4882a593Smuzhiyun  *   - childrens' d_child and d_parent
56*4882a593Smuzhiyun  *   - d_u.d_alias, d_inode
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * Ordering:
59*4882a593Smuzhiyun  * dentry->d_inode->i_lock
60*4882a593Smuzhiyun  *   dentry->d_lock
61*4882a593Smuzhiyun  *     dentry->d_sb->s_dentry_lru_lock
62*4882a593Smuzhiyun  *     dcache_hash_bucket lock
63*4882a593Smuzhiyun  *     s_roots lock
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * If there is an ancestor relationship:
66*4882a593Smuzhiyun  * dentry->d_parent->...->d_parent->d_lock
67*4882a593Smuzhiyun  *   ...
68*4882a593Smuzhiyun  *     dentry->d_parent->d_lock
69*4882a593Smuzhiyun  *       dentry->d_lock
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * If no ancestor relationship:
72*4882a593Smuzhiyun  * arbitrary, since it's serialized on rename_lock
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun int sysctl_vfs_cache_pressure __read_mostly = 100;
75*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun EXPORT_SYMBOL(rename_lock);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun static struct kmem_cache *dentry_cache __read_mostly;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun const struct qstr empty_name = QSTR_INIT("", 0);
84*4882a593Smuzhiyun EXPORT_SYMBOL(empty_name);
85*4882a593Smuzhiyun const struct qstr slash_name = QSTR_INIT("/", 1);
86*4882a593Smuzhiyun EXPORT_SYMBOL(slash_name);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * This is the single most critical data structure when it comes
90*4882a593Smuzhiyun  * to the dcache: the hashtable for lookups. Somebody should try
91*4882a593Smuzhiyun  * to make this good - I've just made it work.
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * This hash-function tries to avoid losing too many bits of hash
94*4882a593Smuzhiyun  * information, yet avoid using a prime hash-size or similar.
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun static unsigned int d_hash_shift __read_mostly;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static struct hlist_bl_head *dentry_hashtable __read_mostly;
100*4882a593Smuzhiyun 
d_hash(unsigned int hash)101*4882a593Smuzhiyun static inline struct hlist_bl_head *d_hash(unsigned int hash)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	return dentry_hashtable + (hash >> d_hash_shift);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #define IN_LOOKUP_SHIFT 10
107*4882a593Smuzhiyun static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
108*4882a593Smuzhiyun 
in_lookup_hash(const struct dentry * parent,unsigned int hash)109*4882a593Smuzhiyun static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
110*4882a593Smuzhiyun 					unsigned int hash)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	hash += (unsigned long) parent / L1_CACHE_BYTES;
113*4882a593Smuzhiyun 	return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* Statistics gathering. */
118*4882a593Smuzhiyun struct dentry_stat_t dentry_stat = {
119*4882a593Smuzhiyun 	.age_limit = 45,
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun static DEFINE_PER_CPU(long, nr_dentry);
123*4882a593Smuzhiyun static DEFINE_PER_CPU(long, nr_dentry_unused);
124*4882a593Smuzhiyun static DEFINE_PER_CPU(long, nr_dentry_negative);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun  * Here we resort to our own counters instead of using generic per-cpu counters
130*4882a593Smuzhiyun  * for consistency with what the vfs inode code does. We are expected to harvest
131*4882a593Smuzhiyun  * better code and performance by having our own specialized counters.
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * Please note that the loop is done over all possible CPUs, not over all online
134*4882a593Smuzhiyun  * CPUs. The reason for this is that we don't want to play games with CPUs going
135*4882a593Smuzhiyun  * on and off. If one of them goes off, we will just keep their counters.
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * glommer: See cffbc8a for details, and if you ever intend to change this,
138*4882a593Smuzhiyun  * please update all vfs counters to match.
139*4882a593Smuzhiyun  */
get_nr_dentry(void)140*4882a593Smuzhiyun static long get_nr_dentry(void)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	int i;
143*4882a593Smuzhiyun 	long sum = 0;
144*4882a593Smuzhiyun 	for_each_possible_cpu(i)
145*4882a593Smuzhiyun 		sum += per_cpu(nr_dentry, i);
146*4882a593Smuzhiyun 	return sum < 0 ? 0 : sum;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
get_nr_dentry_unused(void)149*4882a593Smuzhiyun static long get_nr_dentry_unused(void)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	int i;
152*4882a593Smuzhiyun 	long sum = 0;
153*4882a593Smuzhiyun 	for_each_possible_cpu(i)
154*4882a593Smuzhiyun 		sum += per_cpu(nr_dentry_unused, i);
155*4882a593Smuzhiyun 	return sum < 0 ? 0 : sum;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
get_nr_dentry_negative(void)158*4882a593Smuzhiyun static long get_nr_dentry_negative(void)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	int i;
161*4882a593Smuzhiyun 	long sum = 0;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	for_each_possible_cpu(i)
164*4882a593Smuzhiyun 		sum += per_cpu(nr_dentry_negative, i);
165*4882a593Smuzhiyun 	return sum < 0 ? 0 : sum;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
proc_nr_dentry(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)168*4882a593Smuzhiyun int proc_nr_dentry(struct ctl_table *table, int write, void *buffer,
169*4882a593Smuzhiyun 		   size_t *lenp, loff_t *ppos)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	dentry_stat.nr_dentry = get_nr_dentry();
172*4882a593Smuzhiyun 	dentry_stat.nr_unused = get_nr_dentry_unused();
173*4882a593Smuzhiyun 	dentry_stat.nr_negative = get_nr_dentry_negative();
174*4882a593Smuzhiyun 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun  * Compare 2 name strings, return 0 if they match, otherwise non-zero.
180*4882a593Smuzhiyun  * The strings are both count bytes long, and count is non-zero.
181*4882a593Smuzhiyun  */
182*4882a593Smuzhiyun #ifdef CONFIG_DCACHE_WORD_ACCESS
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun #include <asm/word-at-a-time.h>
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun  * NOTE! 'cs' and 'scount' come from a dentry, so it has a
187*4882a593Smuzhiyun  * aligned allocation for this particular component. We don't
188*4882a593Smuzhiyun  * strictly need the load_unaligned_zeropad() safety, but it
189*4882a593Smuzhiyun  * doesn't hurt either.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * In contrast, 'ct' and 'tcount' can be from a pathname, and do
192*4882a593Smuzhiyun  * need the careful unaligned handling.
193*4882a593Smuzhiyun  */
dentry_string_cmp(const unsigned char * cs,const unsigned char * ct,unsigned tcount)194*4882a593Smuzhiyun static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	unsigned long a,b,mask;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	for (;;) {
199*4882a593Smuzhiyun 		a = read_word_at_a_time(cs);
200*4882a593Smuzhiyun 		b = load_unaligned_zeropad(ct);
201*4882a593Smuzhiyun 		if (tcount < sizeof(unsigned long))
202*4882a593Smuzhiyun 			break;
203*4882a593Smuzhiyun 		if (unlikely(a != b))
204*4882a593Smuzhiyun 			return 1;
205*4882a593Smuzhiyun 		cs += sizeof(unsigned long);
206*4882a593Smuzhiyun 		ct += sizeof(unsigned long);
207*4882a593Smuzhiyun 		tcount -= sizeof(unsigned long);
208*4882a593Smuzhiyun 		if (!tcount)
209*4882a593Smuzhiyun 			return 0;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	mask = bytemask_from_count(tcount);
212*4882a593Smuzhiyun 	return unlikely(!!((a ^ b) & mask));
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun #else
216*4882a593Smuzhiyun 
dentry_string_cmp(const unsigned char * cs,const unsigned char * ct,unsigned tcount)217*4882a593Smuzhiyun static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	do {
220*4882a593Smuzhiyun 		if (*cs != *ct)
221*4882a593Smuzhiyun 			return 1;
222*4882a593Smuzhiyun 		cs++;
223*4882a593Smuzhiyun 		ct++;
224*4882a593Smuzhiyun 		tcount--;
225*4882a593Smuzhiyun 	} while (tcount);
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun #endif
230*4882a593Smuzhiyun 
dentry_cmp(const struct dentry * dentry,const unsigned char * ct,unsigned tcount)231*4882a593Smuzhiyun static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	/*
234*4882a593Smuzhiyun 	 * Be careful about RCU walk racing with rename:
235*4882a593Smuzhiyun 	 * use 'READ_ONCE' to fetch the name pointer.
236*4882a593Smuzhiyun 	 *
237*4882a593Smuzhiyun 	 * NOTE! Even if a rename will mean that the length
238*4882a593Smuzhiyun 	 * was not loaded atomically, we don't care. The
239*4882a593Smuzhiyun 	 * RCU walk will check the sequence count eventually,
240*4882a593Smuzhiyun 	 * and catch it. And we won't overrun the buffer,
241*4882a593Smuzhiyun 	 * because we're reading the name pointer atomically,
242*4882a593Smuzhiyun 	 * and a dentry name is guaranteed to be properly
243*4882a593Smuzhiyun 	 * terminated with a NUL byte.
244*4882a593Smuzhiyun 	 *
245*4882a593Smuzhiyun 	 * End result: even if 'len' is wrong, we'll exit
246*4882a593Smuzhiyun 	 * early because the data cannot match (there can
247*4882a593Smuzhiyun 	 * be no NUL in the ct/tcount data)
248*4882a593Smuzhiyun 	 */
249*4882a593Smuzhiyun 	const unsigned char *cs = READ_ONCE(dentry->d_name.name);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return dentry_string_cmp(cs, ct, tcount);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun struct external_name {
255*4882a593Smuzhiyun 	union {
256*4882a593Smuzhiyun 		atomic_t count;
257*4882a593Smuzhiyun 		struct rcu_head head;
258*4882a593Smuzhiyun 	} u;
259*4882a593Smuzhiyun 	unsigned char name[];
260*4882a593Smuzhiyun };
261*4882a593Smuzhiyun 
external_name(struct dentry * dentry)262*4882a593Smuzhiyun static inline struct external_name *external_name(struct dentry *dentry)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	return container_of(dentry->d_name.name, struct external_name, name[0]);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
__d_free(struct rcu_head * head)267*4882a593Smuzhiyun static void __d_free(struct rcu_head *head)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	kmem_cache_free(dentry_cache, dentry);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
__d_free_external(struct rcu_head * head)274*4882a593Smuzhiyun static void __d_free_external(struct rcu_head *head)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
277*4882a593Smuzhiyun 	kfree(external_name(dentry));
278*4882a593Smuzhiyun 	kmem_cache_free(dentry_cache, dentry);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
dname_external(const struct dentry * dentry)281*4882a593Smuzhiyun static inline int dname_external(const struct dentry *dentry)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	return dentry->d_name.name != dentry->d_iname;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
take_dentry_name_snapshot(struct name_snapshot * name,struct dentry * dentry)286*4882a593Smuzhiyun void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
289*4882a593Smuzhiyun 	name->name = dentry->d_name;
290*4882a593Smuzhiyun 	if (unlikely(dname_external(dentry))) {
291*4882a593Smuzhiyun 		atomic_inc(&external_name(dentry)->u.count);
292*4882a593Smuzhiyun 	} else {
293*4882a593Smuzhiyun 		memcpy(name->inline_name, dentry->d_iname,
294*4882a593Smuzhiyun 		       dentry->d_name.len + 1);
295*4882a593Smuzhiyun 		name->name.name = name->inline_name;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun EXPORT_SYMBOL(take_dentry_name_snapshot);
300*4882a593Smuzhiyun 
release_dentry_name_snapshot(struct name_snapshot * name)301*4882a593Smuzhiyun void release_dentry_name_snapshot(struct name_snapshot *name)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	if (unlikely(name->name.name != name->inline_name)) {
304*4882a593Smuzhiyun 		struct external_name *p;
305*4882a593Smuzhiyun 		p = container_of(name->name.name, struct external_name, name[0]);
306*4882a593Smuzhiyun 		if (unlikely(atomic_dec_and_test(&p->u.count)))
307*4882a593Smuzhiyun 			kfree_rcu(p, u.head);
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun EXPORT_SYMBOL(release_dentry_name_snapshot);
311*4882a593Smuzhiyun 
__d_set_inode_and_type(struct dentry * dentry,struct inode * inode,unsigned type_flags)312*4882a593Smuzhiyun static inline void __d_set_inode_and_type(struct dentry *dentry,
313*4882a593Smuzhiyun 					  struct inode *inode,
314*4882a593Smuzhiyun 					  unsigned type_flags)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	unsigned flags;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	dentry->d_inode = inode;
319*4882a593Smuzhiyun 	flags = READ_ONCE(dentry->d_flags);
320*4882a593Smuzhiyun 	flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
321*4882a593Smuzhiyun 	flags |= type_flags;
322*4882a593Smuzhiyun 	smp_store_release(&dentry->d_flags, flags);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
__d_clear_type_and_inode(struct dentry * dentry)325*4882a593Smuzhiyun static inline void __d_clear_type_and_inode(struct dentry *dentry)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	unsigned flags = READ_ONCE(dentry->d_flags);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
330*4882a593Smuzhiyun 	WRITE_ONCE(dentry->d_flags, flags);
331*4882a593Smuzhiyun 	dentry->d_inode = NULL;
332*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_LRU_LIST)
333*4882a593Smuzhiyun 		this_cpu_inc(nr_dentry_negative);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
dentry_free(struct dentry * dentry)336*4882a593Smuzhiyun static void dentry_free(struct dentry *dentry)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
339*4882a593Smuzhiyun 	if (unlikely(dname_external(dentry))) {
340*4882a593Smuzhiyun 		struct external_name *p = external_name(dentry);
341*4882a593Smuzhiyun 		if (likely(atomic_dec_and_test(&p->u.count))) {
342*4882a593Smuzhiyun 			call_rcu(&dentry->d_u.d_rcu, __d_free_external);
343*4882a593Smuzhiyun 			return;
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 	/* if dentry was never visible to RCU, immediate free is OK */
347*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_NORCU)
348*4882a593Smuzhiyun 		__d_free(&dentry->d_u.d_rcu);
349*4882a593Smuzhiyun 	else
350*4882a593Smuzhiyun 		call_rcu(&dentry->d_u.d_rcu, __d_free);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun /*
354*4882a593Smuzhiyun  * Release the dentry's inode, using the filesystem
355*4882a593Smuzhiyun  * d_iput() operation if defined.
356*4882a593Smuzhiyun  */
dentry_unlink_inode(struct dentry * dentry)357*4882a593Smuzhiyun static void dentry_unlink_inode(struct dentry * dentry)
358*4882a593Smuzhiyun 	__releases(dentry->d_lock)
359*4882a593Smuzhiyun 	__releases(dentry->d_inode->i_lock)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	struct inode *inode = dentry->d_inode;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	raw_write_seqcount_begin(&dentry->d_seq);
364*4882a593Smuzhiyun 	__d_clear_type_and_inode(dentry);
365*4882a593Smuzhiyun 	hlist_del_init(&dentry->d_u.d_alias);
366*4882a593Smuzhiyun 	raw_write_seqcount_end(&dentry->d_seq);
367*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
368*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
369*4882a593Smuzhiyun 	if (!inode->i_nlink)
370*4882a593Smuzhiyun 		fsnotify_inoderemove(inode);
371*4882a593Smuzhiyun 	if (dentry->d_op && dentry->d_op->d_iput)
372*4882a593Smuzhiyun 		dentry->d_op->d_iput(dentry, inode);
373*4882a593Smuzhiyun 	else
374*4882a593Smuzhiyun 		iput(inode);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun  * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
379*4882a593Smuzhiyun  * is in use - which includes both the "real" per-superblock
380*4882a593Smuzhiyun  * LRU list _and_ the DCACHE_SHRINK_LIST use.
381*4882a593Smuzhiyun  *
382*4882a593Smuzhiyun  * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
383*4882a593Smuzhiyun  * on the shrink list (ie not on the superblock LRU list).
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * The per-cpu "nr_dentry_unused" counters are updated with
386*4882a593Smuzhiyun  * the DCACHE_LRU_LIST bit.
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * The per-cpu "nr_dentry_negative" counters are only updated
389*4882a593Smuzhiyun  * when deleted from or added to the per-superblock LRU list, not
390*4882a593Smuzhiyun  * from/to the shrink list. That is to avoid an unneeded dec/inc
391*4882a593Smuzhiyun  * pair when moving from LRU to shrink list in select_collect().
392*4882a593Smuzhiyun  *
393*4882a593Smuzhiyun  * These helper functions make sure we always follow the
394*4882a593Smuzhiyun  * rules. d_lock must be held by the caller.
395*4882a593Smuzhiyun  */
396*4882a593Smuzhiyun #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
d_lru_add(struct dentry * dentry)397*4882a593Smuzhiyun static void d_lru_add(struct dentry *dentry)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, 0);
400*4882a593Smuzhiyun 	dentry->d_flags |= DCACHE_LRU_LIST;
401*4882a593Smuzhiyun 	this_cpu_inc(nr_dentry_unused);
402*4882a593Smuzhiyun 	if (d_is_negative(dentry))
403*4882a593Smuzhiyun 		this_cpu_inc(nr_dentry_negative);
404*4882a593Smuzhiyun 	WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
d_lru_del(struct dentry * dentry)407*4882a593Smuzhiyun static void d_lru_del(struct dentry *dentry)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
410*4882a593Smuzhiyun 	dentry->d_flags &= ~DCACHE_LRU_LIST;
411*4882a593Smuzhiyun 	this_cpu_dec(nr_dentry_unused);
412*4882a593Smuzhiyun 	if (d_is_negative(dentry))
413*4882a593Smuzhiyun 		this_cpu_dec(nr_dentry_negative);
414*4882a593Smuzhiyun 	WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
d_shrink_del(struct dentry * dentry)417*4882a593Smuzhiyun static void d_shrink_del(struct dentry *dentry)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
420*4882a593Smuzhiyun 	list_del_init(&dentry->d_lru);
421*4882a593Smuzhiyun 	dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
422*4882a593Smuzhiyun 	this_cpu_dec(nr_dentry_unused);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
d_shrink_add(struct dentry * dentry,struct list_head * list)425*4882a593Smuzhiyun static void d_shrink_add(struct dentry *dentry, struct list_head *list)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, 0);
428*4882a593Smuzhiyun 	list_add(&dentry->d_lru, list);
429*4882a593Smuzhiyun 	dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
430*4882a593Smuzhiyun 	this_cpu_inc(nr_dentry_unused);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun  * These can only be called under the global LRU lock, ie during the
435*4882a593Smuzhiyun  * callback for freeing the LRU list. "isolate" removes it from the
436*4882a593Smuzhiyun  * LRU lists entirely, while shrink_move moves it to the indicated
437*4882a593Smuzhiyun  * private list.
438*4882a593Smuzhiyun  */
d_lru_isolate(struct list_lru_one * lru,struct dentry * dentry)439*4882a593Smuzhiyun static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
442*4882a593Smuzhiyun 	dentry->d_flags &= ~DCACHE_LRU_LIST;
443*4882a593Smuzhiyun 	this_cpu_dec(nr_dentry_unused);
444*4882a593Smuzhiyun 	if (d_is_negative(dentry))
445*4882a593Smuzhiyun 		this_cpu_dec(nr_dentry_negative);
446*4882a593Smuzhiyun 	list_lru_isolate(lru, &dentry->d_lru);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
d_lru_shrink_move(struct list_lru_one * lru,struct dentry * dentry,struct list_head * list)449*4882a593Smuzhiyun static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
450*4882a593Smuzhiyun 			      struct list_head *list)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
453*4882a593Smuzhiyun 	dentry->d_flags |= DCACHE_SHRINK_LIST;
454*4882a593Smuzhiyun 	if (d_is_negative(dentry))
455*4882a593Smuzhiyun 		this_cpu_dec(nr_dentry_negative);
456*4882a593Smuzhiyun 	list_lru_isolate_move(lru, &dentry->d_lru, list);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /**
460*4882a593Smuzhiyun  * d_drop - drop a dentry
461*4882a593Smuzhiyun  * @dentry: dentry to drop
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
464*4882a593Smuzhiyun  * be found through a VFS lookup any more. Note that this is different from
465*4882a593Smuzhiyun  * deleting the dentry - d_delete will try to mark the dentry negative if
466*4882a593Smuzhiyun  * possible, giving a successful _negative_ lookup, while d_drop will
467*4882a593Smuzhiyun  * just make the cache lookup fail.
468*4882a593Smuzhiyun  *
469*4882a593Smuzhiyun  * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
470*4882a593Smuzhiyun  * reason (NFS timeouts or autofs deletes).
471*4882a593Smuzhiyun  *
472*4882a593Smuzhiyun  * __d_drop requires dentry->d_lock
473*4882a593Smuzhiyun  * ___d_drop doesn't mark dentry as "unhashed"
474*4882a593Smuzhiyun  *   (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
475*4882a593Smuzhiyun  */
___d_drop(struct dentry * dentry)476*4882a593Smuzhiyun static void ___d_drop(struct dentry *dentry)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct hlist_bl_head *b;
479*4882a593Smuzhiyun 	/*
480*4882a593Smuzhiyun 	 * Hashed dentries are normally on the dentry hashtable,
481*4882a593Smuzhiyun 	 * with the exception of those newly allocated by
482*4882a593Smuzhiyun 	 * d_obtain_root, which are always IS_ROOT:
483*4882a593Smuzhiyun 	 */
484*4882a593Smuzhiyun 	if (unlikely(IS_ROOT(dentry)))
485*4882a593Smuzhiyun 		b = &dentry->d_sb->s_roots;
486*4882a593Smuzhiyun 	else
487*4882a593Smuzhiyun 		b = d_hash(dentry->d_name.hash);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	hlist_bl_lock(b);
490*4882a593Smuzhiyun 	__hlist_bl_del(&dentry->d_hash);
491*4882a593Smuzhiyun 	hlist_bl_unlock(b);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
__d_drop(struct dentry * dentry)494*4882a593Smuzhiyun void __d_drop(struct dentry *dentry)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	if (!d_unhashed(dentry)) {
497*4882a593Smuzhiyun 		___d_drop(dentry);
498*4882a593Smuzhiyun 		dentry->d_hash.pprev = NULL;
499*4882a593Smuzhiyun 		write_seqcount_invalidate(&dentry->d_seq);
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun EXPORT_SYMBOL(__d_drop);
503*4882a593Smuzhiyun 
d_drop(struct dentry * dentry)504*4882a593Smuzhiyun void d_drop(struct dentry *dentry)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
507*4882a593Smuzhiyun 	__d_drop(dentry);
508*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun EXPORT_SYMBOL(d_drop);
511*4882a593Smuzhiyun 
dentry_unlist(struct dentry * dentry,struct dentry * parent)512*4882a593Smuzhiyun static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	struct dentry *next;
515*4882a593Smuzhiyun 	/*
516*4882a593Smuzhiyun 	 * Inform d_walk() and shrink_dentry_list() that we are no longer
517*4882a593Smuzhiyun 	 * attached to the dentry tree
518*4882a593Smuzhiyun 	 */
519*4882a593Smuzhiyun 	dentry->d_flags |= DCACHE_DENTRY_KILLED;
520*4882a593Smuzhiyun 	if (unlikely(list_empty(&dentry->d_child)))
521*4882a593Smuzhiyun 		return;
522*4882a593Smuzhiyun 	__list_del_entry(&dentry->d_child);
523*4882a593Smuzhiyun 	/*
524*4882a593Smuzhiyun 	 * Cursors can move around the list of children.  While we'd been
525*4882a593Smuzhiyun 	 * a normal list member, it didn't matter - ->d_child.next would've
526*4882a593Smuzhiyun 	 * been updated.  However, from now on it won't be and for the
527*4882a593Smuzhiyun 	 * things like d_walk() it might end up with a nasty surprise.
528*4882a593Smuzhiyun 	 * Normally d_walk() doesn't care about cursors moving around -
529*4882a593Smuzhiyun 	 * ->d_lock on parent prevents that and since a cursor has no children
530*4882a593Smuzhiyun 	 * of its own, we get through it without ever unlocking the parent.
531*4882a593Smuzhiyun 	 * There is one exception, though - if we ascend from a child that
532*4882a593Smuzhiyun 	 * gets killed as soon as we unlock it, the next sibling is found
533*4882a593Smuzhiyun 	 * using the value left in its ->d_child.next.  And if _that_
534*4882a593Smuzhiyun 	 * pointed to a cursor, and cursor got moved (e.g. by lseek())
535*4882a593Smuzhiyun 	 * before d_walk() regains parent->d_lock, we'll end up skipping
536*4882a593Smuzhiyun 	 * everything the cursor had been moved past.
537*4882a593Smuzhiyun 	 *
538*4882a593Smuzhiyun 	 * Solution: make sure that the pointer left behind in ->d_child.next
539*4882a593Smuzhiyun 	 * points to something that won't be moving around.  I.e. skip the
540*4882a593Smuzhiyun 	 * cursors.
541*4882a593Smuzhiyun 	 */
542*4882a593Smuzhiyun 	while (dentry->d_child.next != &parent->d_subdirs) {
543*4882a593Smuzhiyun 		next = list_entry(dentry->d_child.next, struct dentry, d_child);
544*4882a593Smuzhiyun 		if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
545*4882a593Smuzhiyun 			break;
546*4882a593Smuzhiyun 		dentry->d_child.next = next->d_child.next;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun 
__dentry_kill(struct dentry * dentry)550*4882a593Smuzhiyun static void __dentry_kill(struct dentry *dentry)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	struct dentry *parent = NULL;
553*4882a593Smuzhiyun 	bool can_free = true;
554*4882a593Smuzhiyun 	if (!IS_ROOT(dentry))
555*4882a593Smuzhiyun 		parent = dentry->d_parent;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	/*
558*4882a593Smuzhiyun 	 * The dentry is now unrecoverably dead to the world.
559*4882a593Smuzhiyun 	 */
560*4882a593Smuzhiyun 	lockref_mark_dead(&dentry->d_lockref);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/*
563*4882a593Smuzhiyun 	 * inform the fs via d_prune that this dentry is about to be
564*4882a593Smuzhiyun 	 * unhashed and destroyed.
565*4882a593Smuzhiyun 	 */
566*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_OP_PRUNE)
567*4882a593Smuzhiyun 		dentry->d_op->d_prune(dentry);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_LRU_LIST) {
570*4882a593Smuzhiyun 		if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
571*4882a593Smuzhiyun 			d_lru_del(dentry);
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 	/* if it was on the hash then remove it */
574*4882a593Smuzhiyun 	__d_drop(dentry);
575*4882a593Smuzhiyun 	dentry_unlist(dentry, parent);
576*4882a593Smuzhiyun 	if (parent)
577*4882a593Smuzhiyun 		spin_unlock(&parent->d_lock);
578*4882a593Smuzhiyun 	if (dentry->d_inode)
579*4882a593Smuzhiyun 		dentry_unlink_inode(dentry);
580*4882a593Smuzhiyun 	else
581*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
582*4882a593Smuzhiyun 	this_cpu_dec(nr_dentry);
583*4882a593Smuzhiyun 	if (dentry->d_op && dentry->d_op->d_release)
584*4882a593Smuzhiyun 		dentry->d_op->d_release(dentry);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
587*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_SHRINK_LIST) {
588*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_MAY_FREE;
589*4882a593Smuzhiyun 		can_free = false;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
592*4882a593Smuzhiyun 	if (likely(can_free))
593*4882a593Smuzhiyun 		dentry_free(dentry);
594*4882a593Smuzhiyun 	cond_resched();
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun 
__lock_parent(struct dentry * dentry)597*4882a593Smuzhiyun static struct dentry *__lock_parent(struct dentry *dentry)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun 	struct dentry *parent;
600*4882a593Smuzhiyun 	rcu_read_lock();
601*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
602*4882a593Smuzhiyun again:
603*4882a593Smuzhiyun 	parent = READ_ONCE(dentry->d_parent);
604*4882a593Smuzhiyun 	spin_lock(&parent->d_lock);
605*4882a593Smuzhiyun 	/*
606*4882a593Smuzhiyun 	 * We can't blindly lock dentry until we are sure
607*4882a593Smuzhiyun 	 * that we won't violate the locking order.
608*4882a593Smuzhiyun 	 * Any changes of dentry->d_parent must have
609*4882a593Smuzhiyun 	 * been done with parent->d_lock held, so
610*4882a593Smuzhiyun 	 * spin_lock() above is enough of a barrier
611*4882a593Smuzhiyun 	 * for checking if it's still our child.
612*4882a593Smuzhiyun 	 */
613*4882a593Smuzhiyun 	if (unlikely(parent != dentry->d_parent)) {
614*4882a593Smuzhiyun 		spin_unlock(&parent->d_lock);
615*4882a593Smuzhiyun 		goto again;
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun 	rcu_read_unlock();
618*4882a593Smuzhiyun 	if (parent != dentry)
619*4882a593Smuzhiyun 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
620*4882a593Smuzhiyun 	else
621*4882a593Smuzhiyun 		parent = NULL;
622*4882a593Smuzhiyun 	return parent;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
lock_parent(struct dentry * dentry)625*4882a593Smuzhiyun static inline struct dentry *lock_parent(struct dentry *dentry)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct dentry *parent = dentry->d_parent;
628*4882a593Smuzhiyun 	if (IS_ROOT(dentry))
629*4882a593Smuzhiyun 		return NULL;
630*4882a593Smuzhiyun 	if (likely(spin_trylock(&parent->d_lock)))
631*4882a593Smuzhiyun 		return parent;
632*4882a593Smuzhiyun 	return __lock_parent(dentry);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
retain_dentry(struct dentry * dentry)635*4882a593Smuzhiyun static inline bool retain_dentry(struct dentry *dentry)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	WARN_ON(d_in_lookup(dentry));
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* Unreachable? Get rid of it */
640*4882a593Smuzhiyun 	if (unlikely(d_unhashed(dentry)))
641*4882a593Smuzhiyun 		return false;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
644*4882a593Smuzhiyun 		return false;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
647*4882a593Smuzhiyun 		if (dentry->d_op->d_delete(dentry))
648*4882a593Smuzhiyun 			return false;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	if (unlikely(dentry->d_flags & DCACHE_DONTCACHE))
652*4882a593Smuzhiyun 		return false;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/* retain; LRU fodder */
655*4882a593Smuzhiyun 	dentry->d_lockref.count--;
656*4882a593Smuzhiyun 	if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
657*4882a593Smuzhiyun 		d_lru_add(dentry);
658*4882a593Smuzhiyun 	else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
659*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_REFERENCED;
660*4882a593Smuzhiyun 	return true;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun 
d_mark_dontcache(struct inode * inode)663*4882a593Smuzhiyun void d_mark_dontcache(struct inode *inode)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct dentry *de;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
668*4882a593Smuzhiyun 	hlist_for_each_entry(de, &inode->i_dentry, d_u.d_alias) {
669*4882a593Smuzhiyun 		spin_lock(&de->d_lock);
670*4882a593Smuzhiyun 		de->d_flags |= DCACHE_DONTCACHE;
671*4882a593Smuzhiyun 		spin_unlock(&de->d_lock);
672*4882a593Smuzhiyun 	}
673*4882a593Smuzhiyun 	inode->i_state |= I_DONTCACHE;
674*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun EXPORT_SYMBOL(d_mark_dontcache);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /*
679*4882a593Smuzhiyun  * Finish off a dentry we've decided to kill.
680*4882a593Smuzhiyun  * dentry->d_lock must be held, returns with it unlocked.
681*4882a593Smuzhiyun  * Returns dentry requiring refcount drop, or NULL if we're done.
682*4882a593Smuzhiyun  */
dentry_kill(struct dentry * dentry)683*4882a593Smuzhiyun static struct dentry *dentry_kill(struct dentry *dentry)
684*4882a593Smuzhiyun 	__releases(dentry->d_lock)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	struct inode *inode = dentry->d_inode;
687*4882a593Smuzhiyun 	struct dentry *parent = NULL;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	if (inode && unlikely(!spin_trylock(&inode->i_lock)))
690*4882a593Smuzhiyun 		goto slow_positive;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	if (!IS_ROOT(dentry)) {
693*4882a593Smuzhiyun 		parent = dentry->d_parent;
694*4882a593Smuzhiyun 		if (unlikely(!spin_trylock(&parent->d_lock))) {
695*4882a593Smuzhiyun 			parent = __lock_parent(dentry);
696*4882a593Smuzhiyun 			if (likely(inode || !dentry->d_inode))
697*4882a593Smuzhiyun 				goto got_locks;
698*4882a593Smuzhiyun 			/* negative that became positive */
699*4882a593Smuzhiyun 			if (parent)
700*4882a593Smuzhiyun 				spin_unlock(&parent->d_lock);
701*4882a593Smuzhiyun 			inode = dentry->d_inode;
702*4882a593Smuzhiyun 			goto slow_positive;
703*4882a593Smuzhiyun 		}
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 	__dentry_kill(dentry);
706*4882a593Smuzhiyun 	return parent;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun slow_positive:
709*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
710*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
711*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
712*4882a593Smuzhiyun 	parent = lock_parent(dentry);
713*4882a593Smuzhiyun got_locks:
714*4882a593Smuzhiyun 	if (unlikely(dentry->d_lockref.count != 1)) {
715*4882a593Smuzhiyun 		dentry->d_lockref.count--;
716*4882a593Smuzhiyun 	} else if (likely(!retain_dentry(dentry))) {
717*4882a593Smuzhiyun 		__dentry_kill(dentry);
718*4882a593Smuzhiyun 		return parent;
719*4882a593Smuzhiyun 	}
720*4882a593Smuzhiyun 	/* we are keeping it, after all */
721*4882a593Smuzhiyun 	if (inode)
722*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
723*4882a593Smuzhiyun 	if (parent)
724*4882a593Smuzhiyun 		spin_unlock(&parent->d_lock);
725*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
726*4882a593Smuzhiyun 	return NULL;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun /*
730*4882a593Smuzhiyun  * Try to do a lockless dput(), and return whether that was successful.
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * If unsuccessful, we return false, having already taken the dentry lock.
733*4882a593Smuzhiyun  *
734*4882a593Smuzhiyun  * The caller needs to hold the RCU read lock, so that the dentry is
735*4882a593Smuzhiyun  * guaranteed to stay around even if the refcount goes down to zero!
736*4882a593Smuzhiyun  */
fast_dput(struct dentry * dentry)737*4882a593Smuzhiyun static inline bool fast_dput(struct dentry *dentry)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	int ret;
740*4882a593Smuzhiyun 	unsigned int d_flags;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	/*
743*4882a593Smuzhiyun 	 * If we have a d_op->d_delete() operation, we sould not
744*4882a593Smuzhiyun 	 * let the dentry count go to zero, so use "put_or_lock".
745*4882a593Smuzhiyun 	 */
746*4882a593Smuzhiyun 	if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
747*4882a593Smuzhiyun 		return lockref_put_or_lock(&dentry->d_lockref);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/*
750*4882a593Smuzhiyun 	 * .. otherwise, we can try to just decrement the
751*4882a593Smuzhiyun 	 * lockref optimistically.
752*4882a593Smuzhiyun 	 */
753*4882a593Smuzhiyun 	ret = lockref_put_return(&dentry->d_lockref);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	/*
756*4882a593Smuzhiyun 	 * If the lockref_put_return() failed due to the lock being held
757*4882a593Smuzhiyun 	 * by somebody else, the fast path has failed. We will need to
758*4882a593Smuzhiyun 	 * get the lock, and then check the count again.
759*4882a593Smuzhiyun 	 */
760*4882a593Smuzhiyun 	if (unlikely(ret < 0)) {
761*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
762*4882a593Smuzhiyun 		if (dentry->d_lockref.count > 1) {
763*4882a593Smuzhiyun 			dentry->d_lockref.count--;
764*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
765*4882a593Smuzhiyun 			return true;
766*4882a593Smuzhiyun 		}
767*4882a593Smuzhiyun 		return false;
768*4882a593Smuzhiyun 	}
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	/*
771*4882a593Smuzhiyun 	 * If we weren't the last ref, we're done.
772*4882a593Smuzhiyun 	 */
773*4882a593Smuzhiyun 	if (ret)
774*4882a593Smuzhiyun 		return true;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	/*
777*4882a593Smuzhiyun 	 * Careful, careful. The reference count went down
778*4882a593Smuzhiyun 	 * to zero, but we don't hold the dentry lock, so
779*4882a593Smuzhiyun 	 * somebody else could get it again, and do another
780*4882a593Smuzhiyun 	 * dput(), and we need to not race with that.
781*4882a593Smuzhiyun 	 *
782*4882a593Smuzhiyun 	 * However, there is a very special and common case
783*4882a593Smuzhiyun 	 * where we don't care, because there is nothing to
784*4882a593Smuzhiyun 	 * do: the dentry is still hashed, it does not have
785*4882a593Smuzhiyun 	 * a 'delete' op, and it's referenced and already on
786*4882a593Smuzhiyun 	 * the LRU list.
787*4882a593Smuzhiyun 	 *
788*4882a593Smuzhiyun 	 * NOTE! Since we aren't locked, these values are
789*4882a593Smuzhiyun 	 * not "stable". However, it is sufficient that at
790*4882a593Smuzhiyun 	 * some point after we dropped the reference the
791*4882a593Smuzhiyun 	 * dentry was hashed and the flags had the proper
792*4882a593Smuzhiyun 	 * value. Other dentry users may have re-gotten
793*4882a593Smuzhiyun 	 * a reference to the dentry and change that, but
794*4882a593Smuzhiyun 	 * our work is done - we can leave the dentry
795*4882a593Smuzhiyun 	 * around with a zero refcount.
796*4882a593Smuzhiyun 	 */
797*4882a593Smuzhiyun 	smp_rmb();
798*4882a593Smuzhiyun 	d_flags = READ_ONCE(dentry->d_flags);
799*4882a593Smuzhiyun 	d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	/* Nothing to do? Dropping the reference was all we needed? */
802*4882a593Smuzhiyun 	if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
803*4882a593Smuzhiyun 		return true;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	/*
806*4882a593Smuzhiyun 	 * Not the fast normal case? Get the lock. We've already decremented
807*4882a593Smuzhiyun 	 * the refcount, but we'll need to re-check the situation after
808*4882a593Smuzhiyun 	 * getting the lock.
809*4882a593Smuzhiyun 	 */
810*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	/*
813*4882a593Smuzhiyun 	 * Did somebody else grab a reference to it in the meantime, and
814*4882a593Smuzhiyun 	 * we're no longer the last user after all? Alternatively, somebody
815*4882a593Smuzhiyun 	 * else could have killed it and marked it dead. Either way, we
816*4882a593Smuzhiyun 	 * don't need to do anything else.
817*4882a593Smuzhiyun 	 */
818*4882a593Smuzhiyun 	if (dentry->d_lockref.count) {
819*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
820*4882a593Smuzhiyun 		return true;
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	/*
824*4882a593Smuzhiyun 	 * Re-get the reference we optimistically dropped. We hold the
825*4882a593Smuzhiyun 	 * lock, and we just tested that it was zero, so we can just
826*4882a593Smuzhiyun 	 * set it to 1.
827*4882a593Smuzhiyun 	 */
828*4882a593Smuzhiyun 	dentry->d_lockref.count = 1;
829*4882a593Smuzhiyun 	return false;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun /*
834*4882a593Smuzhiyun  * This is dput
835*4882a593Smuzhiyun  *
836*4882a593Smuzhiyun  * This is complicated by the fact that we do not want to put
837*4882a593Smuzhiyun  * dentries that are no longer on any hash chain on the unused
838*4882a593Smuzhiyun  * list: we'd much rather just get rid of them immediately.
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * However, that implies that we have to traverse the dentry
841*4882a593Smuzhiyun  * tree upwards to the parents which might _also_ now be
842*4882a593Smuzhiyun  * scheduled for deletion (it may have been only waiting for
843*4882a593Smuzhiyun  * its last child to go away).
844*4882a593Smuzhiyun  *
845*4882a593Smuzhiyun  * This tail recursion is done by hand as we don't want to depend
846*4882a593Smuzhiyun  * on the compiler to always get this right (gcc generally doesn't).
847*4882a593Smuzhiyun  * Real recursion would eat up our stack space.
848*4882a593Smuzhiyun  */
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun /*
851*4882a593Smuzhiyun  * dput - release a dentry
852*4882a593Smuzhiyun  * @dentry: dentry to release
853*4882a593Smuzhiyun  *
854*4882a593Smuzhiyun  * Release a dentry. This will drop the usage count and if appropriate
855*4882a593Smuzhiyun  * call the dentry unlink method as well as removing it from the queues and
856*4882a593Smuzhiyun  * releasing its resources. If the parent dentries were scheduled for release
857*4882a593Smuzhiyun  * they too may now get deleted.
858*4882a593Smuzhiyun  */
dput(struct dentry * dentry)859*4882a593Smuzhiyun void dput(struct dentry *dentry)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	while (dentry) {
862*4882a593Smuzhiyun 		might_sleep();
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 		rcu_read_lock();
865*4882a593Smuzhiyun 		if (likely(fast_dput(dentry))) {
866*4882a593Smuzhiyun 			rcu_read_unlock();
867*4882a593Smuzhiyun 			return;
868*4882a593Smuzhiyun 		}
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 		/* Slow case: now with the dentry lock held */
871*4882a593Smuzhiyun 		rcu_read_unlock();
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 		if (likely(retain_dentry(dentry))) {
874*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
875*4882a593Smuzhiyun 			return;
876*4882a593Smuzhiyun 		}
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 		dentry = dentry_kill(dentry);
879*4882a593Smuzhiyun 	}
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun EXPORT_SYMBOL(dput);
882*4882a593Smuzhiyun 
__dput_to_list(struct dentry * dentry,struct list_head * list)883*4882a593Smuzhiyun static void __dput_to_list(struct dentry *dentry, struct list_head *list)
884*4882a593Smuzhiyun __must_hold(&dentry->d_lock)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_SHRINK_LIST) {
887*4882a593Smuzhiyun 		/* let the owner of the list it's on deal with it */
888*4882a593Smuzhiyun 		--dentry->d_lockref.count;
889*4882a593Smuzhiyun 	} else {
890*4882a593Smuzhiyun 		if (dentry->d_flags & DCACHE_LRU_LIST)
891*4882a593Smuzhiyun 			d_lru_del(dentry);
892*4882a593Smuzhiyun 		if (!--dentry->d_lockref.count)
893*4882a593Smuzhiyun 			d_shrink_add(dentry, list);
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun }
896*4882a593Smuzhiyun 
dput_to_list(struct dentry * dentry,struct list_head * list)897*4882a593Smuzhiyun void dput_to_list(struct dentry *dentry, struct list_head *list)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun 	rcu_read_lock();
900*4882a593Smuzhiyun 	if (likely(fast_dput(dentry))) {
901*4882a593Smuzhiyun 		rcu_read_unlock();
902*4882a593Smuzhiyun 		return;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 	rcu_read_unlock();
905*4882a593Smuzhiyun 	if (!retain_dentry(dentry))
906*4882a593Smuzhiyun 		__dput_to_list(dentry, list);
907*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun /* This must be called with d_lock held */
__dget_dlock(struct dentry * dentry)911*4882a593Smuzhiyun static inline void __dget_dlock(struct dentry *dentry)
912*4882a593Smuzhiyun {
913*4882a593Smuzhiyun 	dentry->d_lockref.count++;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun 
__dget(struct dentry * dentry)916*4882a593Smuzhiyun static inline void __dget(struct dentry *dentry)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun 	lockref_get(&dentry->d_lockref);
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
dget_parent(struct dentry * dentry)921*4882a593Smuzhiyun struct dentry *dget_parent(struct dentry *dentry)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	int gotref;
924*4882a593Smuzhiyun 	struct dentry *ret;
925*4882a593Smuzhiyun 	unsigned seq;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	/*
928*4882a593Smuzhiyun 	 * Do optimistic parent lookup without any
929*4882a593Smuzhiyun 	 * locking.
930*4882a593Smuzhiyun 	 */
931*4882a593Smuzhiyun 	rcu_read_lock();
932*4882a593Smuzhiyun 	seq = raw_seqcount_begin(&dentry->d_seq);
933*4882a593Smuzhiyun 	ret = READ_ONCE(dentry->d_parent);
934*4882a593Smuzhiyun 	gotref = lockref_get_not_zero(&ret->d_lockref);
935*4882a593Smuzhiyun 	rcu_read_unlock();
936*4882a593Smuzhiyun 	if (likely(gotref)) {
937*4882a593Smuzhiyun 		if (!read_seqcount_retry(&dentry->d_seq, seq))
938*4882a593Smuzhiyun 			return ret;
939*4882a593Smuzhiyun 		dput(ret);
940*4882a593Smuzhiyun 	}
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun repeat:
943*4882a593Smuzhiyun 	/*
944*4882a593Smuzhiyun 	 * Don't need rcu_dereference because we re-check it was correct under
945*4882a593Smuzhiyun 	 * the lock.
946*4882a593Smuzhiyun 	 */
947*4882a593Smuzhiyun 	rcu_read_lock();
948*4882a593Smuzhiyun 	ret = dentry->d_parent;
949*4882a593Smuzhiyun 	spin_lock(&ret->d_lock);
950*4882a593Smuzhiyun 	if (unlikely(ret != dentry->d_parent)) {
951*4882a593Smuzhiyun 		spin_unlock(&ret->d_lock);
952*4882a593Smuzhiyun 		rcu_read_unlock();
953*4882a593Smuzhiyun 		goto repeat;
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 	rcu_read_unlock();
956*4882a593Smuzhiyun 	BUG_ON(!ret->d_lockref.count);
957*4882a593Smuzhiyun 	ret->d_lockref.count++;
958*4882a593Smuzhiyun 	spin_unlock(&ret->d_lock);
959*4882a593Smuzhiyun 	return ret;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun EXPORT_SYMBOL(dget_parent);
962*4882a593Smuzhiyun 
__d_find_any_alias(struct inode * inode)963*4882a593Smuzhiyun static struct dentry * __d_find_any_alias(struct inode *inode)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	struct dentry *alias;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	if (hlist_empty(&inode->i_dentry))
968*4882a593Smuzhiyun 		return NULL;
969*4882a593Smuzhiyun 	alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
970*4882a593Smuzhiyun 	__dget(alias);
971*4882a593Smuzhiyun 	return alias;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun /**
975*4882a593Smuzhiyun  * d_find_any_alias - find any alias for a given inode
976*4882a593Smuzhiyun  * @inode: inode to find an alias for
977*4882a593Smuzhiyun  *
978*4882a593Smuzhiyun  * If any aliases exist for the given inode, take and return a
979*4882a593Smuzhiyun  * reference for one of them.  If no aliases exist, return %NULL.
980*4882a593Smuzhiyun  */
d_find_any_alias(struct inode * inode)981*4882a593Smuzhiyun struct dentry *d_find_any_alias(struct inode *inode)
982*4882a593Smuzhiyun {
983*4882a593Smuzhiyun 	struct dentry *de;
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
986*4882a593Smuzhiyun 	de = __d_find_any_alias(inode);
987*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
988*4882a593Smuzhiyun 	return de;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun EXPORT_SYMBOL(d_find_any_alias);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun /**
993*4882a593Smuzhiyun  * d_find_alias - grab a hashed alias of inode
994*4882a593Smuzhiyun  * @inode: inode in question
995*4882a593Smuzhiyun  *
996*4882a593Smuzhiyun  * If inode has a hashed alias, or is a directory and has any alias,
997*4882a593Smuzhiyun  * acquire the reference to alias and return it. Otherwise return NULL.
998*4882a593Smuzhiyun  * Notice that if inode is a directory there can be only one alias and
999*4882a593Smuzhiyun  * it can be unhashed only if it has no children, or if it is the root
1000*4882a593Smuzhiyun  * of a filesystem, or if the directory was renamed and d_revalidate
1001*4882a593Smuzhiyun  * was the first vfs operation to notice.
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
1004*4882a593Smuzhiyun  * any other hashed alias over that one.
1005*4882a593Smuzhiyun  */
__d_find_alias(struct inode * inode)1006*4882a593Smuzhiyun static struct dentry *__d_find_alias(struct inode *inode)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun 	struct dentry *alias;
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode))
1011*4882a593Smuzhiyun 		return __d_find_any_alias(inode);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1014*4882a593Smuzhiyun 		spin_lock(&alias->d_lock);
1015*4882a593Smuzhiyun  		if (!d_unhashed(alias)) {
1016*4882a593Smuzhiyun 			__dget_dlock(alias);
1017*4882a593Smuzhiyun 			spin_unlock(&alias->d_lock);
1018*4882a593Smuzhiyun 			return alias;
1019*4882a593Smuzhiyun 		}
1020*4882a593Smuzhiyun 		spin_unlock(&alias->d_lock);
1021*4882a593Smuzhiyun 	}
1022*4882a593Smuzhiyun 	return NULL;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
d_find_alias(struct inode * inode)1025*4882a593Smuzhiyun struct dentry *d_find_alias(struct inode *inode)
1026*4882a593Smuzhiyun {
1027*4882a593Smuzhiyun 	struct dentry *de = NULL;
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	if (!hlist_empty(&inode->i_dentry)) {
1030*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
1031*4882a593Smuzhiyun 		de = __d_find_alias(inode);
1032*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1033*4882a593Smuzhiyun 	}
1034*4882a593Smuzhiyun 	return de;
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun EXPORT_SYMBOL(d_find_alias);
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun /*
1039*4882a593Smuzhiyun  *	Try to kill dentries associated with this inode.
1040*4882a593Smuzhiyun  * WARNING: you must own a reference to inode.
1041*4882a593Smuzhiyun  */
d_prune_aliases(struct inode * inode)1042*4882a593Smuzhiyun void d_prune_aliases(struct inode *inode)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	struct dentry *dentry;
1045*4882a593Smuzhiyun restart:
1046*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1047*4882a593Smuzhiyun 	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1048*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
1049*4882a593Smuzhiyun 		if (!dentry->d_lockref.count) {
1050*4882a593Smuzhiyun 			struct dentry *parent = lock_parent(dentry);
1051*4882a593Smuzhiyun 			if (likely(!dentry->d_lockref.count)) {
1052*4882a593Smuzhiyun 				__dentry_kill(dentry);
1053*4882a593Smuzhiyun 				dput(parent);
1054*4882a593Smuzhiyun 				goto restart;
1055*4882a593Smuzhiyun 			}
1056*4882a593Smuzhiyun 			if (parent)
1057*4882a593Smuzhiyun 				spin_unlock(&parent->d_lock);
1058*4882a593Smuzhiyun 		}
1059*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1060*4882a593Smuzhiyun 	}
1061*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun EXPORT_SYMBOL(d_prune_aliases);
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun /*
1066*4882a593Smuzhiyun  * Lock a dentry from shrink list.
1067*4882a593Smuzhiyun  * Called under rcu_read_lock() and dentry->d_lock; the former
1068*4882a593Smuzhiyun  * guarantees that nothing we access will be freed under us.
1069*4882a593Smuzhiyun  * Note that dentry is *not* protected from concurrent dentry_kill(),
1070*4882a593Smuzhiyun  * d_delete(), etc.
1071*4882a593Smuzhiyun  *
1072*4882a593Smuzhiyun  * Return false if dentry has been disrupted or grabbed, leaving
1073*4882a593Smuzhiyun  * the caller to kick it off-list.  Otherwise, return true and have
1074*4882a593Smuzhiyun  * that dentry's inode and parent both locked.
1075*4882a593Smuzhiyun  */
shrink_lock_dentry(struct dentry * dentry)1076*4882a593Smuzhiyun static bool shrink_lock_dentry(struct dentry *dentry)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun 	struct inode *inode;
1079*4882a593Smuzhiyun 	struct dentry *parent;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	if (dentry->d_lockref.count)
1082*4882a593Smuzhiyun 		return false;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	inode = dentry->d_inode;
1085*4882a593Smuzhiyun 	if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
1086*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1087*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
1088*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
1089*4882a593Smuzhiyun 		if (unlikely(dentry->d_lockref.count))
1090*4882a593Smuzhiyun 			goto out;
1091*4882a593Smuzhiyun 		/* changed inode means that somebody had grabbed it */
1092*4882a593Smuzhiyun 		if (unlikely(inode != dentry->d_inode))
1093*4882a593Smuzhiyun 			goto out;
1094*4882a593Smuzhiyun 	}
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	parent = dentry->d_parent;
1097*4882a593Smuzhiyun 	if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1098*4882a593Smuzhiyun 		return true;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1101*4882a593Smuzhiyun 	spin_lock(&parent->d_lock);
1102*4882a593Smuzhiyun 	if (unlikely(parent != dentry->d_parent)) {
1103*4882a593Smuzhiyun 		spin_unlock(&parent->d_lock);
1104*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
1105*4882a593Smuzhiyun 		goto out;
1106*4882a593Smuzhiyun 	}
1107*4882a593Smuzhiyun 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1108*4882a593Smuzhiyun 	if (likely(!dentry->d_lockref.count))
1109*4882a593Smuzhiyun 		return true;
1110*4882a593Smuzhiyun 	spin_unlock(&parent->d_lock);
1111*4882a593Smuzhiyun out:
1112*4882a593Smuzhiyun 	if (inode)
1113*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1114*4882a593Smuzhiyun 	return false;
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun 
shrink_dentry_list(struct list_head * list)1117*4882a593Smuzhiyun void shrink_dentry_list(struct list_head *list)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun 	while (!list_empty(list)) {
1120*4882a593Smuzhiyun 		struct dentry *dentry, *parent;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 		dentry = list_entry(list->prev, struct dentry, d_lru);
1123*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
1124*4882a593Smuzhiyun 		rcu_read_lock();
1125*4882a593Smuzhiyun 		if (!shrink_lock_dentry(dentry)) {
1126*4882a593Smuzhiyun 			bool can_free = false;
1127*4882a593Smuzhiyun 			rcu_read_unlock();
1128*4882a593Smuzhiyun 			d_shrink_del(dentry);
1129*4882a593Smuzhiyun 			if (dentry->d_lockref.count < 0)
1130*4882a593Smuzhiyun 				can_free = dentry->d_flags & DCACHE_MAY_FREE;
1131*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
1132*4882a593Smuzhiyun 			if (can_free)
1133*4882a593Smuzhiyun 				dentry_free(dentry);
1134*4882a593Smuzhiyun 			continue;
1135*4882a593Smuzhiyun 		}
1136*4882a593Smuzhiyun 		rcu_read_unlock();
1137*4882a593Smuzhiyun 		d_shrink_del(dentry);
1138*4882a593Smuzhiyun 		parent = dentry->d_parent;
1139*4882a593Smuzhiyun 		if (parent != dentry)
1140*4882a593Smuzhiyun 			__dput_to_list(parent, list);
1141*4882a593Smuzhiyun 		__dentry_kill(dentry);
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun 
dentry_lru_isolate(struct list_head * item,struct list_lru_one * lru,spinlock_t * lru_lock,void * arg)1145*4882a593Smuzhiyun static enum lru_status dentry_lru_isolate(struct list_head *item,
1146*4882a593Smuzhiyun 		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	struct list_head *freeable = arg;
1149*4882a593Smuzhiyun 	struct dentry	*dentry = container_of(item, struct dentry, d_lru);
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	/*
1153*4882a593Smuzhiyun 	 * we are inverting the lru lock/dentry->d_lock here,
1154*4882a593Smuzhiyun 	 * so use a trylock. If we fail to get the lock, just skip
1155*4882a593Smuzhiyun 	 * it
1156*4882a593Smuzhiyun 	 */
1157*4882a593Smuzhiyun 	if (!spin_trylock(&dentry->d_lock))
1158*4882a593Smuzhiyun 		return LRU_SKIP;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	/*
1161*4882a593Smuzhiyun 	 * Referenced dentries are still in use. If they have active
1162*4882a593Smuzhiyun 	 * counts, just remove them from the LRU. Otherwise give them
1163*4882a593Smuzhiyun 	 * another pass through the LRU.
1164*4882a593Smuzhiyun 	 */
1165*4882a593Smuzhiyun 	if (dentry->d_lockref.count) {
1166*4882a593Smuzhiyun 		d_lru_isolate(lru, dentry);
1167*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1168*4882a593Smuzhiyun 		return LRU_REMOVED;
1169*4882a593Smuzhiyun 	}
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_REFERENCED) {
1172*4882a593Smuzhiyun 		dentry->d_flags &= ~DCACHE_REFERENCED;
1173*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 		/*
1176*4882a593Smuzhiyun 		 * The list move itself will be made by the common LRU code. At
1177*4882a593Smuzhiyun 		 * this point, we've dropped the dentry->d_lock but keep the
1178*4882a593Smuzhiyun 		 * lru lock. This is safe to do, since every list movement is
1179*4882a593Smuzhiyun 		 * protected by the lru lock even if both locks are held.
1180*4882a593Smuzhiyun 		 *
1181*4882a593Smuzhiyun 		 * This is guaranteed by the fact that all LRU management
1182*4882a593Smuzhiyun 		 * functions are intermediated by the LRU API calls like
1183*4882a593Smuzhiyun 		 * list_lru_add and list_lru_del. List movement in this file
1184*4882a593Smuzhiyun 		 * only ever occur through this functions or through callbacks
1185*4882a593Smuzhiyun 		 * like this one, that are called from the LRU API.
1186*4882a593Smuzhiyun 		 *
1187*4882a593Smuzhiyun 		 * The only exceptions to this are functions like
1188*4882a593Smuzhiyun 		 * shrink_dentry_list, and code that first checks for the
1189*4882a593Smuzhiyun 		 * DCACHE_SHRINK_LIST flag.  Those are guaranteed to be
1190*4882a593Smuzhiyun 		 * operating only with stack provided lists after they are
1191*4882a593Smuzhiyun 		 * properly isolated from the main list.  It is thus, always a
1192*4882a593Smuzhiyun 		 * local access.
1193*4882a593Smuzhiyun 		 */
1194*4882a593Smuzhiyun 		return LRU_ROTATE;
1195*4882a593Smuzhiyun 	}
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	d_lru_shrink_move(lru, dentry, freeable);
1198*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	return LRU_REMOVED;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun /**
1204*4882a593Smuzhiyun  * prune_dcache_sb - shrink the dcache
1205*4882a593Smuzhiyun  * @sb: superblock
1206*4882a593Smuzhiyun  * @sc: shrink control, passed to list_lru_shrink_walk()
1207*4882a593Smuzhiyun  *
1208*4882a593Smuzhiyun  * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
1209*4882a593Smuzhiyun  * is done when we need more memory and called from the superblock shrinker
1210*4882a593Smuzhiyun  * function.
1211*4882a593Smuzhiyun  *
1212*4882a593Smuzhiyun  * This function may fail to free any resources if all the dentries are in
1213*4882a593Smuzhiyun  * use.
1214*4882a593Smuzhiyun  */
prune_dcache_sb(struct super_block * sb,struct shrink_control * sc)1215*4882a593Smuzhiyun long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
1216*4882a593Smuzhiyun {
1217*4882a593Smuzhiyun 	LIST_HEAD(dispose);
1218*4882a593Smuzhiyun 	long freed;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
1221*4882a593Smuzhiyun 				     dentry_lru_isolate, &dispose);
1222*4882a593Smuzhiyun 	shrink_dentry_list(&dispose);
1223*4882a593Smuzhiyun 	return freed;
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun 
dentry_lru_isolate_shrink(struct list_head * item,struct list_lru_one * lru,spinlock_t * lru_lock,void * arg)1226*4882a593Smuzhiyun static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1227*4882a593Smuzhiyun 		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun 	struct list_head *freeable = arg;
1230*4882a593Smuzhiyun 	struct dentry	*dentry = container_of(item, struct dentry, d_lru);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	/*
1233*4882a593Smuzhiyun 	 * we are inverting the lru lock/dentry->d_lock here,
1234*4882a593Smuzhiyun 	 * so use a trylock. If we fail to get the lock, just skip
1235*4882a593Smuzhiyun 	 * it
1236*4882a593Smuzhiyun 	 */
1237*4882a593Smuzhiyun 	if (!spin_trylock(&dentry->d_lock))
1238*4882a593Smuzhiyun 		return LRU_SKIP;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	d_lru_shrink_move(lru, dentry, freeable);
1241*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	return LRU_REMOVED;
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun /**
1248*4882a593Smuzhiyun  * shrink_dcache_sb - shrink dcache for a superblock
1249*4882a593Smuzhiyun  * @sb: superblock
1250*4882a593Smuzhiyun  *
1251*4882a593Smuzhiyun  * Shrink the dcache for the specified super block. This is used to free
1252*4882a593Smuzhiyun  * the dcache before unmounting a file system.
1253*4882a593Smuzhiyun  */
shrink_dcache_sb(struct super_block * sb)1254*4882a593Smuzhiyun void shrink_dcache_sb(struct super_block *sb)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun 	do {
1257*4882a593Smuzhiyun 		LIST_HEAD(dispose);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 		list_lru_walk(&sb->s_dentry_lru,
1260*4882a593Smuzhiyun 			dentry_lru_isolate_shrink, &dispose, 1024);
1261*4882a593Smuzhiyun 		shrink_dentry_list(&dispose);
1262*4882a593Smuzhiyun 	} while (list_lru_count(&sb->s_dentry_lru) > 0);
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun EXPORT_SYMBOL(shrink_dcache_sb);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun /**
1267*4882a593Smuzhiyun  * enum d_walk_ret - action to talke during tree walk
1268*4882a593Smuzhiyun  * @D_WALK_CONTINUE:	contrinue walk
1269*4882a593Smuzhiyun  * @D_WALK_QUIT:	quit walk
1270*4882a593Smuzhiyun  * @D_WALK_NORETRY:	quit when retry is needed
1271*4882a593Smuzhiyun  * @D_WALK_SKIP:	skip this dentry and its children
1272*4882a593Smuzhiyun  */
1273*4882a593Smuzhiyun enum d_walk_ret {
1274*4882a593Smuzhiyun 	D_WALK_CONTINUE,
1275*4882a593Smuzhiyun 	D_WALK_QUIT,
1276*4882a593Smuzhiyun 	D_WALK_NORETRY,
1277*4882a593Smuzhiyun 	D_WALK_SKIP,
1278*4882a593Smuzhiyun };
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun /**
1281*4882a593Smuzhiyun  * d_walk - walk the dentry tree
1282*4882a593Smuzhiyun  * @parent:	start of walk
1283*4882a593Smuzhiyun  * @data:	data passed to @enter() and @finish()
1284*4882a593Smuzhiyun  * @enter:	callback when first entering the dentry
1285*4882a593Smuzhiyun  *
1286*4882a593Smuzhiyun  * The @enter() callbacks are called with d_lock held.
1287*4882a593Smuzhiyun  */
d_walk(struct dentry * parent,void * data,enum d_walk_ret (* enter)(void *,struct dentry *))1288*4882a593Smuzhiyun static void d_walk(struct dentry *parent, void *data,
1289*4882a593Smuzhiyun 		   enum d_walk_ret (*enter)(void *, struct dentry *))
1290*4882a593Smuzhiyun {
1291*4882a593Smuzhiyun 	struct dentry *this_parent;
1292*4882a593Smuzhiyun 	struct list_head *next;
1293*4882a593Smuzhiyun 	unsigned seq = 0;
1294*4882a593Smuzhiyun 	enum d_walk_ret ret;
1295*4882a593Smuzhiyun 	bool retry = true;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun again:
1298*4882a593Smuzhiyun 	read_seqbegin_or_lock(&rename_lock, &seq);
1299*4882a593Smuzhiyun 	this_parent = parent;
1300*4882a593Smuzhiyun 	spin_lock(&this_parent->d_lock);
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	ret = enter(data, this_parent);
1303*4882a593Smuzhiyun 	switch (ret) {
1304*4882a593Smuzhiyun 	case D_WALK_CONTINUE:
1305*4882a593Smuzhiyun 		break;
1306*4882a593Smuzhiyun 	case D_WALK_QUIT:
1307*4882a593Smuzhiyun 	case D_WALK_SKIP:
1308*4882a593Smuzhiyun 		goto out_unlock;
1309*4882a593Smuzhiyun 	case D_WALK_NORETRY:
1310*4882a593Smuzhiyun 		retry = false;
1311*4882a593Smuzhiyun 		break;
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun repeat:
1314*4882a593Smuzhiyun 	next = this_parent->d_subdirs.next;
1315*4882a593Smuzhiyun resume:
1316*4882a593Smuzhiyun 	while (next != &this_parent->d_subdirs) {
1317*4882a593Smuzhiyun 		struct list_head *tmp = next;
1318*4882a593Smuzhiyun 		struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1319*4882a593Smuzhiyun 		next = tmp->next;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 		if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1322*4882a593Smuzhiyun 			continue;
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 		ret = enter(data, dentry);
1327*4882a593Smuzhiyun 		switch (ret) {
1328*4882a593Smuzhiyun 		case D_WALK_CONTINUE:
1329*4882a593Smuzhiyun 			break;
1330*4882a593Smuzhiyun 		case D_WALK_QUIT:
1331*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
1332*4882a593Smuzhiyun 			goto out_unlock;
1333*4882a593Smuzhiyun 		case D_WALK_NORETRY:
1334*4882a593Smuzhiyun 			retry = false;
1335*4882a593Smuzhiyun 			break;
1336*4882a593Smuzhiyun 		case D_WALK_SKIP:
1337*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
1338*4882a593Smuzhiyun 			continue;
1339*4882a593Smuzhiyun 		}
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 		if (!list_empty(&dentry->d_subdirs)) {
1342*4882a593Smuzhiyun 			spin_unlock(&this_parent->d_lock);
1343*4882a593Smuzhiyun 			spin_release(&dentry->d_lock.dep_map, _RET_IP_);
1344*4882a593Smuzhiyun 			this_parent = dentry;
1345*4882a593Smuzhiyun 			spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1346*4882a593Smuzhiyun 			goto repeat;
1347*4882a593Smuzhiyun 		}
1348*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 	/*
1351*4882a593Smuzhiyun 	 * All done at this level ... ascend and resume the search.
1352*4882a593Smuzhiyun 	 */
1353*4882a593Smuzhiyun 	rcu_read_lock();
1354*4882a593Smuzhiyun ascend:
1355*4882a593Smuzhiyun 	if (this_parent != parent) {
1356*4882a593Smuzhiyun 		struct dentry *child = this_parent;
1357*4882a593Smuzhiyun 		this_parent = child->d_parent;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 		spin_unlock(&child->d_lock);
1360*4882a593Smuzhiyun 		spin_lock(&this_parent->d_lock);
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 		/* might go back up the wrong parent if we have had a rename. */
1363*4882a593Smuzhiyun 		if (need_seqretry(&rename_lock, seq))
1364*4882a593Smuzhiyun 			goto rename_retry;
1365*4882a593Smuzhiyun 		/* go into the first sibling still alive */
1366*4882a593Smuzhiyun 		do {
1367*4882a593Smuzhiyun 			next = child->d_child.next;
1368*4882a593Smuzhiyun 			if (next == &this_parent->d_subdirs)
1369*4882a593Smuzhiyun 				goto ascend;
1370*4882a593Smuzhiyun 			child = list_entry(next, struct dentry, d_child);
1371*4882a593Smuzhiyun 		} while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
1372*4882a593Smuzhiyun 		rcu_read_unlock();
1373*4882a593Smuzhiyun 		goto resume;
1374*4882a593Smuzhiyun 	}
1375*4882a593Smuzhiyun 	if (need_seqretry(&rename_lock, seq))
1376*4882a593Smuzhiyun 		goto rename_retry;
1377*4882a593Smuzhiyun 	rcu_read_unlock();
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun out_unlock:
1380*4882a593Smuzhiyun 	spin_unlock(&this_parent->d_lock);
1381*4882a593Smuzhiyun 	done_seqretry(&rename_lock, seq);
1382*4882a593Smuzhiyun 	return;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun rename_retry:
1385*4882a593Smuzhiyun 	spin_unlock(&this_parent->d_lock);
1386*4882a593Smuzhiyun 	rcu_read_unlock();
1387*4882a593Smuzhiyun 	BUG_ON(seq & 1);
1388*4882a593Smuzhiyun 	if (!retry)
1389*4882a593Smuzhiyun 		return;
1390*4882a593Smuzhiyun 	seq = 1;
1391*4882a593Smuzhiyun 	goto again;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun struct check_mount {
1395*4882a593Smuzhiyun 	struct vfsmount *mnt;
1396*4882a593Smuzhiyun 	unsigned int mounted;
1397*4882a593Smuzhiyun };
1398*4882a593Smuzhiyun 
path_check_mount(void * data,struct dentry * dentry)1399*4882a593Smuzhiyun static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1400*4882a593Smuzhiyun {
1401*4882a593Smuzhiyun 	struct check_mount *info = data;
1402*4882a593Smuzhiyun 	struct path path = { .mnt = info->mnt, .dentry = dentry };
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	if (likely(!d_mountpoint(dentry)))
1405*4882a593Smuzhiyun 		return D_WALK_CONTINUE;
1406*4882a593Smuzhiyun 	if (__path_is_mountpoint(&path)) {
1407*4882a593Smuzhiyun 		info->mounted = 1;
1408*4882a593Smuzhiyun 		return D_WALK_QUIT;
1409*4882a593Smuzhiyun 	}
1410*4882a593Smuzhiyun 	return D_WALK_CONTINUE;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun /**
1414*4882a593Smuzhiyun  * path_has_submounts - check for mounts over a dentry in the
1415*4882a593Smuzhiyun  *                      current namespace.
1416*4882a593Smuzhiyun  * @parent: path to check.
1417*4882a593Smuzhiyun  *
1418*4882a593Smuzhiyun  * Return true if the parent or its subdirectories contain
1419*4882a593Smuzhiyun  * a mount point in the current namespace.
1420*4882a593Smuzhiyun  */
path_has_submounts(const struct path * parent)1421*4882a593Smuzhiyun int path_has_submounts(const struct path *parent)
1422*4882a593Smuzhiyun {
1423*4882a593Smuzhiyun 	struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	read_seqlock_excl(&mount_lock);
1426*4882a593Smuzhiyun 	d_walk(parent->dentry, &data, path_check_mount);
1427*4882a593Smuzhiyun 	read_sequnlock_excl(&mount_lock);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	return data.mounted;
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun EXPORT_SYMBOL(path_has_submounts);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun /*
1434*4882a593Smuzhiyun  * Called by mount code to set a mountpoint and check if the mountpoint is
1435*4882a593Smuzhiyun  * reachable (e.g. NFS can unhash a directory dentry and then the complete
1436*4882a593Smuzhiyun  * subtree can become unreachable).
1437*4882a593Smuzhiyun  *
1438*4882a593Smuzhiyun  * Only one of d_invalidate() and d_set_mounted() must succeed.  For
1439*4882a593Smuzhiyun  * this reason take rename_lock and d_lock on dentry and ancestors.
1440*4882a593Smuzhiyun  */
d_set_mounted(struct dentry * dentry)1441*4882a593Smuzhiyun int d_set_mounted(struct dentry *dentry)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun 	struct dentry *p;
1444*4882a593Smuzhiyun 	int ret = -ENOENT;
1445*4882a593Smuzhiyun 	write_seqlock(&rename_lock);
1446*4882a593Smuzhiyun 	for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1447*4882a593Smuzhiyun 		/* Need exclusion wrt. d_invalidate() */
1448*4882a593Smuzhiyun 		spin_lock(&p->d_lock);
1449*4882a593Smuzhiyun 		if (unlikely(d_unhashed(p))) {
1450*4882a593Smuzhiyun 			spin_unlock(&p->d_lock);
1451*4882a593Smuzhiyun 			goto out;
1452*4882a593Smuzhiyun 		}
1453*4882a593Smuzhiyun 		spin_unlock(&p->d_lock);
1454*4882a593Smuzhiyun 	}
1455*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
1456*4882a593Smuzhiyun 	if (!d_unlinked(dentry)) {
1457*4882a593Smuzhiyun 		ret = -EBUSY;
1458*4882a593Smuzhiyun 		if (!d_mountpoint(dentry)) {
1459*4882a593Smuzhiyun 			dentry->d_flags |= DCACHE_MOUNTED;
1460*4882a593Smuzhiyun 			ret = 0;
1461*4882a593Smuzhiyun 		}
1462*4882a593Smuzhiyun 	}
1463*4882a593Smuzhiyun  	spin_unlock(&dentry->d_lock);
1464*4882a593Smuzhiyun out:
1465*4882a593Smuzhiyun 	write_sequnlock(&rename_lock);
1466*4882a593Smuzhiyun 	return ret;
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun /*
1470*4882a593Smuzhiyun  * Search the dentry child list of the specified parent,
1471*4882a593Smuzhiyun  * and move any unused dentries to the end of the unused
1472*4882a593Smuzhiyun  * list for prune_dcache(). We descend to the next level
1473*4882a593Smuzhiyun  * whenever the d_subdirs list is non-empty and continue
1474*4882a593Smuzhiyun  * searching.
1475*4882a593Smuzhiyun  *
1476*4882a593Smuzhiyun  * It returns zero iff there are no unused children,
1477*4882a593Smuzhiyun  * otherwise  it returns the number of children moved to
1478*4882a593Smuzhiyun  * the end of the unused list. This may not be the total
1479*4882a593Smuzhiyun  * number of unused children, because select_parent can
1480*4882a593Smuzhiyun  * drop the lock and return early due to latency
1481*4882a593Smuzhiyun  * constraints.
1482*4882a593Smuzhiyun  */
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun struct select_data {
1485*4882a593Smuzhiyun 	struct dentry *start;
1486*4882a593Smuzhiyun 	union {
1487*4882a593Smuzhiyun 		long found;
1488*4882a593Smuzhiyun 		struct dentry *victim;
1489*4882a593Smuzhiyun 	};
1490*4882a593Smuzhiyun 	struct list_head dispose;
1491*4882a593Smuzhiyun };
1492*4882a593Smuzhiyun 
select_collect(void * _data,struct dentry * dentry)1493*4882a593Smuzhiyun static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun 	struct select_data *data = _data;
1496*4882a593Smuzhiyun 	enum d_walk_ret ret = D_WALK_CONTINUE;
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	if (data->start == dentry)
1499*4882a593Smuzhiyun 		goto out;
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1502*4882a593Smuzhiyun 		data->found++;
1503*4882a593Smuzhiyun 	} else {
1504*4882a593Smuzhiyun 		if (dentry->d_flags & DCACHE_LRU_LIST)
1505*4882a593Smuzhiyun 			d_lru_del(dentry);
1506*4882a593Smuzhiyun 		if (!dentry->d_lockref.count) {
1507*4882a593Smuzhiyun 			d_shrink_add(dentry, &data->dispose);
1508*4882a593Smuzhiyun 			data->found++;
1509*4882a593Smuzhiyun 		}
1510*4882a593Smuzhiyun 	}
1511*4882a593Smuzhiyun 	/*
1512*4882a593Smuzhiyun 	 * We can return to the caller if we have found some (this
1513*4882a593Smuzhiyun 	 * ensures forward progress). We'll be coming back to find
1514*4882a593Smuzhiyun 	 * the rest.
1515*4882a593Smuzhiyun 	 */
1516*4882a593Smuzhiyun 	if (!list_empty(&data->dispose))
1517*4882a593Smuzhiyun 		ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1518*4882a593Smuzhiyun out:
1519*4882a593Smuzhiyun 	return ret;
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun 
select_collect2(void * _data,struct dentry * dentry)1522*4882a593Smuzhiyun static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun 	struct select_data *data = _data;
1525*4882a593Smuzhiyun 	enum d_walk_ret ret = D_WALK_CONTINUE;
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	if (data->start == dentry)
1528*4882a593Smuzhiyun 		goto out;
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1531*4882a593Smuzhiyun 		if (!dentry->d_lockref.count) {
1532*4882a593Smuzhiyun 			rcu_read_lock();
1533*4882a593Smuzhiyun 			data->victim = dentry;
1534*4882a593Smuzhiyun 			return D_WALK_QUIT;
1535*4882a593Smuzhiyun 		}
1536*4882a593Smuzhiyun 	} else {
1537*4882a593Smuzhiyun 		if (dentry->d_flags & DCACHE_LRU_LIST)
1538*4882a593Smuzhiyun 			d_lru_del(dentry);
1539*4882a593Smuzhiyun 		if (!dentry->d_lockref.count)
1540*4882a593Smuzhiyun 			d_shrink_add(dentry, &data->dispose);
1541*4882a593Smuzhiyun 	}
1542*4882a593Smuzhiyun 	/*
1543*4882a593Smuzhiyun 	 * We can return to the caller if we have found some (this
1544*4882a593Smuzhiyun 	 * ensures forward progress). We'll be coming back to find
1545*4882a593Smuzhiyun 	 * the rest.
1546*4882a593Smuzhiyun 	 */
1547*4882a593Smuzhiyun 	if (!list_empty(&data->dispose))
1548*4882a593Smuzhiyun 		ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1549*4882a593Smuzhiyun out:
1550*4882a593Smuzhiyun 	return ret;
1551*4882a593Smuzhiyun }
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun /**
1554*4882a593Smuzhiyun  * shrink_dcache_parent - prune dcache
1555*4882a593Smuzhiyun  * @parent: parent of entries to prune
1556*4882a593Smuzhiyun  *
1557*4882a593Smuzhiyun  * Prune the dcache to remove unused children of the parent dentry.
1558*4882a593Smuzhiyun  */
shrink_dcache_parent(struct dentry * parent)1559*4882a593Smuzhiyun void shrink_dcache_parent(struct dentry *parent)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun 	for (;;) {
1562*4882a593Smuzhiyun 		struct select_data data = {.start = parent};
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 		INIT_LIST_HEAD(&data.dispose);
1565*4882a593Smuzhiyun 		d_walk(parent, &data, select_collect);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 		if (!list_empty(&data.dispose)) {
1568*4882a593Smuzhiyun 			shrink_dentry_list(&data.dispose);
1569*4882a593Smuzhiyun 			continue;
1570*4882a593Smuzhiyun 		}
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 		cond_resched();
1573*4882a593Smuzhiyun 		if (!data.found)
1574*4882a593Smuzhiyun 			break;
1575*4882a593Smuzhiyun 		data.victim = NULL;
1576*4882a593Smuzhiyun 		d_walk(parent, &data, select_collect2);
1577*4882a593Smuzhiyun 		if (data.victim) {
1578*4882a593Smuzhiyun 			struct dentry *parent;
1579*4882a593Smuzhiyun 			spin_lock(&data.victim->d_lock);
1580*4882a593Smuzhiyun 			if (!shrink_lock_dentry(data.victim)) {
1581*4882a593Smuzhiyun 				spin_unlock(&data.victim->d_lock);
1582*4882a593Smuzhiyun 				rcu_read_unlock();
1583*4882a593Smuzhiyun 			} else {
1584*4882a593Smuzhiyun 				rcu_read_unlock();
1585*4882a593Smuzhiyun 				parent = data.victim->d_parent;
1586*4882a593Smuzhiyun 				if (parent != data.victim)
1587*4882a593Smuzhiyun 					__dput_to_list(parent, &data.dispose);
1588*4882a593Smuzhiyun 				__dentry_kill(data.victim);
1589*4882a593Smuzhiyun 			}
1590*4882a593Smuzhiyun 		}
1591*4882a593Smuzhiyun 		if (!list_empty(&data.dispose))
1592*4882a593Smuzhiyun 			shrink_dentry_list(&data.dispose);
1593*4882a593Smuzhiyun 	}
1594*4882a593Smuzhiyun }
1595*4882a593Smuzhiyun EXPORT_SYMBOL(shrink_dcache_parent);
1596*4882a593Smuzhiyun 
umount_check(void * _data,struct dentry * dentry)1597*4882a593Smuzhiyun static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1598*4882a593Smuzhiyun {
1599*4882a593Smuzhiyun 	/* it has busy descendents; complain about those instead */
1600*4882a593Smuzhiyun 	if (!list_empty(&dentry->d_subdirs))
1601*4882a593Smuzhiyun 		return D_WALK_CONTINUE;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	/* root with refcount 1 is fine */
1604*4882a593Smuzhiyun 	if (dentry == _data && dentry->d_lockref.count == 1)
1605*4882a593Smuzhiyun 		return D_WALK_CONTINUE;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1608*4882a593Smuzhiyun 			" still in use (%d) [unmount of %s %s]\n",
1609*4882a593Smuzhiyun 		       dentry,
1610*4882a593Smuzhiyun 		       dentry->d_inode ?
1611*4882a593Smuzhiyun 		       dentry->d_inode->i_ino : 0UL,
1612*4882a593Smuzhiyun 		       dentry,
1613*4882a593Smuzhiyun 		       dentry->d_lockref.count,
1614*4882a593Smuzhiyun 		       dentry->d_sb->s_type->name,
1615*4882a593Smuzhiyun 		       dentry->d_sb->s_id);
1616*4882a593Smuzhiyun 	WARN_ON(1);
1617*4882a593Smuzhiyun 	return D_WALK_CONTINUE;
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun 
do_one_tree(struct dentry * dentry)1620*4882a593Smuzhiyun static void do_one_tree(struct dentry *dentry)
1621*4882a593Smuzhiyun {
1622*4882a593Smuzhiyun 	shrink_dcache_parent(dentry);
1623*4882a593Smuzhiyun 	d_walk(dentry, dentry, umount_check);
1624*4882a593Smuzhiyun 	d_drop(dentry);
1625*4882a593Smuzhiyun 	dput(dentry);
1626*4882a593Smuzhiyun }
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun /*
1629*4882a593Smuzhiyun  * destroy the dentries attached to a superblock on unmounting
1630*4882a593Smuzhiyun  */
shrink_dcache_for_umount(struct super_block * sb)1631*4882a593Smuzhiyun void shrink_dcache_for_umount(struct super_block *sb)
1632*4882a593Smuzhiyun {
1633*4882a593Smuzhiyun 	struct dentry *dentry;
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 	dentry = sb->s_root;
1638*4882a593Smuzhiyun 	sb->s_root = NULL;
1639*4882a593Smuzhiyun 	do_one_tree(dentry);
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	while (!hlist_bl_empty(&sb->s_roots)) {
1642*4882a593Smuzhiyun 		dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
1643*4882a593Smuzhiyun 		do_one_tree(dentry);
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun 
find_submount(void * _data,struct dentry * dentry)1647*4882a593Smuzhiyun static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	struct dentry **victim = _data;
1650*4882a593Smuzhiyun 	if (d_mountpoint(dentry)) {
1651*4882a593Smuzhiyun 		__dget_dlock(dentry);
1652*4882a593Smuzhiyun 		*victim = dentry;
1653*4882a593Smuzhiyun 		return D_WALK_QUIT;
1654*4882a593Smuzhiyun 	}
1655*4882a593Smuzhiyun 	return D_WALK_CONTINUE;
1656*4882a593Smuzhiyun }
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun /**
1659*4882a593Smuzhiyun  * d_invalidate - detach submounts, prune dcache, and drop
1660*4882a593Smuzhiyun  * @dentry: dentry to invalidate (aka detach, prune and drop)
1661*4882a593Smuzhiyun  */
d_invalidate(struct dentry * dentry)1662*4882a593Smuzhiyun void d_invalidate(struct dentry *dentry)
1663*4882a593Smuzhiyun {
1664*4882a593Smuzhiyun 	bool had_submounts = false;
1665*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
1666*4882a593Smuzhiyun 	if (d_unhashed(dentry)) {
1667*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
1668*4882a593Smuzhiyun 		return;
1669*4882a593Smuzhiyun 	}
1670*4882a593Smuzhiyun 	__d_drop(dentry);
1671*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	/* Negative dentries can be dropped without further checks */
1674*4882a593Smuzhiyun 	if (!dentry->d_inode)
1675*4882a593Smuzhiyun 		return;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	shrink_dcache_parent(dentry);
1678*4882a593Smuzhiyun 	for (;;) {
1679*4882a593Smuzhiyun 		struct dentry *victim = NULL;
1680*4882a593Smuzhiyun 		d_walk(dentry, &victim, find_submount);
1681*4882a593Smuzhiyun 		if (!victim) {
1682*4882a593Smuzhiyun 			if (had_submounts)
1683*4882a593Smuzhiyun 				shrink_dcache_parent(dentry);
1684*4882a593Smuzhiyun 			return;
1685*4882a593Smuzhiyun 		}
1686*4882a593Smuzhiyun 		had_submounts = true;
1687*4882a593Smuzhiyun 		detach_mounts(victim);
1688*4882a593Smuzhiyun 		dput(victim);
1689*4882a593Smuzhiyun 	}
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun EXPORT_SYMBOL(d_invalidate);
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun /**
1694*4882a593Smuzhiyun  * __d_alloc	-	allocate a dcache entry
1695*4882a593Smuzhiyun  * @sb: filesystem it will belong to
1696*4882a593Smuzhiyun  * @name: qstr of the name
1697*4882a593Smuzhiyun  *
1698*4882a593Smuzhiyun  * Allocates a dentry. It returns %NULL if there is insufficient memory
1699*4882a593Smuzhiyun  * available. On a success the dentry is returned. The name passed in is
1700*4882a593Smuzhiyun  * copied and the copy passed in may be reused after this call.
1701*4882a593Smuzhiyun  */
1702*4882a593Smuzhiyun 
__d_alloc(struct super_block * sb,const struct qstr * name)1703*4882a593Smuzhiyun static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1704*4882a593Smuzhiyun {
1705*4882a593Smuzhiyun 	struct dentry *dentry;
1706*4882a593Smuzhiyun 	char *dname;
1707*4882a593Smuzhiyun 	int err;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1710*4882a593Smuzhiyun 	if (!dentry)
1711*4882a593Smuzhiyun 		return NULL;
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 	/*
1714*4882a593Smuzhiyun 	 * We guarantee that the inline name is always NUL-terminated.
1715*4882a593Smuzhiyun 	 * This way the memcpy() done by the name switching in rename
1716*4882a593Smuzhiyun 	 * will still always have a NUL at the end, even if we might
1717*4882a593Smuzhiyun 	 * be overwriting an internal NUL character
1718*4882a593Smuzhiyun 	 */
1719*4882a593Smuzhiyun 	dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1720*4882a593Smuzhiyun 	if (unlikely(!name)) {
1721*4882a593Smuzhiyun 		name = &slash_name;
1722*4882a593Smuzhiyun 		dname = dentry->d_iname;
1723*4882a593Smuzhiyun 	} else if (name->len > DNAME_INLINE_LEN-1) {
1724*4882a593Smuzhiyun 		size_t size = offsetof(struct external_name, name[1]);
1725*4882a593Smuzhiyun 		struct external_name *p = kmalloc(size + name->len,
1726*4882a593Smuzhiyun 						  GFP_KERNEL_ACCOUNT |
1727*4882a593Smuzhiyun 						  __GFP_RECLAIMABLE);
1728*4882a593Smuzhiyun 		if (!p) {
1729*4882a593Smuzhiyun 			kmem_cache_free(dentry_cache, dentry);
1730*4882a593Smuzhiyun 			return NULL;
1731*4882a593Smuzhiyun 		}
1732*4882a593Smuzhiyun 		atomic_set(&p->u.count, 1);
1733*4882a593Smuzhiyun 		dname = p->name;
1734*4882a593Smuzhiyun 	} else  {
1735*4882a593Smuzhiyun 		dname = dentry->d_iname;
1736*4882a593Smuzhiyun 	}
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	dentry->d_name.len = name->len;
1739*4882a593Smuzhiyun 	dentry->d_name.hash = name->hash;
1740*4882a593Smuzhiyun 	memcpy(dname, name->name, name->len);
1741*4882a593Smuzhiyun 	dname[name->len] = 0;
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	/* Make sure we always see the terminating NUL character */
1744*4882a593Smuzhiyun 	smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	dentry->d_lockref.count = 1;
1747*4882a593Smuzhiyun 	dentry->d_flags = 0;
1748*4882a593Smuzhiyun 	spin_lock_init(&dentry->d_lock);
1749*4882a593Smuzhiyun 	seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
1750*4882a593Smuzhiyun 	dentry->d_inode = NULL;
1751*4882a593Smuzhiyun 	dentry->d_parent = dentry;
1752*4882a593Smuzhiyun 	dentry->d_sb = sb;
1753*4882a593Smuzhiyun 	dentry->d_op = NULL;
1754*4882a593Smuzhiyun 	dentry->d_fsdata = NULL;
1755*4882a593Smuzhiyun 	INIT_HLIST_BL_NODE(&dentry->d_hash);
1756*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dentry->d_lru);
1757*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dentry->d_subdirs);
1758*4882a593Smuzhiyun 	INIT_HLIST_NODE(&dentry->d_u.d_alias);
1759*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dentry->d_child);
1760*4882a593Smuzhiyun 	d_set_d_op(dentry, dentry->d_sb->s_d_op);
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	if (dentry->d_op && dentry->d_op->d_init) {
1763*4882a593Smuzhiyun 		err = dentry->d_op->d_init(dentry);
1764*4882a593Smuzhiyun 		if (err) {
1765*4882a593Smuzhiyun 			if (dname_external(dentry))
1766*4882a593Smuzhiyun 				kfree(external_name(dentry));
1767*4882a593Smuzhiyun 			kmem_cache_free(dentry_cache, dentry);
1768*4882a593Smuzhiyun 			return NULL;
1769*4882a593Smuzhiyun 		}
1770*4882a593Smuzhiyun 	}
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	this_cpu_inc(nr_dentry);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	return dentry;
1775*4882a593Smuzhiyun }
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun /**
1778*4882a593Smuzhiyun  * d_alloc	-	allocate a dcache entry
1779*4882a593Smuzhiyun  * @parent: parent of entry to allocate
1780*4882a593Smuzhiyun  * @name: qstr of the name
1781*4882a593Smuzhiyun  *
1782*4882a593Smuzhiyun  * Allocates a dentry. It returns %NULL if there is insufficient memory
1783*4882a593Smuzhiyun  * available. On a success the dentry is returned. The name passed in is
1784*4882a593Smuzhiyun  * copied and the copy passed in may be reused after this call.
1785*4882a593Smuzhiyun  */
d_alloc(struct dentry * parent,const struct qstr * name)1786*4882a593Smuzhiyun struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1787*4882a593Smuzhiyun {
1788*4882a593Smuzhiyun 	struct dentry *dentry = __d_alloc(parent->d_sb, name);
1789*4882a593Smuzhiyun 	if (!dentry)
1790*4882a593Smuzhiyun 		return NULL;
1791*4882a593Smuzhiyun 	spin_lock(&parent->d_lock);
1792*4882a593Smuzhiyun 	/*
1793*4882a593Smuzhiyun 	 * don't need child lock because it is not subject
1794*4882a593Smuzhiyun 	 * to concurrency here
1795*4882a593Smuzhiyun 	 */
1796*4882a593Smuzhiyun 	__dget_dlock(parent);
1797*4882a593Smuzhiyun 	dentry->d_parent = parent;
1798*4882a593Smuzhiyun 	list_add(&dentry->d_child, &parent->d_subdirs);
1799*4882a593Smuzhiyun 	spin_unlock(&parent->d_lock);
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	return dentry;
1802*4882a593Smuzhiyun }
1803*4882a593Smuzhiyun EXPORT_SYMBOL(d_alloc);
1804*4882a593Smuzhiyun 
d_alloc_anon(struct super_block * sb)1805*4882a593Smuzhiyun struct dentry *d_alloc_anon(struct super_block *sb)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun 	return __d_alloc(sb, NULL);
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun EXPORT_SYMBOL(d_alloc_anon);
1810*4882a593Smuzhiyun 
d_alloc_cursor(struct dentry * parent)1811*4882a593Smuzhiyun struct dentry *d_alloc_cursor(struct dentry * parent)
1812*4882a593Smuzhiyun {
1813*4882a593Smuzhiyun 	struct dentry *dentry = d_alloc_anon(parent->d_sb);
1814*4882a593Smuzhiyun 	if (dentry) {
1815*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_DENTRY_CURSOR;
1816*4882a593Smuzhiyun 		dentry->d_parent = dget(parent);
1817*4882a593Smuzhiyun 	}
1818*4882a593Smuzhiyun 	return dentry;
1819*4882a593Smuzhiyun }
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun /**
1822*4882a593Smuzhiyun  * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1823*4882a593Smuzhiyun  * @sb: the superblock
1824*4882a593Smuzhiyun  * @name: qstr of the name
1825*4882a593Smuzhiyun  *
1826*4882a593Smuzhiyun  * For a filesystem that just pins its dentries in memory and never
1827*4882a593Smuzhiyun  * performs lookups at all, return an unhashed IS_ROOT dentry.
1828*4882a593Smuzhiyun  * This is used for pipes, sockets et.al. - the stuff that should
1829*4882a593Smuzhiyun  * never be anyone's children or parents.  Unlike all other
1830*4882a593Smuzhiyun  * dentries, these will not have RCU delay between dropping the
1831*4882a593Smuzhiyun  * last reference and freeing them.
1832*4882a593Smuzhiyun  *
1833*4882a593Smuzhiyun  * The only user is alloc_file_pseudo() and that's what should
1834*4882a593Smuzhiyun  * be considered a public interface.  Don't use directly.
1835*4882a593Smuzhiyun  */
d_alloc_pseudo(struct super_block * sb,const struct qstr * name)1836*4882a593Smuzhiyun struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1837*4882a593Smuzhiyun {
1838*4882a593Smuzhiyun 	struct dentry *dentry = __d_alloc(sb, name);
1839*4882a593Smuzhiyun 	if (likely(dentry))
1840*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_NORCU;
1841*4882a593Smuzhiyun 	return dentry;
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun 
d_alloc_name(struct dentry * parent,const char * name)1844*4882a593Smuzhiyun struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1845*4882a593Smuzhiyun {
1846*4882a593Smuzhiyun 	struct qstr q;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	q.name = name;
1849*4882a593Smuzhiyun 	q.hash_len = hashlen_string(parent, name);
1850*4882a593Smuzhiyun 	return d_alloc(parent, &q);
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun EXPORT_SYMBOL(d_alloc_name);
1853*4882a593Smuzhiyun 
d_set_d_op(struct dentry * dentry,const struct dentry_operations * op)1854*4882a593Smuzhiyun void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1855*4882a593Smuzhiyun {
1856*4882a593Smuzhiyun 	WARN_ON_ONCE(dentry->d_op);
1857*4882a593Smuzhiyun 	WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH	|
1858*4882a593Smuzhiyun 				DCACHE_OP_COMPARE	|
1859*4882a593Smuzhiyun 				DCACHE_OP_REVALIDATE	|
1860*4882a593Smuzhiyun 				DCACHE_OP_WEAK_REVALIDATE	|
1861*4882a593Smuzhiyun 				DCACHE_OP_DELETE	|
1862*4882a593Smuzhiyun 				DCACHE_OP_REAL));
1863*4882a593Smuzhiyun 	dentry->d_op = op;
1864*4882a593Smuzhiyun 	if (!op)
1865*4882a593Smuzhiyun 		return;
1866*4882a593Smuzhiyun 	if (op->d_hash)
1867*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_HASH;
1868*4882a593Smuzhiyun 	if (op->d_compare)
1869*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_COMPARE;
1870*4882a593Smuzhiyun 	if (op->d_revalidate)
1871*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_REVALIDATE;
1872*4882a593Smuzhiyun 	if (op->d_weak_revalidate)
1873*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1874*4882a593Smuzhiyun 	if (op->d_delete)
1875*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_DELETE;
1876*4882a593Smuzhiyun 	if (op->d_prune)
1877*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_PRUNE;
1878*4882a593Smuzhiyun 	if (op->d_real)
1879*4882a593Smuzhiyun 		dentry->d_flags |= DCACHE_OP_REAL;
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun }
1882*4882a593Smuzhiyun EXPORT_SYMBOL(d_set_d_op);
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun /*
1886*4882a593Smuzhiyun  * d_set_fallthru - Mark a dentry as falling through to a lower layer
1887*4882a593Smuzhiyun  * @dentry - The dentry to mark
1888*4882a593Smuzhiyun  *
1889*4882a593Smuzhiyun  * Mark a dentry as falling through to the lower layer (as set with
1890*4882a593Smuzhiyun  * d_pin_lower()).  This flag may be recorded on the medium.
1891*4882a593Smuzhiyun  */
d_set_fallthru(struct dentry * dentry)1892*4882a593Smuzhiyun void d_set_fallthru(struct dentry *dentry)
1893*4882a593Smuzhiyun {
1894*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
1895*4882a593Smuzhiyun 	dentry->d_flags |= DCACHE_FALLTHRU;
1896*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun EXPORT_SYMBOL(d_set_fallthru);
1899*4882a593Smuzhiyun 
d_flags_for_inode(struct inode * inode)1900*4882a593Smuzhiyun static unsigned d_flags_for_inode(struct inode *inode)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun 	unsigned add_flags = DCACHE_REGULAR_TYPE;
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun 	if (!inode)
1905*4882a593Smuzhiyun 		return DCACHE_MISS_TYPE;
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode)) {
1908*4882a593Smuzhiyun 		add_flags = DCACHE_DIRECTORY_TYPE;
1909*4882a593Smuzhiyun 		if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1910*4882a593Smuzhiyun 			if (unlikely(!inode->i_op->lookup))
1911*4882a593Smuzhiyun 				add_flags = DCACHE_AUTODIR_TYPE;
1912*4882a593Smuzhiyun 			else
1913*4882a593Smuzhiyun 				inode->i_opflags |= IOP_LOOKUP;
1914*4882a593Smuzhiyun 		}
1915*4882a593Smuzhiyun 		goto type_determined;
1916*4882a593Smuzhiyun 	}
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1919*4882a593Smuzhiyun 		if (unlikely(inode->i_op->get_link)) {
1920*4882a593Smuzhiyun 			add_flags = DCACHE_SYMLINK_TYPE;
1921*4882a593Smuzhiyun 			goto type_determined;
1922*4882a593Smuzhiyun 		}
1923*4882a593Smuzhiyun 		inode->i_opflags |= IOP_NOFOLLOW;
1924*4882a593Smuzhiyun 	}
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	if (unlikely(!S_ISREG(inode->i_mode)))
1927*4882a593Smuzhiyun 		add_flags = DCACHE_SPECIAL_TYPE;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun type_determined:
1930*4882a593Smuzhiyun 	if (unlikely(IS_AUTOMOUNT(inode)))
1931*4882a593Smuzhiyun 		add_flags |= DCACHE_NEED_AUTOMOUNT;
1932*4882a593Smuzhiyun 	return add_flags;
1933*4882a593Smuzhiyun }
1934*4882a593Smuzhiyun 
__d_instantiate(struct dentry * dentry,struct inode * inode)1935*4882a593Smuzhiyun static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1936*4882a593Smuzhiyun {
1937*4882a593Smuzhiyun 	unsigned add_flags = d_flags_for_inode(inode);
1938*4882a593Smuzhiyun 	WARN_ON(d_in_lookup(dentry));
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
1941*4882a593Smuzhiyun 	/*
1942*4882a593Smuzhiyun 	 * Decrement negative dentry count if it was in the LRU list.
1943*4882a593Smuzhiyun 	 */
1944*4882a593Smuzhiyun 	if (dentry->d_flags & DCACHE_LRU_LIST)
1945*4882a593Smuzhiyun 		this_cpu_dec(nr_dentry_negative);
1946*4882a593Smuzhiyun 	hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1947*4882a593Smuzhiyun 	raw_write_seqcount_begin(&dentry->d_seq);
1948*4882a593Smuzhiyun 	__d_set_inode_and_type(dentry, inode, add_flags);
1949*4882a593Smuzhiyun 	raw_write_seqcount_end(&dentry->d_seq);
1950*4882a593Smuzhiyun 	fsnotify_update_flags(dentry);
1951*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
1952*4882a593Smuzhiyun }
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun /**
1955*4882a593Smuzhiyun  * d_instantiate - fill in inode information for a dentry
1956*4882a593Smuzhiyun  * @entry: dentry to complete
1957*4882a593Smuzhiyun  * @inode: inode to attach to this dentry
1958*4882a593Smuzhiyun  *
1959*4882a593Smuzhiyun  * Fill in inode information in the entry.
1960*4882a593Smuzhiyun  *
1961*4882a593Smuzhiyun  * This turns negative dentries into productive full members
1962*4882a593Smuzhiyun  * of society.
1963*4882a593Smuzhiyun  *
1964*4882a593Smuzhiyun  * NOTE! This assumes that the inode count has been incremented
1965*4882a593Smuzhiyun  * (or otherwise set) by the caller to indicate that it is now
1966*4882a593Smuzhiyun  * in use by the dcache.
1967*4882a593Smuzhiyun  */
1968*4882a593Smuzhiyun 
d_instantiate(struct dentry * entry,struct inode * inode)1969*4882a593Smuzhiyun void d_instantiate(struct dentry *entry, struct inode * inode)
1970*4882a593Smuzhiyun {
1971*4882a593Smuzhiyun 	BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1972*4882a593Smuzhiyun 	if (inode) {
1973*4882a593Smuzhiyun 		security_d_instantiate(entry, inode);
1974*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
1975*4882a593Smuzhiyun 		__d_instantiate(entry, inode);
1976*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
1977*4882a593Smuzhiyun 	}
1978*4882a593Smuzhiyun }
1979*4882a593Smuzhiyun EXPORT_SYMBOL(d_instantiate);
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun /*
1982*4882a593Smuzhiyun  * This should be equivalent to d_instantiate() + unlock_new_inode(),
1983*4882a593Smuzhiyun  * with lockdep-related part of unlock_new_inode() done before
1984*4882a593Smuzhiyun  * anything else.  Use that instead of open-coding d_instantiate()/
1985*4882a593Smuzhiyun  * unlock_new_inode() combinations.
1986*4882a593Smuzhiyun  */
d_instantiate_new(struct dentry * entry,struct inode * inode)1987*4882a593Smuzhiyun void d_instantiate_new(struct dentry *entry, struct inode *inode)
1988*4882a593Smuzhiyun {
1989*4882a593Smuzhiyun 	BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1990*4882a593Smuzhiyun 	BUG_ON(!inode);
1991*4882a593Smuzhiyun 	lockdep_annotate_inode_mutex_key(inode);
1992*4882a593Smuzhiyun 	security_d_instantiate(entry, inode);
1993*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1994*4882a593Smuzhiyun 	__d_instantiate(entry, inode);
1995*4882a593Smuzhiyun 	WARN_ON(!(inode->i_state & I_NEW));
1996*4882a593Smuzhiyun 	inode->i_state &= ~I_NEW & ~I_CREATING;
1997*4882a593Smuzhiyun 	smp_mb();
1998*4882a593Smuzhiyun 	wake_up_bit(&inode->i_state, __I_NEW);
1999*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun EXPORT_SYMBOL(d_instantiate_new);
2002*4882a593Smuzhiyun 
d_make_root(struct inode * root_inode)2003*4882a593Smuzhiyun struct dentry *d_make_root(struct inode *root_inode)
2004*4882a593Smuzhiyun {
2005*4882a593Smuzhiyun 	struct dentry *res = NULL;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	if (root_inode) {
2008*4882a593Smuzhiyun 		res = d_alloc_anon(root_inode->i_sb);
2009*4882a593Smuzhiyun 		if (res)
2010*4882a593Smuzhiyun 			d_instantiate(res, root_inode);
2011*4882a593Smuzhiyun 		else
2012*4882a593Smuzhiyun 			iput(root_inode);
2013*4882a593Smuzhiyun 	}
2014*4882a593Smuzhiyun 	return res;
2015*4882a593Smuzhiyun }
2016*4882a593Smuzhiyun EXPORT_SYMBOL(d_make_root);
2017*4882a593Smuzhiyun 
__d_instantiate_anon(struct dentry * dentry,struct inode * inode,bool disconnected)2018*4882a593Smuzhiyun static struct dentry *__d_instantiate_anon(struct dentry *dentry,
2019*4882a593Smuzhiyun 					   struct inode *inode,
2020*4882a593Smuzhiyun 					   bool disconnected)
2021*4882a593Smuzhiyun {
2022*4882a593Smuzhiyun 	struct dentry *res;
2023*4882a593Smuzhiyun 	unsigned add_flags;
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	security_d_instantiate(dentry, inode);
2026*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
2027*4882a593Smuzhiyun 	res = __d_find_any_alias(inode);
2028*4882a593Smuzhiyun 	if (res) {
2029*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
2030*4882a593Smuzhiyun 		dput(dentry);
2031*4882a593Smuzhiyun 		goto out_iput;
2032*4882a593Smuzhiyun 	}
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun 	/* attach a disconnected dentry */
2035*4882a593Smuzhiyun 	add_flags = d_flags_for_inode(inode);
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun 	if (disconnected)
2038*4882a593Smuzhiyun 		add_flags |= DCACHE_DISCONNECTED;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
2041*4882a593Smuzhiyun 	__d_set_inode_and_type(dentry, inode, add_flags);
2042*4882a593Smuzhiyun 	hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2043*4882a593Smuzhiyun 	if (!disconnected) {
2044*4882a593Smuzhiyun 		hlist_bl_lock(&dentry->d_sb->s_roots);
2045*4882a593Smuzhiyun 		hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
2046*4882a593Smuzhiyun 		hlist_bl_unlock(&dentry->d_sb->s_roots);
2047*4882a593Smuzhiyun 	}
2048*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
2049*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun 	return dentry;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun  out_iput:
2054*4882a593Smuzhiyun 	iput(inode);
2055*4882a593Smuzhiyun 	return res;
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun 
d_instantiate_anon(struct dentry * dentry,struct inode * inode)2058*4882a593Smuzhiyun struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun 	return __d_instantiate_anon(dentry, inode, true);
2061*4882a593Smuzhiyun }
2062*4882a593Smuzhiyun EXPORT_SYMBOL(d_instantiate_anon);
2063*4882a593Smuzhiyun 
__d_obtain_alias(struct inode * inode,bool disconnected)2064*4882a593Smuzhiyun static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun 	struct dentry *tmp;
2067*4882a593Smuzhiyun 	struct dentry *res;
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	if (!inode)
2070*4882a593Smuzhiyun 		return ERR_PTR(-ESTALE);
2071*4882a593Smuzhiyun 	if (IS_ERR(inode))
2072*4882a593Smuzhiyun 		return ERR_CAST(inode);
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 	res = d_find_any_alias(inode);
2075*4882a593Smuzhiyun 	if (res)
2076*4882a593Smuzhiyun 		goto out_iput;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	tmp = d_alloc_anon(inode->i_sb);
2079*4882a593Smuzhiyun 	if (!tmp) {
2080*4882a593Smuzhiyun 		res = ERR_PTR(-ENOMEM);
2081*4882a593Smuzhiyun 		goto out_iput;
2082*4882a593Smuzhiyun 	}
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 	return __d_instantiate_anon(tmp, inode, disconnected);
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun out_iput:
2087*4882a593Smuzhiyun 	iput(inode);
2088*4882a593Smuzhiyun 	return res;
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun /**
2092*4882a593Smuzhiyun  * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2093*4882a593Smuzhiyun  * @inode: inode to allocate the dentry for
2094*4882a593Smuzhiyun  *
2095*4882a593Smuzhiyun  * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2096*4882a593Smuzhiyun  * similar open by handle operations.  The returned dentry may be anonymous,
2097*4882a593Smuzhiyun  * or may have a full name (if the inode was already in the cache).
2098*4882a593Smuzhiyun  *
2099*4882a593Smuzhiyun  * When called on a directory inode, we must ensure that the inode only ever
2100*4882a593Smuzhiyun  * has one dentry.  If a dentry is found, that is returned instead of
2101*4882a593Smuzhiyun  * allocating a new one.
2102*4882a593Smuzhiyun  *
2103*4882a593Smuzhiyun  * On successful return, the reference to the inode has been transferred
2104*4882a593Smuzhiyun  * to the dentry.  In case of an error the reference on the inode is released.
2105*4882a593Smuzhiyun  * To make it easier to use in export operations a %NULL or IS_ERR inode may
2106*4882a593Smuzhiyun  * be passed in and the error will be propagated to the return value,
2107*4882a593Smuzhiyun  * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
2108*4882a593Smuzhiyun  */
d_obtain_alias(struct inode * inode)2109*4882a593Smuzhiyun struct dentry *d_obtain_alias(struct inode *inode)
2110*4882a593Smuzhiyun {
2111*4882a593Smuzhiyun 	return __d_obtain_alias(inode, true);
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun EXPORT_SYMBOL_NS(d_obtain_alias, ANDROID_GKI_VFS_EXPORT_ONLY);
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun /**
2116*4882a593Smuzhiyun  * d_obtain_root - find or allocate a dentry for a given inode
2117*4882a593Smuzhiyun  * @inode: inode to allocate the dentry for
2118*4882a593Smuzhiyun  *
2119*4882a593Smuzhiyun  * Obtain an IS_ROOT dentry for the root of a filesystem.
2120*4882a593Smuzhiyun  *
2121*4882a593Smuzhiyun  * We must ensure that directory inodes only ever have one dentry.  If a
2122*4882a593Smuzhiyun  * dentry is found, that is returned instead of allocating a new one.
2123*4882a593Smuzhiyun  *
2124*4882a593Smuzhiyun  * On successful return, the reference to the inode has been transferred
2125*4882a593Smuzhiyun  * to the dentry.  In case of an error the reference on the inode is
2126*4882a593Smuzhiyun  * released.  A %NULL or IS_ERR inode may be passed in and will be the
2127*4882a593Smuzhiyun  * error will be propagate to the return value, with a %NULL @inode
2128*4882a593Smuzhiyun  * replaced by ERR_PTR(-ESTALE).
2129*4882a593Smuzhiyun  */
d_obtain_root(struct inode * inode)2130*4882a593Smuzhiyun struct dentry *d_obtain_root(struct inode *inode)
2131*4882a593Smuzhiyun {
2132*4882a593Smuzhiyun 	return __d_obtain_alias(inode, false);
2133*4882a593Smuzhiyun }
2134*4882a593Smuzhiyun EXPORT_SYMBOL(d_obtain_root);
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun /**
2137*4882a593Smuzhiyun  * d_add_ci - lookup or allocate new dentry with case-exact name
2138*4882a593Smuzhiyun  * @inode:  the inode case-insensitive lookup has found
2139*4882a593Smuzhiyun  * @dentry: the negative dentry that was passed to the parent's lookup func
2140*4882a593Smuzhiyun  * @name:   the case-exact name to be associated with the returned dentry
2141*4882a593Smuzhiyun  *
2142*4882a593Smuzhiyun  * This is to avoid filling the dcache with case-insensitive names to the
2143*4882a593Smuzhiyun  * same inode, only the actual correct case is stored in the dcache for
2144*4882a593Smuzhiyun  * case-insensitive filesystems.
2145*4882a593Smuzhiyun  *
2146*4882a593Smuzhiyun  * For a case-insensitive lookup match and if the the case-exact dentry
2147*4882a593Smuzhiyun  * already exists in in the dcache, use it and return it.
2148*4882a593Smuzhiyun  *
2149*4882a593Smuzhiyun  * If no entry exists with the exact case name, allocate new dentry with
2150*4882a593Smuzhiyun  * the exact case, and return the spliced entry.
2151*4882a593Smuzhiyun  */
d_add_ci(struct dentry * dentry,struct inode * inode,struct qstr * name)2152*4882a593Smuzhiyun struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2153*4882a593Smuzhiyun 			struct qstr *name)
2154*4882a593Smuzhiyun {
2155*4882a593Smuzhiyun 	struct dentry *found, *res;
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	/*
2158*4882a593Smuzhiyun 	 * First check if a dentry matching the name already exists,
2159*4882a593Smuzhiyun 	 * if not go ahead and create it now.
2160*4882a593Smuzhiyun 	 */
2161*4882a593Smuzhiyun 	found = d_hash_and_lookup(dentry->d_parent, name);
2162*4882a593Smuzhiyun 	if (found) {
2163*4882a593Smuzhiyun 		iput(inode);
2164*4882a593Smuzhiyun 		return found;
2165*4882a593Smuzhiyun 	}
2166*4882a593Smuzhiyun 	if (d_in_lookup(dentry)) {
2167*4882a593Smuzhiyun 		found = d_alloc_parallel(dentry->d_parent, name,
2168*4882a593Smuzhiyun 					dentry->d_wait);
2169*4882a593Smuzhiyun 		if (IS_ERR(found) || !d_in_lookup(found)) {
2170*4882a593Smuzhiyun 			iput(inode);
2171*4882a593Smuzhiyun 			return found;
2172*4882a593Smuzhiyun 		}
2173*4882a593Smuzhiyun 	} else {
2174*4882a593Smuzhiyun 		found = d_alloc(dentry->d_parent, name);
2175*4882a593Smuzhiyun 		if (!found) {
2176*4882a593Smuzhiyun 			iput(inode);
2177*4882a593Smuzhiyun 			return ERR_PTR(-ENOMEM);
2178*4882a593Smuzhiyun 		}
2179*4882a593Smuzhiyun 	}
2180*4882a593Smuzhiyun 	res = d_splice_alias(inode, found);
2181*4882a593Smuzhiyun 	if (res) {
2182*4882a593Smuzhiyun 		dput(found);
2183*4882a593Smuzhiyun 		return res;
2184*4882a593Smuzhiyun 	}
2185*4882a593Smuzhiyun 	return found;
2186*4882a593Smuzhiyun }
2187*4882a593Smuzhiyun EXPORT_SYMBOL_NS(d_add_ci, ANDROID_GKI_VFS_EXPORT_ONLY);
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 
d_same_name(const struct dentry * dentry,const struct dentry * parent,const struct qstr * name)2190*4882a593Smuzhiyun static inline bool d_same_name(const struct dentry *dentry,
2191*4882a593Smuzhiyun 				const struct dentry *parent,
2192*4882a593Smuzhiyun 				const struct qstr *name)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun 	if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
2195*4882a593Smuzhiyun 		if (dentry->d_name.len != name->len)
2196*4882a593Smuzhiyun 			return false;
2197*4882a593Smuzhiyun 		return dentry_cmp(dentry, name->name, name->len) == 0;
2198*4882a593Smuzhiyun 	}
2199*4882a593Smuzhiyun 	return parent->d_op->d_compare(dentry,
2200*4882a593Smuzhiyun 				       dentry->d_name.len, dentry->d_name.name,
2201*4882a593Smuzhiyun 				       name) == 0;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun /**
2205*4882a593Smuzhiyun  * __d_lookup_rcu - search for a dentry (racy, store-free)
2206*4882a593Smuzhiyun  * @parent: parent dentry
2207*4882a593Smuzhiyun  * @name: qstr of name we wish to find
2208*4882a593Smuzhiyun  * @seqp: returns d_seq value at the point where the dentry was found
2209*4882a593Smuzhiyun  * Returns: dentry, or NULL
2210*4882a593Smuzhiyun  *
2211*4882a593Smuzhiyun  * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2212*4882a593Smuzhiyun  * resolution (store-free path walking) design described in
2213*4882a593Smuzhiyun  * Documentation/filesystems/path-lookup.txt.
2214*4882a593Smuzhiyun  *
2215*4882a593Smuzhiyun  * This is not to be used outside core vfs.
2216*4882a593Smuzhiyun  *
2217*4882a593Smuzhiyun  * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2218*4882a593Smuzhiyun  * held, and rcu_read_lock held. The returned dentry must not be stored into
2219*4882a593Smuzhiyun  * without taking d_lock and checking d_seq sequence count against @seq
2220*4882a593Smuzhiyun  * returned here.
2221*4882a593Smuzhiyun  *
2222*4882a593Smuzhiyun  * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2223*4882a593Smuzhiyun  * function.
2224*4882a593Smuzhiyun  *
2225*4882a593Smuzhiyun  * Alternatively, __d_lookup_rcu may be called again to look up the child of
2226*4882a593Smuzhiyun  * the returned dentry, so long as its parent's seqlock is checked after the
2227*4882a593Smuzhiyun  * child is looked up. Thus, an interlocking stepping of sequence lock checks
2228*4882a593Smuzhiyun  * is formed, giving integrity down the path walk.
2229*4882a593Smuzhiyun  *
2230*4882a593Smuzhiyun  * NOTE! The caller *has* to check the resulting dentry against the sequence
2231*4882a593Smuzhiyun  * number we've returned before using any of the resulting dentry state!
2232*4882a593Smuzhiyun  */
__d_lookup_rcu(const struct dentry * parent,const struct qstr * name,unsigned * seqp)2233*4882a593Smuzhiyun struct dentry *__d_lookup_rcu(const struct dentry *parent,
2234*4882a593Smuzhiyun 				const struct qstr *name,
2235*4882a593Smuzhiyun 				unsigned *seqp)
2236*4882a593Smuzhiyun {
2237*4882a593Smuzhiyun 	u64 hashlen = name->hash_len;
2238*4882a593Smuzhiyun 	const unsigned char *str = name->name;
2239*4882a593Smuzhiyun 	struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
2240*4882a593Smuzhiyun 	struct hlist_bl_node *node;
2241*4882a593Smuzhiyun 	struct dentry *dentry;
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	/*
2244*4882a593Smuzhiyun 	 * Note: There is significant duplication with __d_lookup_rcu which is
2245*4882a593Smuzhiyun 	 * required to prevent single threaded performance regressions
2246*4882a593Smuzhiyun 	 * especially on architectures where smp_rmb (in seqcounts) are costly.
2247*4882a593Smuzhiyun 	 * Keep the two functions in sync.
2248*4882a593Smuzhiyun 	 */
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 	/*
2251*4882a593Smuzhiyun 	 * The hash list is protected using RCU.
2252*4882a593Smuzhiyun 	 *
2253*4882a593Smuzhiyun 	 * Carefully use d_seq when comparing a candidate dentry, to avoid
2254*4882a593Smuzhiyun 	 * races with d_move().
2255*4882a593Smuzhiyun 	 *
2256*4882a593Smuzhiyun 	 * It is possible that concurrent renames can mess up our list
2257*4882a593Smuzhiyun 	 * walk here and result in missing our dentry, resulting in the
2258*4882a593Smuzhiyun 	 * false-negative result. d_lookup() protects against concurrent
2259*4882a593Smuzhiyun 	 * renames using rename_lock seqlock.
2260*4882a593Smuzhiyun 	 *
2261*4882a593Smuzhiyun 	 * See Documentation/filesystems/path-lookup.txt for more details.
2262*4882a593Smuzhiyun 	 */
2263*4882a593Smuzhiyun 	hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2264*4882a593Smuzhiyun 		unsigned seq;
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun seqretry:
2267*4882a593Smuzhiyun 		/*
2268*4882a593Smuzhiyun 		 * The dentry sequence count protects us from concurrent
2269*4882a593Smuzhiyun 		 * renames, and thus protects parent and name fields.
2270*4882a593Smuzhiyun 		 *
2271*4882a593Smuzhiyun 		 * The caller must perform a seqcount check in order
2272*4882a593Smuzhiyun 		 * to do anything useful with the returned dentry.
2273*4882a593Smuzhiyun 		 *
2274*4882a593Smuzhiyun 		 * NOTE! We do a "raw" seqcount_begin here. That means that
2275*4882a593Smuzhiyun 		 * we don't wait for the sequence count to stabilize if it
2276*4882a593Smuzhiyun 		 * is in the middle of a sequence change. If we do the slow
2277*4882a593Smuzhiyun 		 * dentry compare, we will do seqretries until it is stable,
2278*4882a593Smuzhiyun 		 * and if we end up with a successful lookup, we actually
2279*4882a593Smuzhiyun 		 * want to exit RCU lookup anyway.
2280*4882a593Smuzhiyun 		 *
2281*4882a593Smuzhiyun 		 * Note that raw_seqcount_begin still *does* smp_rmb(), so
2282*4882a593Smuzhiyun 		 * we are still guaranteed NUL-termination of ->d_name.name.
2283*4882a593Smuzhiyun 		 */
2284*4882a593Smuzhiyun 		seq = raw_seqcount_begin(&dentry->d_seq);
2285*4882a593Smuzhiyun 		if (dentry->d_parent != parent)
2286*4882a593Smuzhiyun 			continue;
2287*4882a593Smuzhiyun 		if (d_unhashed(dentry))
2288*4882a593Smuzhiyun 			continue;
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 		if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
2291*4882a593Smuzhiyun 			int tlen;
2292*4882a593Smuzhiyun 			const char *tname;
2293*4882a593Smuzhiyun 			if (dentry->d_name.hash != hashlen_hash(hashlen))
2294*4882a593Smuzhiyun 				continue;
2295*4882a593Smuzhiyun 			tlen = dentry->d_name.len;
2296*4882a593Smuzhiyun 			tname = dentry->d_name.name;
2297*4882a593Smuzhiyun 			/* we want a consistent (name,len) pair */
2298*4882a593Smuzhiyun 			if (read_seqcount_retry(&dentry->d_seq, seq)) {
2299*4882a593Smuzhiyun 				cpu_relax();
2300*4882a593Smuzhiyun 				goto seqretry;
2301*4882a593Smuzhiyun 			}
2302*4882a593Smuzhiyun 			if (parent->d_op->d_compare(dentry,
2303*4882a593Smuzhiyun 						    tlen, tname, name) != 0)
2304*4882a593Smuzhiyun 				continue;
2305*4882a593Smuzhiyun 		} else {
2306*4882a593Smuzhiyun 			if (dentry->d_name.hash_len != hashlen)
2307*4882a593Smuzhiyun 				continue;
2308*4882a593Smuzhiyun 			if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2309*4882a593Smuzhiyun 				continue;
2310*4882a593Smuzhiyun 		}
2311*4882a593Smuzhiyun 		*seqp = seq;
2312*4882a593Smuzhiyun 		return dentry;
2313*4882a593Smuzhiyun 	}
2314*4882a593Smuzhiyun 	return NULL;
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun /**
2318*4882a593Smuzhiyun  * d_lookup - search for a dentry
2319*4882a593Smuzhiyun  * @parent: parent dentry
2320*4882a593Smuzhiyun  * @name: qstr of name we wish to find
2321*4882a593Smuzhiyun  * Returns: dentry, or NULL
2322*4882a593Smuzhiyun  *
2323*4882a593Smuzhiyun  * d_lookup searches the children of the parent dentry for the name in
2324*4882a593Smuzhiyun  * question. If the dentry is found its reference count is incremented and the
2325*4882a593Smuzhiyun  * dentry is returned. The caller must use dput to free the entry when it has
2326*4882a593Smuzhiyun  * finished using it. %NULL is returned if the dentry does not exist.
2327*4882a593Smuzhiyun  */
d_lookup(const struct dentry * parent,const struct qstr * name)2328*4882a593Smuzhiyun struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2329*4882a593Smuzhiyun {
2330*4882a593Smuzhiyun 	struct dentry *dentry;
2331*4882a593Smuzhiyun 	unsigned seq;
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	do {
2334*4882a593Smuzhiyun 		seq = read_seqbegin(&rename_lock);
2335*4882a593Smuzhiyun 		dentry = __d_lookup(parent, name);
2336*4882a593Smuzhiyun 		if (dentry)
2337*4882a593Smuzhiyun 			break;
2338*4882a593Smuzhiyun 	} while (read_seqretry(&rename_lock, seq));
2339*4882a593Smuzhiyun 	return dentry;
2340*4882a593Smuzhiyun }
2341*4882a593Smuzhiyun EXPORT_SYMBOL(d_lookup);
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun /**
2344*4882a593Smuzhiyun  * __d_lookup - search for a dentry (racy)
2345*4882a593Smuzhiyun  * @parent: parent dentry
2346*4882a593Smuzhiyun  * @name: qstr of name we wish to find
2347*4882a593Smuzhiyun  * Returns: dentry, or NULL
2348*4882a593Smuzhiyun  *
2349*4882a593Smuzhiyun  * __d_lookup is like d_lookup, however it may (rarely) return a
2350*4882a593Smuzhiyun  * false-negative result due to unrelated rename activity.
2351*4882a593Smuzhiyun  *
2352*4882a593Smuzhiyun  * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2353*4882a593Smuzhiyun  * however it must be used carefully, eg. with a following d_lookup in
2354*4882a593Smuzhiyun  * the case of failure.
2355*4882a593Smuzhiyun  *
2356*4882a593Smuzhiyun  * __d_lookup callers must be commented.
2357*4882a593Smuzhiyun  */
__d_lookup(const struct dentry * parent,const struct qstr * name)2358*4882a593Smuzhiyun struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2359*4882a593Smuzhiyun {
2360*4882a593Smuzhiyun 	unsigned int hash = name->hash;
2361*4882a593Smuzhiyun 	struct hlist_bl_head *b = d_hash(hash);
2362*4882a593Smuzhiyun 	struct hlist_bl_node *node;
2363*4882a593Smuzhiyun 	struct dentry *found = NULL;
2364*4882a593Smuzhiyun 	struct dentry *dentry;
2365*4882a593Smuzhiyun 
2366*4882a593Smuzhiyun 	/*
2367*4882a593Smuzhiyun 	 * Note: There is significant duplication with __d_lookup_rcu which is
2368*4882a593Smuzhiyun 	 * required to prevent single threaded performance regressions
2369*4882a593Smuzhiyun 	 * especially on architectures where smp_rmb (in seqcounts) are costly.
2370*4882a593Smuzhiyun 	 * Keep the two functions in sync.
2371*4882a593Smuzhiyun 	 */
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun 	/*
2374*4882a593Smuzhiyun 	 * The hash list is protected using RCU.
2375*4882a593Smuzhiyun 	 *
2376*4882a593Smuzhiyun 	 * Take d_lock when comparing a candidate dentry, to avoid races
2377*4882a593Smuzhiyun 	 * with d_move().
2378*4882a593Smuzhiyun 	 *
2379*4882a593Smuzhiyun 	 * It is possible that concurrent renames can mess up our list
2380*4882a593Smuzhiyun 	 * walk here and result in missing our dentry, resulting in the
2381*4882a593Smuzhiyun 	 * false-negative result. d_lookup() protects against concurrent
2382*4882a593Smuzhiyun 	 * renames using rename_lock seqlock.
2383*4882a593Smuzhiyun 	 *
2384*4882a593Smuzhiyun 	 * See Documentation/filesystems/path-lookup.txt for more details.
2385*4882a593Smuzhiyun 	 */
2386*4882a593Smuzhiyun 	rcu_read_lock();
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun 	hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2389*4882a593Smuzhiyun 
2390*4882a593Smuzhiyun 		if (dentry->d_name.hash != hash)
2391*4882a593Smuzhiyun 			continue;
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
2394*4882a593Smuzhiyun 		if (dentry->d_parent != parent)
2395*4882a593Smuzhiyun 			goto next;
2396*4882a593Smuzhiyun 		if (d_unhashed(dentry))
2397*4882a593Smuzhiyun 			goto next;
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun 		if (!d_same_name(dentry, parent, name))
2400*4882a593Smuzhiyun 			goto next;
2401*4882a593Smuzhiyun 
2402*4882a593Smuzhiyun 		dentry->d_lockref.count++;
2403*4882a593Smuzhiyun 		found = dentry;
2404*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
2405*4882a593Smuzhiyun 		break;
2406*4882a593Smuzhiyun next:
2407*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
2408*4882a593Smuzhiyun  	}
2409*4882a593Smuzhiyun  	rcu_read_unlock();
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun  	return found;
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun /**
2415*4882a593Smuzhiyun  * d_hash_and_lookup - hash the qstr then search for a dentry
2416*4882a593Smuzhiyun  * @dir: Directory to search in
2417*4882a593Smuzhiyun  * @name: qstr of name we wish to find
2418*4882a593Smuzhiyun  *
2419*4882a593Smuzhiyun  * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
2420*4882a593Smuzhiyun  */
d_hash_and_lookup(struct dentry * dir,struct qstr * name)2421*4882a593Smuzhiyun struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2422*4882a593Smuzhiyun {
2423*4882a593Smuzhiyun 	/*
2424*4882a593Smuzhiyun 	 * Check for a fs-specific hash function. Note that we must
2425*4882a593Smuzhiyun 	 * calculate the standard hash first, as the d_op->d_hash()
2426*4882a593Smuzhiyun 	 * routine may choose to leave the hash value unchanged.
2427*4882a593Smuzhiyun 	 */
2428*4882a593Smuzhiyun 	name->hash = full_name_hash(dir, name->name, name->len);
2429*4882a593Smuzhiyun 	if (dir->d_flags & DCACHE_OP_HASH) {
2430*4882a593Smuzhiyun 		int err = dir->d_op->d_hash(dir, name);
2431*4882a593Smuzhiyun 		if (unlikely(err < 0))
2432*4882a593Smuzhiyun 			return ERR_PTR(err);
2433*4882a593Smuzhiyun 	}
2434*4882a593Smuzhiyun 	return d_lookup(dir, name);
2435*4882a593Smuzhiyun }
2436*4882a593Smuzhiyun EXPORT_SYMBOL(d_hash_and_lookup);
2437*4882a593Smuzhiyun 
2438*4882a593Smuzhiyun /*
2439*4882a593Smuzhiyun  * When a file is deleted, we have two options:
2440*4882a593Smuzhiyun  * - turn this dentry into a negative dentry
2441*4882a593Smuzhiyun  * - unhash this dentry and free it.
2442*4882a593Smuzhiyun  *
2443*4882a593Smuzhiyun  * Usually, we want to just turn this into
2444*4882a593Smuzhiyun  * a negative dentry, but if anybody else is
2445*4882a593Smuzhiyun  * currently using the dentry or the inode
2446*4882a593Smuzhiyun  * we can't do that and we fall back on removing
2447*4882a593Smuzhiyun  * it from the hash queues and waiting for
2448*4882a593Smuzhiyun  * it to be deleted later when it has no users
2449*4882a593Smuzhiyun  */
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun /**
2452*4882a593Smuzhiyun  * d_delete - delete a dentry
2453*4882a593Smuzhiyun  * @dentry: The dentry to delete
2454*4882a593Smuzhiyun  *
2455*4882a593Smuzhiyun  * Turn the dentry into a negative dentry if possible, otherwise
2456*4882a593Smuzhiyun  * remove it from the hash queues so it can be deleted later
2457*4882a593Smuzhiyun  */
2458*4882a593Smuzhiyun 
d_delete(struct dentry * dentry)2459*4882a593Smuzhiyun void d_delete(struct dentry * dentry)
2460*4882a593Smuzhiyun {
2461*4882a593Smuzhiyun 	struct inode *inode = dentry->d_inode;
2462*4882a593Smuzhiyun 
2463*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
2464*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
2465*4882a593Smuzhiyun 	/*
2466*4882a593Smuzhiyun 	 * Are we the only user?
2467*4882a593Smuzhiyun 	 */
2468*4882a593Smuzhiyun 	if (dentry->d_lockref.count == 1) {
2469*4882a593Smuzhiyun 		dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2470*4882a593Smuzhiyun 		dentry_unlink_inode(dentry);
2471*4882a593Smuzhiyun 	} else {
2472*4882a593Smuzhiyun 		__d_drop(dentry);
2473*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
2474*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
2475*4882a593Smuzhiyun 	}
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun EXPORT_SYMBOL(d_delete);
2478*4882a593Smuzhiyun 
__d_rehash(struct dentry * entry)2479*4882a593Smuzhiyun static void __d_rehash(struct dentry *entry)
2480*4882a593Smuzhiyun {
2481*4882a593Smuzhiyun 	struct hlist_bl_head *b = d_hash(entry->d_name.hash);
2482*4882a593Smuzhiyun 
2483*4882a593Smuzhiyun 	hlist_bl_lock(b);
2484*4882a593Smuzhiyun 	hlist_bl_add_head_rcu(&entry->d_hash, b);
2485*4882a593Smuzhiyun 	hlist_bl_unlock(b);
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun /**
2489*4882a593Smuzhiyun  * d_rehash	- add an entry back to the hash
2490*4882a593Smuzhiyun  * @entry: dentry to add to the hash
2491*4882a593Smuzhiyun  *
2492*4882a593Smuzhiyun  * Adds a dentry to the hash according to its name.
2493*4882a593Smuzhiyun  */
2494*4882a593Smuzhiyun 
d_rehash(struct dentry * entry)2495*4882a593Smuzhiyun void d_rehash(struct dentry * entry)
2496*4882a593Smuzhiyun {
2497*4882a593Smuzhiyun 	spin_lock(&entry->d_lock);
2498*4882a593Smuzhiyun 	__d_rehash(entry);
2499*4882a593Smuzhiyun 	spin_unlock(&entry->d_lock);
2500*4882a593Smuzhiyun }
2501*4882a593Smuzhiyun EXPORT_SYMBOL(d_rehash);
2502*4882a593Smuzhiyun 
start_dir_add(struct inode * dir)2503*4882a593Smuzhiyun static inline unsigned start_dir_add(struct inode *dir)
2504*4882a593Smuzhiyun {
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun 	for (;;) {
2507*4882a593Smuzhiyun 		unsigned n = dir->i_dir_seq;
2508*4882a593Smuzhiyun 		if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
2509*4882a593Smuzhiyun 			return n;
2510*4882a593Smuzhiyun 		cpu_relax();
2511*4882a593Smuzhiyun 	}
2512*4882a593Smuzhiyun }
2513*4882a593Smuzhiyun 
end_dir_add(struct inode * dir,unsigned n)2514*4882a593Smuzhiyun static inline void end_dir_add(struct inode *dir, unsigned n)
2515*4882a593Smuzhiyun {
2516*4882a593Smuzhiyun 	smp_store_release(&dir->i_dir_seq, n + 2);
2517*4882a593Smuzhiyun }
2518*4882a593Smuzhiyun 
d_wait_lookup(struct dentry * dentry)2519*4882a593Smuzhiyun static void d_wait_lookup(struct dentry *dentry)
2520*4882a593Smuzhiyun {
2521*4882a593Smuzhiyun 	if (d_in_lookup(dentry)) {
2522*4882a593Smuzhiyun 		DECLARE_WAITQUEUE(wait, current);
2523*4882a593Smuzhiyun 		add_wait_queue(dentry->d_wait, &wait);
2524*4882a593Smuzhiyun 		do {
2525*4882a593Smuzhiyun 			set_current_state(TASK_UNINTERRUPTIBLE);
2526*4882a593Smuzhiyun 			spin_unlock(&dentry->d_lock);
2527*4882a593Smuzhiyun 			schedule();
2528*4882a593Smuzhiyun 			spin_lock(&dentry->d_lock);
2529*4882a593Smuzhiyun 		} while (d_in_lookup(dentry));
2530*4882a593Smuzhiyun 	}
2531*4882a593Smuzhiyun }
2532*4882a593Smuzhiyun 
d_alloc_parallel(struct dentry * parent,const struct qstr * name,wait_queue_head_t * wq)2533*4882a593Smuzhiyun struct dentry *d_alloc_parallel(struct dentry *parent,
2534*4882a593Smuzhiyun 				const struct qstr *name,
2535*4882a593Smuzhiyun 				wait_queue_head_t *wq)
2536*4882a593Smuzhiyun {
2537*4882a593Smuzhiyun 	unsigned int hash = name->hash;
2538*4882a593Smuzhiyun 	struct hlist_bl_head *b = in_lookup_hash(parent, hash);
2539*4882a593Smuzhiyun 	struct hlist_bl_node *node;
2540*4882a593Smuzhiyun 	struct dentry *new = d_alloc(parent, name);
2541*4882a593Smuzhiyun 	struct dentry *dentry;
2542*4882a593Smuzhiyun 	unsigned seq, r_seq, d_seq;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	if (unlikely(!new))
2545*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun retry:
2548*4882a593Smuzhiyun 	rcu_read_lock();
2549*4882a593Smuzhiyun 	seq = smp_load_acquire(&parent->d_inode->i_dir_seq);
2550*4882a593Smuzhiyun 	r_seq = read_seqbegin(&rename_lock);
2551*4882a593Smuzhiyun 	dentry = __d_lookup_rcu(parent, name, &d_seq);
2552*4882a593Smuzhiyun 	if (unlikely(dentry)) {
2553*4882a593Smuzhiyun 		if (!lockref_get_not_dead(&dentry->d_lockref)) {
2554*4882a593Smuzhiyun 			rcu_read_unlock();
2555*4882a593Smuzhiyun 			goto retry;
2556*4882a593Smuzhiyun 		}
2557*4882a593Smuzhiyun 		if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2558*4882a593Smuzhiyun 			rcu_read_unlock();
2559*4882a593Smuzhiyun 			dput(dentry);
2560*4882a593Smuzhiyun 			goto retry;
2561*4882a593Smuzhiyun 		}
2562*4882a593Smuzhiyun 		rcu_read_unlock();
2563*4882a593Smuzhiyun 		dput(new);
2564*4882a593Smuzhiyun 		return dentry;
2565*4882a593Smuzhiyun 	}
2566*4882a593Smuzhiyun 	if (unlikely(read_seqretry(&rename_lock, r_seq))) {
2567*4882a593Smuzhiyun 		rcu_read_unlock();
2568*4882a593Smuzhiyun 		goto retry;
2569*4882a593Smuzhiyun 	}
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun 	if (unlikely(seq & 1)) {
2572*4882a593Smuzhiyun 		rcu_read_unlock();
2573*4882a593Smuzhiyun 		goto retry;
2574*4882a593Smuzhiyun 	}
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 	hlist_bl_lock(b);
2577*4882a593Smuzhiyun 	if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) {
2578*4882a593Smuzhiyun 		hlist_bl_unlock(b);
2579*4882a593Smuzhiyun 		rcu_read_unlock();
2580*4882a593Smuzhiyun 		goto retry;
2581*4882a593Smuzhiyun 	}
2582*4882a593Smuzhiyun 	/*
2583*4882a593Smuzhiyun 	 * No changes for the parent since the beginning of d_lookup().
2584*4882a593Smuzhiyun 	 * Since all removals from the chain happen with hlist_bl_lock(),
2585*4882a593Smuzhiyun 	 * any potential in-lookup matches are going to stay here until
2586*4882a593Smuzhiyun 	 * we unlock the chain.  All fields are stable in everything
2587*4882a593Smuzhiyun 	 * we encounter.
2588*4882a593Smuzhiyun 	 */
2589*4882a593Smuzhiyun 	hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2590*4882a593Smuzhiyun 		if (dentry->d_name.hash != hash)
2591*4882a593Smuzhiyun 			continue;
2592*4882a593Smuzhiyun 		if (dentry->d_parent != parent)
2593*4882a593Smuzhiyun 			continue;
2594*4882a593Smuzhiyun 		if (!d_same_name(dentry, parent, name))
2595*4882a593Smuzhiyun 			continue;
2596*4882a593Smuzhiyun 		hlist_bl_unlock(b);
2597*4882a593Smuzhiyun 		/* now we can try to grab a reference */
2598*4882a593Smuzhiyun 		if (!lockref_get_not_dead(&dentry->d_lockref)) {
2599*4882a593Smuzhiyun 			rcu_read_unlock();
2600*4882a593Smuzhiyun 			goto retry;
2601*4882a593Smuzhiyun 		}
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun 		rcu_read_unlock();
2604*4882a593Smuzhiyun 		/*
2605*4882a593Smuzhiyun 		 * somebody is likely to be still doing lookup for it;
2606*4882a593Smuzhiyun 		 * wait for them to finish
2607*4882a593Smuzhiyun 		 */
2608*4882a593Smuzhiyun 		spin_lock(&dentry->d_lock);
2609*4882a593Smuzhiyun 		d_wait_lookup(dentry);
2610*4882a593Smuzhiyun 		/*
2611*4882a593Smuzhiyun 		 * it's not in-lookup anymore; in principle we should repeat
2612*4882a593Smuzhiyun 		 * everything from dcache lookup, but it's likely to be what
2613*4882a593Smuzhiyun 		 * d_lookup() would've found anyway.  If it is, just return it;
2614*4882a593Smuzhiyun 		 * otherwise we really have to repeat the whole thing.
2615*4882a593Smuzhiyun 		 */
2616*4882a593Smuzhiyun 		if (unlikely(dentry->d_name.hash != hash))
2617*4882a593Smuzhiyun 			goto mismatch;
2618*4882a593Smuzhiyun 		if (unlikely(dentry->d_parent != parent))
2619*4882a593Smuzhiyun 			goto mismatch;
2620*4882a593Smuzhiyun 		if (unlikely(d_unhashed(dentry)))
2621*4882a593Smuzhiyun 			goto mismatch;
2622*4882a593Smuzhiyun 		if (unlikely(!d_same_name(dentry, parent, name)))
2623*4882a593Smuzhiyun 			goto mismatch;
2624*4882a593Smuzhiyun 		/* OK, it *is* a hashed match; return it */
2625*4882a593Smuzhiyun 		spin_unlock(&dentry->d_lock);
2626*4882a593Smuzhiyun 		dput(new);
2627*4882a593Smuzhiyun 		return dentry;
2628*4882a593Smuzhiyun 	}
2629*4882a593Smuzhiyun 	rcu_read_unlock();
2630*4882a593Smuzhiyun 	/* we can't take ->d_lock here; it's OK, though. */
2631*4882a593Smuzhiyun 	new->d_flags |= DCACHE_PAR_LOOKUP;
2632*4882a593Smuzhiyun 	new->d_wait = wq;
2633*4882a593Smuzhiyun 	hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
2634*4882a593Smuzhiyun 	hlist_bl_unlock(b);
2635*4882a593Smuzhiyun 	return new;
2636*4882a593Smuzhiyun mismatch:
2637*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
2638*4882a593Smuzhiyun 	dput(dentry);
2639*4882a593Smuzhiyun 	goto retry;
2640*4882a593Smuzhiyun }
2641*4882a593Smuzhiyun EXPORT_SYMBOL(d_alloc_parallel);
2642*4882a593Smuzhiyun 
__d_lookup_done(struct dentry * dentry)2643*4882a593Smuzhiyun void __d_lookup_done(struct dentry *dentry)
2644*4882a593Smuzhiyun {
2645*4882a593Smuzhiyun 	struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
2646*4882a593Smuzhiyun 						 dentry->d_name.hash);
2647*4882a593Smuzhiyun 	hlist_bl_lock(b);
2648*4882a593Smuzhiyun 	dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
2649*4882a593Smuzhiyun 	__hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
2650*4882a593Smuzhiyun 	wake_up_all(dentry->d_wait);
2651*4882a593Smuzhiyun 	dentry->d_wait = NULL;
2652*4882a593Smuzhiyun 	hlist_bl_unlock(b);
2653*4882a593Smuzhiyun 	INIT_HLIST_NODE(&dentry->d_u.d_alias);
2654*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dentry->d_lru);
2655*4882a593Smuzhiyun }
2656*4882a593Smuzhiyun EXPORT_SYMBOL(__d_lookup_done);
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun /* inode->i_lock held if inode is non-NULL */
2659*4882a593Smuzhiyun 
__d_add(struct dentry * dentry,struct inode * inode)2660*4882a593Smuzhiyun static inline void __d_add(struct dentry *dentry, struct inode *inode)
2661*4882a593Smuzhiyun {
2662*4882a593Smuzhiyun 	struct inode *dir = NULL;
2663*4882a593Smuzhiyun 	unsigned n;
2664*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
2665*4882a593Smuzhiyun 	if (unlikely(d_in_lookup(dentry))) {
2666*4882a593Smuzhiyun 		dir = dentry->d_parent->d_inode;
2667*4882a593Smuzhiyun 		n = start_dir_add(dir);
2668*4882a593Smuzhiyun 		__d_lookup_done(dentry);
2669*4882a593Smuzhiyun 	}
2670*4882a593Smuzhiyun 	if (inode) {
2671*4882a593Smuzhiyun 		unsigned add_flags = d_flags_for_inode(inode);
2672*4882a593Smuzhiyun 		hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2673*4882a593Smuzhiyun 		raw_write_seqcount_begin(&dentry->d_seq);
2674*4882a593Smuzhiyun 		__d_set_inode_and_type(dentry, inode, add_flags);
2675*4882a593Smuzhiyun 		raw_write_seqcount_end(&dentry->d_seq);
2676*4882a593Smuzhiyun 		fsnotify_update_flags(dentry);
2677*4882a593Smuzhiyun 	}
2678*4882a593Smuzhiyun 	__d_rehash(dentry);
2679*4882a593Smuzhiyun 	if (dir)
2680*4882a593Smuzhiyun 		end_dir_add(dir, n);
2681*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
2682*4882a593Smuzhiyun 	if (inode)
2683*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
2684*4882a593Smuzhiyun }
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun /**
2687*4882a593Smuzhiyun  * d_add - add dentry to hash queues
2688*4882a593Smuzhiyun  * @entry: dentry to add
2689*4882a593Smuzhiyun  * @inode: The inode to attach to this dentry
2690*4882a593Smuzhiyun  *
2691*4882a593Smuzhiyun  * This adds the entry to the hash queues and initializes @inode.
2692*4882a593Smuzhiyun  * The entry was actually filled in earlier during d_alloc().
2693*4882a593Smuzhiyun  */
2694*4882a593Smuzhiyun 
d_add(struct dentry * entry,struct inode * inode)2695*4882a593Smuzhiyun void d_add(struct dentry *entry, struct inode *inode)
2696*4882a593Smuzhiyun {
2697*4882a593Smuzhiyun 	if (inode) {
2698*4882a593Smuzhiyun 		security_d_instantiate(entry, inode);
2699*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
2700*4882a593Smuzhiyun 	}
2701*4882a593Smuzhiyun 	__d_add(entry, inode);
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun EXPORT_SYMBOL(d_add);
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun /**
2706*4882a593Smuzhiyun  * d_exact_alias - find and hash an exact unhashed alias
2707*4882a593Smuzhiyun  * @entry: dentry to add
2708*4882a593Smuzhiyun  * @inode: The inode to go with this dentry
2709*4882a593Smuzhiyun  *
2710*4882a593Smuzhiyun  * If an unhashed dentry with the same name/parent and desired
2711*4882a593Smuzhiyun  * inode already exists, hash and return it.  Otherwise, return
2712*4882a593Smuzhiyun  * NULL.
2713*4882a593Smuzhiyun  *
2714*4882a593Smuzhiyun  * Parent directory should be locked.
2715*4882a593Smuzhiyun  */
d_exact_alias(struct dentry * entry,struct inode * inode)2716*4882a593Smuzhiyun struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2717*4882a593Smuzhiyun {
2718*4882a593Smuzhiyun 	struct dentry *alias;
2719*4882a593Smuzhiyun 	unsigned int hash = entry->d_name.hash;
2720*4882a593Smuzhiyun 
2721*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
2722*4882a593Smuzhiyun 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
2723*4882a593Smuzhiyun 		/*
2724*4882a593Smuzhiyun 		 * Don't need alias->d_lock here, because aliases with
2725*4882a593Smuzhiyun 		 * d_parent == entry->d_parent are not subject to name or
2726*4882a593Smuzhiyun 		 * parent changes, because the parent inode i_mutex is held.
2727*4882a593Smuzhiyun 		 */
2728*4882a593Smuzhiyun 		if (alias->d_name.hash != hash)
2729*4882a593Smuzhiyun 			continue;
2730*4882a593Smuzhiyun 		if (alias->d_parent != entry->d_parent)
2731*4882a593Smuzhiyun 			continue;
2732*4882a593Smuzhiyun 		if (!d_same_name(alias, entry->d_parent, &entry->d_name))
2733*4882a593Smuzhiyun 			continue;
2734*4882a593Smuzhiyun 		spin_lock(&alias->d_lock);
2735*4882a593Smuzhiyun 		if (!d_unhashed(alias)) {
2736*4882a593Smuzhiyun 			spin_unlock(&alias->d_lock);
2737*4882a593Smuzhiyun 			alias = NULL;
2738*4882a593Smuzhiyun 		} else {
2739*4882a593Smuzhiyun 			__dget_dlock(alias);
2740*4882a593Smuzhiyun 			__d_rehash(alias);
2741*4882a593Smuzhiyun 			spin_unlock(&alias->d_lock);
2742*4882a593Smuzhiyun 		}
2743*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
2744*4882a593Smuzhiyun 		return alias;
2745*4882a593Smuzhiyun 	}
2746*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
2747*4882a593Smuzhiyun 	return NULL;
2748*4882a593Smuzhiyun }
2749*4882a593Smuzhiyun EXPORT_SYMBOL(d_exact_alias);
2750*4882a593Smuzhiyun 
swap_names(struct dentry * dentry,struct dentry * target)2751*4882a593Smuzhiyun static void swap_names(struct dentry *dentry, struct dentry *target)
2752*4882a593Smuzhiyun {
2753*4882a593Smuzhiyun 	if (unlikely(dname_external(target))) {
2754*4882a593Smuzhiyun 		if (unlikely(dname_external(dentry))) {
2755*4882a593Smuzhiyun 			/*
2756*4882a593Smuzhiyun 			 * Both external: swap the pointers
2757*4882a593Smuzhiyun 			 */
2758*4882a593Smuzhiyun 			swap(target->d_name.name, dentry->d_name.name);
2759*4882a593Smuzhiyun 		} else {
2760*4882a593Smuzhiyun 			/*
2761*4882a593Smuzhiyun 			 * dentry:internal, target:external.  Steal target's
2762*4882a593Smuzhiyun 			 * storage and make target internal.
2763*4882a593Smuzhiyun 			 */
2764*4882a593Smuzhiyun 			memcpy(target->d_iname, dentry->d_name.name,
2765*4882a593Smuzhiyun 					dentry->d_name.len + 1);
2766*4882a593Smuzhiyun 			dentry->d_name.name = target->d_name.name;
2767*4882a593Smuzhiyun 			target->d_name.name = target->d_iname;
2768*4882a593Smuzhiyun 		}
2769*4882a593Smuzhiyun 	} else {
2770*4882a593Smuzhiyun 		if (unlikely(dname_external(dentry))) {
2771*4882a593Smuzhiyun 			/*
2772*4882a593Smuzhiyun 			 * dentry:external, target:internal.  Give dentry's
2773*4882a593Smuzhiyun 			 * storage to target and make dentry internal
2774*4882a593Smuzhiyun 			 */
2775*4882a593Smuzhiyun 			memcpy(dentry->d_iname, target->d_name.name,
2776*4882a593Smuzhiyun 					target->d_name.len + 1);
2777*4882a593Smuzhiyun 			target->d_name.name = dentry->d_name.name;
2778*4882a593Smuzhiyun 			dentry->d_name.name = dentry->d_iname;
2779*4882a593Smuzhiyun 		} else {
2780*4882a593Smuzhiyun 			/*
2781*4882a593Smuzhiyun 			 * Both are internal.
2782*4882a593Smuzhiyun 			 */
2783*4882a593Smuzhiyun 			unsigned int i;
2784*4882a593Smuzhiyun 			BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2785*4882a593Smuzhiyun 			for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2786*4882a593Smuzhiyun 				swap(((long *) &dentry->d_iname)[i],
2787*4882a593Smuzhiyun 				     ((long *) &target->d_iname)[i]);
2788*4882a593Smuzhiyun 			}
2789*4882a593Smuzhiyun 		}
2790*4882a593Smuzhiyun 	}
2791*4882a593Smuzhiyun 	swap(dentry->d_name.hash_len, target->d_name.hash_len);
2792*4882a593Smuzhiyun }
2793*4882a593Smuzhiyun 
copy_name(struct dentry * dentry,struct dentry * target)2794*4882a593Smuzhiyun static void copy_name(struct dentry *dentry, struct dentry *target)
2795*4882a593Smuzhiyun {
2796*4882a593Smuzhiyun 	struct external_name *old_name = NULL;
2797*4882a593Smuzhiyun 	if (unlikely(dname_external(dentry)))
2798*4882a593Smuzhiyun 		old_name = external_name(dentry);
2799*4882a593Smuzhiyun 	if (unlikely(dname_external(target))) {
2800*4882a593Smuzhiyun 		atomic_inc(&external_name(target)->u.count);
2801*4882a593Smuzhiyun 		dentry->d_name = target->d_name;
2802*4882a593Smuzhiyun 	} else {
2803*4882a593Smuzhiyun 		memcpy(dentry->d_iname, target->d_name.name,
2804*4882a593Smuzhiyun 				target->d_name.len + 1);
2805*4882a593Smuzhiyun 		dentry->d_name.name = dentry->d_iname;
2806*4882a593Smuzhiyun 		dentry->d_name.hash_len = target->d_name.hash_len;
2807*4882a593Smuzhiyun 	}
2808*4882a593Smuzhiyun 	if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2809*4882a593Smuzhiyun 		kfree_rcu(old_name, u.head);
2810*4882a593Smuzhiyun }
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun /*
2813*4882a593Smuzhiyun  * __d_move - move a dentry
2814*4882a593Smuzhiyun  * @dentry: entry to move
2815*4882a593Smuzhiyun  * @target: new dentry
2816*4882a593Smuzhiyun  * @exchange: exchange the two dentries
2817*4882a593Smuzhiyun  *
2818*4882a593Smuzhiyun  * Update the dcache to reflect the move of a file name. Negative
2819*4882a593Smuzhiyun  * dcache entries should not be moved in this way. Caller must hold
2820*4882a593Smuzhiyun  * rename_lock, the i_mutex of the source and target directories,
2821*4882a593Smuzhiyun  * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
2822*4882a593Smuzhiyun  */
__d_move(struct dentry * dentry,struct dentry * target,bool exchange)2823*4882a593Smuzhiyun static void __d_move(struct dentry *dentry, struct dentry *target,
2824*4882a593Smuzhiyun 		     bool exchange)
2825*4882a593Smuzhiyun {
2826*4882a593Smuzhiyun 	struct dentry *old_parent, *p;
2827*4882a593Smuzhiyun 	struct inode *dir = NULL;
2828*4882a593Smuzhiyun 	unsigned n;
2829*4882a593Smuzhiyun 
2830*4882a593Smuzhiyun 	WARN_ON(!dentry->d_inode);
2831*4882a593Smuzhiyun 	if (WARN_ON(dentry == target))
2832*4882a593Smuzhiyun 		return;
2833*4882a593Smuzhiyun 
2834*4882a593Smuzhiyun 	BUG_ON(d_ancestor(target, dentry));
2835*4882a593Smuzhiyun 	old_parent = dentry->d_parent;
2836*4882a593Smuzhiyun 	p = d_ancestor(old_parent, target);
2837*4882a593Smuzhiyun 	if (IS_ROOT(dentry)) {
2838*4882a593Smuzhiyun 		BUG_ON(p);
2839*4882a593Smuzhiyun 		spin_lock(&target->d_parent->d_lock);
2840*4882a593Smuzhiyun 	} else if (!p) {
2841*4882a593Smuzhiyun 		/* target is not a descendent of dentry->d_parent */
2842*4882a593Smuzhiyun 		spin_lock(&target->d_parent->d_lock);
2843*4882a593Smuzhiyun 		spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
2844*4882a593Smuzhiyun 	} else {
2845*4882a593Smuzhiyun 		BUG_ON(p == dentry);
2846*4882a593Smuzhiyun 		spin_lock(&old_parent->d_lock);
2847*4882a593Smuzhiyun 		if (p != target)
2848*4882a593Smuzhiyun 			spin_lock_nested(&target->d_parent->d_lock,
2849*4882a593Smuzhiyun 					DENTRY_D_LOCK_NESTED);
2850*4882a593Smuzhiyun 	}
2851*4882a593Smuzhiyun 	spin_lock_nested(&dentry->d_lock, 2);
2852*4882a593Smuzhiyun 	spin_lock_nested(&target->d_lock, 3);
2853*4882a593Smuzhiyun 
2854*4882a593Smuzhiyun 	if (unlikely(d_in_lookup(target))) {
2855*4882a593Smuzhiyun 		dir = target->d_parent->d_inode;
2856*4882a593Smuzhiyun 		n = start_dir_add(dir);
2857*4882a593Smuzhiyun 		__d_lookup_done(target);
2858*4882a593Smuzhiyun 	}
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	write_seqcount_begin(&dentry->d_seq);
2861*4882a593Smuzhiyun 	write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2862*4882a593Smuzhiyun 
2863*4882a593Smuzhiyun 	/* unhash both */
2864*4882a593Smuzhiyun 	if (!d_unhashed(dentry))
2865*4882a593Smuzhiyun 		___d_drop(dentry);
2866*4882a593Smuzhiyun 	if (!d_unhashed(target))
2867*4882a593Smuzhiyun 		___d_drop(target);
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun 	/* ... and switch them in the tree */
2870*4882a593Smuzhiyun 	dentry->d_parent = target->d_parent;
2871*4882a593Smuzhiyun 	if (!exchange) {
2872*4882a593Smuzhiyun 		copy_name(dentry, target);
2873*4882a593Smuzhiyun 		target->d_hash.pprev = NULL;
2874*4882a593Smuzhiyun 		dentry->d_parent->d_lockref.count++;
2875*4882a593Smuzhiyun 		if (dentry != old_parent) /* wasn't IS_ROOT */
2876*4882a593Smuzhiyun 			WARN_ON(!--old_parent->d_lockref.count);
2877*4882a593Smuzhiyun 	} else {
2878*4882a593Smuzhiyun 		target->d_parent = old_parent;
2879*4882a593Smuzhiyun 		swap_names(dentry, target);
2880*4882a593Smuzhiyun 		list_move(&target->d_child, &target->d_parent->d_subdirs);
2881*4882a593Smuzhiyun 		__d_rehash(target);
2882*4882a593Smuzhiyun 		fsnotify_update_flags(target);
2883*4882a593Smuzhiyun 	}
2884*4882a593Smuzhiyun 	list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
2885*4882a593Smuzhiyun 	__d_rehash(dentry);
2886*4882a593Smuzhiyun 	fsnotify_update_flags(dentry);
2887*4882a593Smuzhiyun 	fscrypt_handle_d_move(dentry);
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	write_seqcount_end(&target->d_seq);
2890*4882a593Smuzhiyun 	write_seqcount_end(&dentry->d_seq);
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	if (dir)
2893*4882a593Smuzhiyun 		end_dir_add(dir, n);
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun 	if (dentry->d_parent != old_parent)
2896*4882a593Smuzhiyun 		spin_unlock(&dentry->d_parent->d_lock);
2897*4882a593Smuzhiyun 	if (dentry != old_parent)
2898*4882a593Smuzhiyun 		spin_unlock(&old_parent->d_lock);
2899*4882a593Smuzhiyun 	spin_unlock(&target->d_lock);
2900*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
2901*4882a593Smuzhiyun }
2902*4882a593Smuzhiyun 
2903*4882a593Smuzhiyun /*
2904*4882a593Smuzhiyun  * d_move - move a dentry
2905*4882a593Smuzhiyun  * @dentry: entry to move
2906*4882a593Smuzhiyun  * @target: new dentry
2907*4882a593Smuzhiyun  *
2908*4882a593Smuzhiyun  * Update the dcache to reflect the move of a file name. Negative
2909*4882a593Smuzhiyun  * dcache entries should not be moved in this way. See the locking
2910*4882a593Smuzhiyun  * requirements for __d_move.
2911*4882a593Smuzhiyun  */
d_move(struct dentry * dentry,struct dentry * target)2912*4882a593Smuzhiyun void d_move(struct dentry *dentry, struct dentry *target)
2913*4882a593Smuzhiyun {
2914*4882a593Smuzhiyun 	write_seqlock(&rename_lock);
2915*4882a593Smuzhiyun 	__d_move(dentry, target, false);
2916*4882a593Smuzhiyun 	write_sequnlock(&rename_lock);
2917*4882a593Smuzhiyun }
2918*4882a593Smuzhiyun EXPORT_SYMBOL(d_move);
2919*4882a593Smuzhiyun 
2920*4882a593Smuzhiyun /*
2921*4882a593Smuzhiyun  * d_exchange - exchange two dentries
2922*4882a593Smuzhiyun  * @dentry1: first dentry
2923*4882a593Smuzhiyun  * @dentry2: second dentry
2924*4882a593Smuzhiyun  */
d_exchange(struct dentry * dentry1,struct dentry * dentry2)2925*4882a593Smuzhiyun void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2926*4882a593Smuzhiyun {
2927*4882a593Smuzhiyun 	write_seqlock(&rename_lock);
2928*4882a593Smuzhiyun 
2929*4882a593Smuzhiyun 	WARN_ON(!dentry1->d_inode);
2930*4882a593Smuzhiyun 	WARN_ON(!dentry2->d_inode);
2931*4882a593Smuzhiyun 	WARN_ON(IS_ROOT(dentry1));
2932*4882a593Smuzhiyun 	WARN_ON(IS_ROOT(dentry2));
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 	__d_move(dentry1, dentry2, true);
2935*4882a593Smuzhiyun 
2936*4882a593Smuzhiyun 	write_sequnlock(&rename_lock);
2937*4882a593Smuzhiyun }
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun /**
2940*4882a593Smuzhiyun  * d_ancestor - search for an ancestor
2941*4882a593Smuzhiyun  * @p1: ancestor dentry
2942*4882a593Smuzhiyun  * @p2: child dentry
2943*4882a593Smuzhiyun  *
2944*4882a593Smuzhiyun  * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2945*4882a593Smuzhiyun  * an ancestor of p2, else NULL.
2946*4882a593Smuzhiyun  */
d_ancestor(struct dentry * p1,struct dentry * p2)2947*4882a593Smuzhiyun struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2948*4882a593Smuzhiyun {
2949*4882a593Smuzhiyun 	struct dentry *p;
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun 	for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2952*4882a593Smuzhiyun 		if (p->d_parent == p1)
2953*4882a593Smuzhiyun 			return p;
2954*4882a593Smuzhiyun 	}
2955*4882a593Smuzhiyun 	return NULL;
2956*4882a593Smuzhiyun }
2957*4882a593Smuzhiyun 
2958*4882a593Smuzhiyun /*
2959*4882a593Smuzhiyun  * This helper attempts to cope with remotely renamed directories
2960*4882a593Smuzhiyun  *
2961*4882a593Smuzhiyun  * It assumes that the caller is already holding
2962*4882a593Smuzhiyun  * dentry->d_parent->d_inode->i_mutex, and rename_lock
2963*4882a593Smuzhiyun  *
2964*4882a593Smuzhiyun  * Note: If ever the locking in lock_rename() changes, then please
2965*4882a593Smuzhiyun  * remember to update this too...
2966*4882a593Smuzhiyun  */
__d_unalias(struct inode * inode,struct dentry * dentry,struct dentry * alias)2967*4882a593Smuzhiyun static int __d_unalias(struct inode *inode,
2968*4882a593Smuzhiyun 		struct dentry *dentry, struct dentry *alias)
2969*4882a593Smuzhiyun {
2970*4882a593Smuzhiyun 	struct mutex *m1 = NULL;
2971*4882a593Smuzhiyun 	struct rw_semaphore *m2 = NULL;
2972*4882a593Smuzhiyun 	int ret = -ESTALE;
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun 	/* If alias and dentry share a parent, then no extra locks required */
2975*4882a593Smuzhiyun 	if (alias->d_parent == dentry->d_parent)
2976*4882a593Smuzhiyun 		goto out_unalias;
2977*4882a593Smuzhiyun 
2978*4882a593Smuzhiyun 	/* See lock_rename() */
2979*4882a593Smuzhiyun 	if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2980*4882a593Smuzhiyun 		goto out_err;
2981*4882a593Smuzhiyun 	m1 = &dentry->d_sb->s_vfs_rename_mutex;
2982*4882a593Smuzhiyun 	if (!inode_trylock_shared(alias->d_parent->d_inode))
2983*4882a593Smuzhiyun 		goto out_err;
2984*4882a593Smuzhiyun 	m2 = &alias->d_parent->d_inode->i_rwsem;
2985*4882a593Smuzhiyun out_unalias:
2986*4882a593Smuzhiyun 	__d_move(alias, dentry, false);
2987*4882a593Smuzhiyun 	ret = 0;
2988*4882a593Smuzhiyun out_err:
2989*4882a593Smuzhiyun 	if (m2)
2990*4882a593Smuzhiyun 		up_read(m2);
2991*4882a593Smuzhiyun 	if (m1)
2992*4882a593Smuzhiyun 		mutex_unlock(m1);
2993*4882a593Smuzhiyun 	return ret;
2994*4882a593Smuzhiyun }
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun /**
2997*4882a593Smuzhiyun  * d_splice_alias - splice a disconnected dentry into the tree if one exists
2998*4882a593Smuzhiyun  * @inode:  the inode which may have a disconnected dentry
2999*4882a593Smuzhiyun  * @dentry: a negative dentry which we want to point to the inode.
3000*4882a593Smuzhiyun  *
3001*4882a593Smuzhiyun  * If inode is a directory and has an IS_ROOT alias, then d_move that in
3002*4882a593Smuzhiyun  * place of the given dentry and return it, else simply d_add the inode
3003*4882a593Smuzhiyun  * to the dentry and return NULL.
3004*4882a593Smuzhiyun  *
3005*4882a593Smuzhiyun  * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
3006*4882a593Smuzhiyun  * we should error out: directories can't have multiple aliases.
3007*4882a593Smuzhiyun  *
3008*4882a593Smuzhiyun  * This is needed in the lookup routine of any filesystem that is exportable
3009*4882a593Smuzhiyun  * (via knfsd) so that we can build dcache paths to directories effectively.
3010*4882a593Smuzhiyun  *
3011*4882a593Smuzhiyun  * If a dentry was found and moved, then it is returned.  Otherwise NULL
3012*4882a593Smuzhiyun  * is returned.  This matches the expected return value of ->lookup.
3013*4882a593Smuzhiyun  *
3014*4882a593Smuzhiyun  * Cluster filesystems may call this function with a negative, hashed dentry.
3015*4882a593Smuzhiyun  * In that case, we know that the inode will be a regular file, and also this
3016*4882a593Smuzhiyun  * will only occur during atomic_open. So we need to check for the dentry
3017*4882a593Smuzhiyun  * being already hashed only in the final case.
3018*4882a593Smuzhiyun  */
d_splice_alias(struct inode * inode,struct dentry * dentry)3019*4882a593Smuzhiyun struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3020*4882a593Smuzhiyun {
3021*4882a593Smuzhiyun 	if (IS_ERR(inode))
3022*4882a593Smuzhiyun 		return ERR_CAST(inode);
3023*4882a593Smuzhiyun 
3024*4882a593Smuzhiyun 	BUG_ON(!d_unhashed(dentry));
3025*4882a593Smuzhiyun 
3026*4882a593Smuzhiyun 	if (!inode)
3027*4882a593Smuzhiyun 		goto out;
3028*4882a593Smuzhiyun 
3029*4882a593Smuzhiyun 	security_d_instantiate(dentry, inode);
3030*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
3031*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode)) {
3032*4882a593Smuzhiyun 		struct dentry *new = __d_find_any_alias(inode);
3033*4882a593Smuzhiyun 		if (unlikely(new)) {
3034*4882a593Smuzhiyun 			/* The reference to new ensures it remains an alias */
3035*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
3036*4882a593Smuzhiyun 			write_seqlock(&rename_lock);
3037*4882a593Smuzhiyun 			if (unlikely(d_ancestor(new, dentry))) {
3038*4882a593Smuzhiyun 				write_sequnlock(&rename_lock);
3039*4882a593Smuzhiyun 				dput(new);
3040*4882a593Smuzhiyun 				new = ERR_PTR(-ELOOP);
3041*4882a593Smuzhiyun 				pr_warn_ratelimited(
3042*4882a593Smuzhiyun 					"VFS: Lookup of '%s' in %s %s"
3043*4882a593Smuzhiyun 					" would have caused loop\n",
3044*4882a593Smuzhiyun 					dentry->d_name.name,
3045*4882a593Smuzhiyun 					inode->i_sb->s_type->name,
3046*4882a593Smuzhiyun 					inode->i_sb->s_id);
3047*4882a593Smuzhiyun 			} else if (!IS_ROOT(new)) {
3048*4882a593Smuzhiyun 				struct dentry *old_parent = dget(new->d_parent);
3049*4882a593Smuzhiyun 				int err = __d_unalias(inode, dentry, new);
3050*4882a593Smuzhiyun 				write_sequnlock(&rename_lock);
3051*4882a593Smuzhiyun 				if (err) {
3052*4882a593Smuzhiyun 					dput(new);
3053*4882a593Smuzhiyun 					new = ERR_PTR(err);
3054*4882a593Smuzhiyun 				}
3055*4882a593Smuzhiyun 				dput(old_parent);
3056*4882a593Smuzhiyun 			} else {
3057*4882a593Smuzhiyun 				__d_move(new, dentry, false);
3058*4882a593Smuzhiyun 				write_sequnlock(&rename_lock);
3059*4882a593Smuzhiyun 			}
3060*4882a593Smuzhiyun 			iput(inode);
3061*4882a593Smuzhiyun 			return new;
3062*4882a593Smuzhiyun 		}
3063*4882a593Smuzhiyun 	}
3064*4882a593Smuzhiyun out:
3065*4882a593Smuzhiyun 	__d_add(dentry, inode);
3066*4882a593Smuzhiyun 	return NULL;
3067*4882a593Smuzhiyun }
3068*4882a593Smuzhiyun EXPORT_SYMBOL_NS(d_splice_alias, ANDROID_GKI_VFS_EXPORT_ONLY);
3069*4882a593Smuzhiyun 
3070*4882a593Smuzhiyun /*
3071*4882a593Smuzhiyun  * Test whether new_dentry is a subdirectory of old_dentry.
3072*4882a593Smuzhiyun  *
3073*4882a593Smuzhiyun  * Trivially implemented using the dcache structure
3074*4882a593Smuzhiyun  */
3075*4882a593Smuzhiyun 
3076*4882a593Smuzhiyun /**
3077*4882a593Smuzhiyun  * is_subdir - is new dentry a subdirectory of old_dentry
3078*4882a593Smuzhiyun  * @new_dentry: new dentry
3079*4882a593Smuzhiyun  * @old_dentry: old dentry
3080*4882a593Smuzhiyun  *
3081*4882a593Smuzhiyun  * Returns true if new_dentry is a subdirectory of the parent (at any depth).
3082*4882a593Smuzhiyun  * Returns false otherwise.
3083*4882a593Smuzhiyun  * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3084*4882a593Smuzhiyun  */
3085*4882a593Smuzhiyun 
is_subdir(struct dentry * new_dentry,struct dentry * old_dentry)3086*4882a593Smuzhiyun bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3087*4882a593Smuzhiyun {
3088*4882a593Smuzhiyun 	bool result;
3089*4882a593Smuzhiyun 	unsigned seq;
3090*4882a593Smuzhiyun 
3091*4882a593Smuzhiyun 	if (new_dentry == old_dentry)
3092*4882a593Smuzhiyun 		return true;
3093*4882a593Smuzhiyun 
3094*4882a593Smuzhiyun 	do {
3095*4882a593Smuzhiyun 		/* for restarting inner loop in case of seq retry */
3096*4882a593Smuzhiyun 		seq = read_seqbegin(&rename_lock);
3097*4882a593Smuzhiyun 		/*
3098*4882a593Smuzhiyun 		 * Need rcu_readlock to protect against the d_parent trashing
3099*4882a593Smuzhiyun 		 * due to d_move
3100*4882a593Smuzhiyun 		 */
3101*4882a593Smuzhiyun 		rcu_read_lock();
3102*4882a593Smuzhiyun 		if (d_ancestor(old_dentry, new_dentry))
3103*4882a593Smuzhiyun 			result = true;
3104*4882a593Smuzhiyun 		else
3105*4882a593Smuzhiyun 			result = false;
3106*4882a593Smuzhiyun 		rcu_read_unlock();
3107*4882a593Smuzhiyun 	} while (read_seqretry(&rename_lock, seq));
3108*4882a593Smuzhiyun 
3109*4882a593Smuzhiyun 	return result;
3110*4882a593Smuzhiyun }
3111*4882a593Smuzhiyun EXPORT_SYMBOL(is_subdir);
3112*4882a593Smuzhiyun 
d_genocide_kill(void * data,struct dentry * dentry)3113*4882a593Smuzhiyun static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3114*4882a593Smuzhiyun {
3115*4882a593Smuzhiyun 	struct dentry *root = data;
3116*4882a593Smuzhiyun 	if (dentry != root) {
3117*4882a593Smuzhiyun 		if (d_unhashed(dentry) || !dentry->d_inode)
3118*4882a593Smuzhiyun 			return D_WALK_SKIP;
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 		if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3121*4882a593Smuzhiyun 			dentry->d_flags |= DCACHE_GENOCIDE;
3122*4882a593Smuzhiyun 			dentry->d_lockref.count--;
3123*4882a593Smuzhiyun 		}
3124*4882a593Smuzhiyun 	}
3125*4882a593Smuzhiyun 	return D_WALK_CONTINUE;
3126*4882a593Smuzhiyun }
3127*4882a593Smuzhiyun 
d_genocide(struct dentry * parent)3128*4882a593Smuzhiyun void d_genocide(struct dentry *parent)
3129*4882a593Smuzhiyun {
3130*4882a593Smuzhiyun 	d_walk(parent, parent, d_genocide_kill);
3131*4882a593Smuzhiyun }
3132*4882a593Smuzhiyun 
3133*4882a593Smuzhiyun EXPORT_SYMBOL(d_genocide);
3134*4882a593Smuzhiyun 
d_tmpfile(struct dentry * dentry,struct inode * inode)3135*4882a593Smuzhiyun void d_tmpfile(struct dentry *dentry, struct inode *inode)
3136*4882a593Smuzhiyun {
3137*4882a593Smuzhiyun 	inode_dec_link_count(inode);
3138*4882a593Smuzhiyun 	BUG_ON(dentry->d_name.name != dentry->d_iname ||
3139*4882a593Smuzhiyun 		!hlist_unhashed(&dentry->d_u.d_alias) ||
3140*4882a593Smuzhiyun 		!d_unlinked(dentry));
3141*4882a593Smuzhiyun 	spin_lock(&dentry->d_parent->d_lock);
3142*4882a593Smuzhiyun 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3143*4882a593Smuzhiyun 	dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3144*4882a593Smuzhiyun 				(unsigned long long)inode->i_ino);
3145*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
3146*4882a593Smuzhiyun 	spin_unlock(&dentry->d_parent->d_lock);
3147*4882a593Smuzhiyun 	d_instantiate(dentry, inode);
3148*4882a593Smuzhiyun }
3149*4882a593Smuzhiyun EXPORT_SYMBOL(d_tmpfile);
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun static __initdata unsigned long dhash_entries;
set_dhash_entries(char * str)3152*4882a593Smuzhiyun static int __init set_dhash_entries(char *str)
3153*4882a593Smuzhiyun {
3154*4882a593Smuzhiyun 	if (!str)
3155*4882a593Smuzhiyun 		return 0;
3156*4882a593Smuzhiyun 	dhash_entries = simple_strtoul(str, &str, 0);
3157*4882a593Smuzhiyun 	return 1;
3158*4882a593Smuzhiyun }
3159*4882a593Smuzhiyun __setup("dhash_entries=", set_dhash_entries);
3160*4882a593Smuzhiyun 
dcache_init_early(void)3161*4882a593Smuzhiyun static void __init dcache_init_early(void)
3162*4882a593Smuzhiyun {
3163*4882a593Smuzhiyun 	/* If hashes are distributed across NUMA nodes, defer
3164*4882a593Smuzhiyun 	 * hash allocation until vmalloc space is available.
3165*4882a593Smuzhiyun 	 */
3166*4882a593Smuzhiyun 	if (hashdist)
3167*4882a593Smuzhiyun 		return;
3168*4882a593Smuzhiyun 
3169*4882a593Smuzhiyun 	dentry_hashtable =
3170*4882a593Smuzhiyun 		alloc_large_system_hash("Dentry cache",
3171*4882a593Smuzhiyun 					sizeof(struct hlist_bl_head),
3172*4882a593Smuzhiyun 					dhash_entries,
3173*4882a593Smuzhiyun 					13,
3174*4882a593Smuzhiyun 					HASH_EARLY | HASH_ZERO,
3175*4882a593Smuzhiyun 					&d_hash_shift,
3176*4882a593Smuzhiyun 					NULL,
3177*4882a593Smuzhiyun 					0,
3178*4882a593Smuzhiyun 					0);
3179*4882a593Smuzhiyun 	d_hash_shift = 32 - d_hash_shift;
3180*4882a593Smuzhiyun }
3181*4882a593Smuzhiyun 
dcache_init(void)3182*4882a593Smuzhiyun static void __init dcache_init(void)
3183*4882a593Smuzhiyun {
3184*4882a593Smuzhiyun 	/*
3185*4882a593Smuzhiyun 	 * A constructor could be added for stable state like the lists,
3186*4882a593Smuzhiyun 	 * but it is probably not worth it because of the cache nature
3187*4882a593Smuzhiyun 	 * of the dcache.
3188*4882a593Smuzhiyun 	 */
3189*4882a593Smuzhiyun 	dentry_cache = KMEM_CACHE_USERCOPY(dentry,
3190*4882a593Smuzhiyun 		SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
3191*4882a593Smuzhiyun 		d_iname);
3192*4882a593Smuzhiyun 
3193*4882a593Smuzhiyun 	/* Hash may have been set up in dcache_init_early */
3194*4882a593Smuzhiyun 	if (!hashdist)
3195*4882a593Smuzhiyun 		return;
3196*4882a593Smuzhiyun 
3197*4882a593Smuzhiyun 	dentry_hashtable =
3198*4882a593Smuzhiyun 		alloc_large_system_hash("Dentry cache",
3199*4882a593Smuzhiyun 					sizeof(struct hlist_bl_head),
3200*4882a593Smuzhiyun 					dhash_entries,
3201*4882a593Smuzhiyun 					13,
3202*4882a593Smuzhiyun 					HASH_ZERO,
3203*4882a593Smuzhiyun 					&d_hash_shift,
3204*4882a593Smuzhiyun 					NULL,
3205*4882a593Smuzhiyun 					0,
3206*4882a593Smuzhiyun 					0);
3207*4882a593Smuzhiyun 	d_hash_shift = 32 - d_hash_shift;
3208*4882a593Smuzhiyun }
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun /* SLAB cache for __getname() consumers */
3211*4882a593Smuzhiyun struct kmem_cache *names_cachep __read_mostly;
3212*4882a593Smuzhiyun EXPORT_SYMBOL(names_cachep);
3213*4882a593Smuzhiyun 
vfs_caches_init_early(void)3214*4882a593Smuzhiyun void __init vfs_caches_init_early(void)
3215*4882a593Smuzhiyun {
3216*4882a593Smuzhiyun 	int i;
3217*4882a593Smuzhiyun 
3218*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
3219*4882a593Smuzhiyun 		INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
3220*4882a593Smuzhiyun 
3221*4882a593Smuzhiyun 	dcache_init_early();
3222*4882a593Smuzhiyun 	inode_init_early();
3223*4882a593Smuzhiyun }
3224*4882a593Smuzhiyun 
vfs_caches_init(void)3225*4882a593Smuzhiyun void __init vfs_caches_init(void)
3226*4882a593Smuzhiyun {
3227*4882a593Smuzhiyun 	names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
3228*4882a593Smuzhiyun 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
3229*4882a593Smuzhiyun 
3230*4882a593Smuzhiyun 	dcache_init();
3231*4882a593Smuzhiyun 	inode_init();
3232*4882a593Smuzhiyun 	files_init();
3233*4882a593Smuzhiyun 	files_maxfiles_init();
3234*4882a593Smuzhiyun 	mnt_init();
3235*4882a593Smuzhiyun 	bdev_cache_init();
3236*4882a593Smuzhiyun 	chrdev_init();
3237*4882a593Smuzhiyun }
3238