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