blob: 525770e576db0db8085f5c3c2220095008cb2629 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110036#include <linux/bit_spinlock.h>
37#include <linux/rculist_bl.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070038#include <linux/prefetch.h>
David Howellsdd179942011-08-16 15:31:30 +010039#include <linux/ratelimit.h>
Dave Chinnerf6041562013-08-28 10:18:00 +100040#include <linux/list_lru.h>
David Howells07f3f052006-09-30 20:52:18 +020041#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050042#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Nick Piggin789680d2011-01-07 17:49:30 +110044/*
45 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110046 * dcache->d_inode->i_lock protects:
47 * - i_dentry, d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110048 * dcache_hash_bucket lock protects:
49 * - the dcache hash table
50 * s_anon bl list spinlock protects:
51 * - the s_anon list (see __d_drop)
Dave Chinner19156842013-08-28 10:17:55 +100052 * dentry->d_sb->s_dentry_lru_lock protects:
Nick Piggin23044502011-01-07 17:49:31 +110053 * - the dcache lru lists and counters
54 * d_lock protects:
55 * - d_flags
56 * - d_name
57 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110058 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110059 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110060 * - d_parent and d_subdirs
61 * - childrens' d_child and d_parent
Nick Pigginb23fb0a2011-01-07 17:49:35 +110062 * - d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110063 *
64 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110065 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110066 * dentry->d_lock
Dave Chinner19156842013-08-28 10:17:55 +100067 * dentry->d_sb->s_dentry_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110068 * dcache_hash_bucket lock
69 * s_anon lock
Nick Piggin789680d2011-01-07 17:49:30 +110070 *
Nick Pigginda502952011-01-07 17:49:33 +110071 * If there is an ancestor relationship:
72 * dentry->d_parent->...->d_parent->d_lock
73 * ...
74 * dentry->d_parent->d_lock
75 * dentry->d_lock
76 *
77 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110078 * if (dentry1 < dentry2)
79 * dentry1->d_lock
80 * dentry2->d_lock
81 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080082int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
84
Al Viro74c3cbe2007-07-22 08:04:18 -040085__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Nick Piggin949854d2011-01-07 17:49:37 +110087EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Lametere18b8902006-12-06 20:33:20 -080089static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Waiman Long232d2d62013-09-09 12:18:13 -040091/**
92 * read_seqbegin_or_lock - begin a sequence number check or locking block
Waiman Long18129972013-09-12 10:55:35 -040093 * @lock: sequence lock
94 * @seq : sequence number to be checked
Waiman Long232d2d62013-09-09 12:18:13 -040095 *
96 * First try it once optimistically without taking the lock. If that fails,
97 * take the lock. The sequence number is also used as a marker for deciding
98 * whether to be a reader (even) or writer (odd).
99 * N.B. seq must be initialized to an even number to begin with.
100 */
101static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
102{
Al Viro48f5ec22013-09-09 15:22:25 -0400103 if (!(*seq & 1)) /* Even */
Waiman Long232d2d62013-09-09 12:18:13 -0400104 *seq = read_seqbegin(lock);
Al Viro48f5ec22013-09-09 15:22:25 -0400105 else /* Odd */
Waiman Long18129972013-09-12 10:55:35 -0400106 read_seqlock_excl(lock);
Waiman Long232d2d62013-09-09 12:18:13 -0400107}
108
Al Viro48f5ec22013-09-09 15:22:25 -0400109static inline int need_seqretry(seqlock_t *lock, int seq)
Waiman Long232d2d62013-09-09 12:18:13 -0400110{
Al Viro48f5ec22013-09-09 15:22:25 -0400111 return !(seq & 1) && read_seqretry(lock, seq);
112}
113
114static inline void done_seqretry(seqlock_t *lock, int seq)
115{
116 if (seq & 1)
Waiman Long18129972013-09-12 10:55:35 -0400117 read_sequnlock_excl(lock);
Waiman Long232d2d62013-09-09 12:18:13 -0400118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * This is the single most critical data structure when it comes
122 * to the dcache: the hashtable for lookups. Somebody should try
123 * to make this good - I've just made it work.
124 *
125 * This hash-function tries to avoid losing too many bits of hash
126 * information, yet avoid using a prime hash-size or similar.
127 */
128#define D_HASHBITS d_hash_shift
129#define D_HASHMASK d_hash_mask
130
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800131static unsigned int d_hash_mask __read_mostly;
132static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100133
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700134static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100135
Linus Torvalds8966be92012-03-02 14:23:30 -0800136static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700137 unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100138{
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700139 hash += (unsigned long) parent / L1_CACHE_BYTES;
140 hash = hash + (hash >> D_HASHBITS);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100141 return dentry_hashtable + (hash & D_HASHMASK);
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Statistics gathering. */
145struct dentry_stat_t dentry_stat = {
146 .age_limit = 45,
147};
148
Glauber Costa3942c072013-08-28 10:17:53 +1000149static DEFINE_PER_CPU(long, nr_dentry);
Dave Chinner62d36c72013-08-28 10:17:54 +1000150static DEFINE_PER_CPU(long, nr_dentry_unused);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400151
152#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Dave Chinner62d36c72013-08-28 10:17:54 +1000153
154/*
155 * Here we resort to our own counters instead of using generic per-cpu counters
156 * for consistency with what the vfs inode code does. We are expected to harvest
157 * better code and performance by having our own specialized counters.
158 *
159 * Please note that the loop is done over all possible CPUs, not over all online
160 * CPUs. The reason for this is that we don't want to play games with CPUs going
161 * on and off. If one of them goes off, we will just keep their counters.
162 *
163 * glommer: See cffbc8a for details, and if you ever intend to change this,
164 * please update all vfs counters to match.
165 */
Glauber Costa3942c072013-08-28 10:17:53 +1000166static long get_nr_dentry(void)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100167{
168 int i;
Glauber Costa3942c072013-08-28 10:17:53 +1000169 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100170 for_each_possible_cpu(i)
171 sum += per_cpu(nr_dentry, i);
172 return sum < 0 ? 0 : sum;
173}
174
Dave Chinner62d36c72013-08-28 10:17:54 +1000175static long get_nr_dentry_unused(void)
176{
177 int i;
178 long sum = 0;
179 for_each_possible_cpu(i)
180 sum += per_cpu(nr_dentry_unused, i);
181 return sum < 0 ? 0 : sum;
182}
183
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400184int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
185 size_t *lenp, loff_t *ppos)
186{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100187 dentry_stat.nr_dentry = get_nr_dentry();
Dave Chinner62d36c72013-08-28 10:17:54 +1000188 dentry_stat.nr_unused = get_nr_dentry_unused();
Glauber Costa3942c072013-08-28 10:17:53 +1000189 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400190}
191#endif
192
Linus Torvalds5483f182012-03-04 15:51:42 -0800193/*
194 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
195 * The strings are both count bytes long, and count is non-zero.
196 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700197#ifdef CONFIG_DCACHE_WORD_ACCESS
198
199#include <asm/word-at-a-time.h>
200/*
201 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
202 * aligned allocation for this particular component. We don't
203 * strictly need the load_unaligned_zeropad() safety, but it
204 * doesn't hurt either.
205 *
206 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
207 * need the careful unaligned handling.
208 */
Linus Torvalds94753db52012-05-10 12:19:19 -0700209static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800210{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800211 unsigned long a,b,mask;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800212
213 for (;;) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -0700214 a = *(unsigned long *)cs;
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700215 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800216 if (tcount < sizeof(unsigned long))
217 break;
218 if (unlikely(a != b))
219 return 1;
220 cs += sizeof(unsigned long);
221 ct += sizeof(unsigned long);
222 tcount -= sizeof(unsigned long);
223 if (!tcount)
224 return 0;
225 }
226 mask = ~(~0ul << tcount*8);
227 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700228}
229
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800230#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700231
Linus Torvalds94753db52012-05-10 12:19:19 -0700232static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700233{
Linus Torvalds5483f182012-03-04 15:51:42 -0800234 do {
235 if (*cs != *ct)
236 return 1;
237 cs++;
238 ct++;
239 tcount--;
240 } while (tcount);
241 return 0;
242}
243
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700244#endif
245
Linus Torvalds94753db52012-05-10 12:19:19 -0700246static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
247{
Linus Torvalds6326c712012-05-21 16:14:04 -0700248 const unsigned char *cs;
Linus Torvalds94753db52012-05-10 12:19:19 -0700249 /*
250 * Be careful about RCU walk racing with rename:
251 * use ACCESS_ONCE to fetch the name pointer.
252 *
253 * NOTE! Even if a rename will mean that the length
254 * was not loaded atomically, we don't care. The
255 * RCU walk will check the sequence count eventually,
256 * and catch it. And we won't overrun the buffer,
257 * because we're reading the name pointer atomically,
258 * and a dentry name is guaranteed to be properly
259 * terminated with a NUL byte.
260 *
261 * End result: even if 'len' is wrong, we'll exit
262 * early because the data cannot match (there can
263 * be no NUL in the ct/tcount data)
264 */
Linus Torvalds6326c712012-05-21 16:14:04 -0700265 cs = ACCESS_ONCE(dentry->d_name.name);
266 smp_read_barrier_depends();
267 return dentry_string_cmp(cs, ct, tcount);
Linus Torvalds94753db52012-05-10 12:19:19 -0700268}
269
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400270static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400272 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
273
Al Virob3d9b7a2012-06-09 13:51:19 -0400274 WARN_ON(!hlist_unhashed(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (dname_external(dentry))
276 kfree(dentry->d_name.name);
277 kmem_cache_free(dentry_cache, dentry);
278}
279
280/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100281 * no locks, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283static void d_free(struct dentry *dentry)
284{
Linus Torvalds0d984392013-09-08 13:46:52 -0700285 BUG_ON((int)dentry->d_lockref.count > 0);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100286 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (dentry->d_op && dentry->d_op->d_release)
288 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400289
Linus Torvaldsdea36672011-04-24 07:58:46 -0700290 /* if dentry was never visible to RCU, immediate free is OK */
291 if (!(dentry->d_flags & DCACHE_RCUACCESS))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400292 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800293 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400294 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Nick Piggin31e6b012011-01-07 17:49:52 +1100297/**
298 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800299 * @dentry: the target dentry
Nick Piggin31e6b012011-01-07 17:49:52 +1100300 * After this call, in-progress rcu-walk path lookup will fail. This
301 * should be called after unhashing, and after changing d_inode (if
302 * the dentry has not already been unhashed).
303 */
304static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
305{
306 assert_spin_locked(&dentry->d_lock);
307 /* Go through a barrier */
308 write_seqcount_barrier(&dentry->d_seq);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * Release the dentry's inode, using the filesystem
Nick Piggin31e6b012011-01-07 17:49:52 +1100313 * d_iput() operation if defined. Dentry has no refcount
314 * and is unhashed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800316static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200317 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100318 __releases(dentry->d_inode->i_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct inode *inode = dentry->d_inode;
321 if (inode) {
322 dentry->d_inode = NULL;
Al Virob3d9b7a2012-06-09 13:51:19 -0400323 hlist_del_init(&dentry->d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100325 spin_unlock(&inode->i_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700326 if (!inode->i_nlink)
327 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (dentry->d_op && dentry->d_op->d_iput)
329 dentry->d_op->d_iput(dentry, inode);
330 else
331 iput(inode);
332 } else {
333 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335}
336
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700337/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100338 * Release the dentry's inode, using the filesystem
339 * d_iput() operation if defined. dentry remains in-use.
340 */
341static void dentry_unlink_inode(struct dentry * dentry)
342 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100343 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100344{
345 struct inode *inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +0100346 __d_clear_type(dentry);
Nick Piggin31e6b012011-01-07 17:49:52 +1100347 dentry->d_inode = NULL;
Al Virob3d9b7a2012-06-09 13:51:19 -0400348 hlist_del_init(&dentry->d_alias);
Nick Piggin31e6b012011-01-07 17:49:52 +1100349 dentry_rcuwalk_barrier(dentry);
350 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100351 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100352 if (!inode->i_nlink)
353 fsnotify_inoderemove(inode);
354 if (dentry->d_op && dentry->d_op->d_iput)
355 dentry->d_op->d_iput(dentry, inode);
356 else
357 iput(inode);
358}
359
360/*
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400361 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
362 * is in use - which includes both the "real" per-superblock
363 * LRU list _and_ the DCACHE_SHRINK_LIST use.
364 *
365 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
366 * on the shrink list (ie not on the superblock LRU list).
367 *
368 * The per-cpu "nr_dentry_unused" counters are updated with
369 * the DCACHE_LRU_LIST bit.
370 *
371 * These helper functions make sure we always follow the
372 * rules. d_lock must be held by the caller.
373 */
374#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
375static void d_lru_add(struct dentry *dentry)
376{
377 D_FLAG_VERIFY(dentry, 0);
378 dentry->d_flags |= DCACHE_LRU_LIST;
379 this_cpu_inc(nr_dentry_unused);
380 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
381}
382
383static void d_lru_del(struct dentry *dentry)
384{
385 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
386 dentry->d_flags &= ~DCACHE_LRU_LIST;
387 this_cpu_dec(nr_dentry_unused);
388 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
389}
390
391static void d_shrink_del(struct dentry *dentry)
392{
393 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
394 list_del_init(&dentry->d_lru);
395 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
396 this_cpu_dec(nr_dentry_unused);
397}
398
399static void d_shrink_add(struct dentry *dentry, struct list_head *list)
400{
401 D_FLAG_VERIFY(dentry, 0);
402 list_add(&dentry->d_lru, list);
403 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
404 this_cpu_inc(nr_dentry_unused);
405}
406
407/*
408 * These can only be called under the global LRU lock, ie during the
409 * callback for freeing the LRU list. "isolate" removes it from the
410 * LRU lists entirely, while shrink_move moves it to the indicated
411 * private list.
412 */
413static void d_lru_isolate(struct dentry *dentry)
414{
415 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
416 dentry->d_flags &= ~DCACHE_LRU_LIST;
417 this_cpu_dec(nr_dentry_unused);
418 list_del_init(&dentry->d_lru);
419}
420
421static void d_lru_shrink_move(struct dentry *dentry, struct list_head *list)
422{
423 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
424 dentry->d_flags |= DCACHE_SHRINK_LIST;
425 list_move_tail(&dentry->d_lru, list);
426}
427
428/*
Dave Chinnerf6041562013-08-28 10:18:00 +1000429 * dentry_lru_(add|del)_list) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700430 */
431static void dentry_lru_add(struct dentry *dentry)
432{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400433 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
434 d_lru_add(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700435}
436
Sage Weilf0023bc2011-10-28 10:02:42 -0700437/*
438 * Remove a dentry with references from the LRU.
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000439 *
440 * If we are on the shrink list, then we can get to try_prune_one_dentry() and
441 * lose our last reference through the parent walk. In this case, we need to
442 * remove ourselves from the shrink list, not the LRU.
Sage Weilf0023bc2011-10-28 10:02:42 -0700443 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700444static void dentry_lru_del(struct dentry *dentry)
445{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400446 if (dentry->d_flags & DCACHE_LRU_LIST) {
447 if (dentry->d_flags & DCACHE_SHRINK_LIST)
448 return d_shrink_del(dentry);
449 d_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700450 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700451}
452
Miklos Szeredid52b9082007-05-08 00:23:46 -0700453/**
454 * d_kill - kill dentry and return parent
455 * @dentry: dentry to kill
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800456 * @parent: parent dentry
Miklos Szeredid52b9082007-05-08 00:23:46 -0700457 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200458 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700459 *
460 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100461 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100462 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
463 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700464 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100465static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200466 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100467 __releases(parent->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100468 __releases(dentry->d_inode->i_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700469{
Miklos Szeredid52b9082007-05-08 00:23:46 -0700470 list_del(&dentry->d_u.d_child);
Trond Myklebustc83ce982011-03-15 13:36:43 -0400471 /*
472 * Inform try_to_ascend() that we are no longer attached to the
473 * dentry tree
474 */
Miklos Szeredib161dfa62012-09-17 22:31:38 +0200475 dentry->d_flags |= DCACHE_DENTRY_KILLED;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100476 if (parent)
477 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700478 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100479 /*
480 * dentry_iput drops the locks, at which point nobody (except
481 * transient RCU lookups) can reach this dentry.
482 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700483 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900484 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700485}
486
Nick Piggin789680d2011-01-07 17:49:30 +1100487/**
488 * d_drop - drop a dentry
489 * @dentry: dentry to drop
490 *
491 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
492 * be found through a VFS lookup any more. Note that this is different from
493 * deleting the dentry - d_delete will try to mark the dentry negative if
494 * possible, giving a successful _negative_ lookup, while d_drop will
495 * just make the cache lookup fail.
496 *
497 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
498 * reason (NFS timeouts or autofs deletes).
499 *
500 * __d_drop requires dentry->d_lock.
501 */
502void __d_drop(struct dentry *dentry)
503{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700504 if (!d_unhashed(dentry)) {
Al Virob61625d2013-10-04 11:09:01 -0400505 struct hlist_bl_head *b;
506 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
507 b = &dentry->d_sb->s_anon;
508 else
509 b = d_hash(dentry->d_parent, dentry->d_name.hash);
510
511 hlist_bl_lock(b);
512 __hlist_bl_del(&dentry->d_hash);
513 dentry->d_hash.pprev = NULL;
514 hlist_bl_unlock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -0700515 dentry_rcuwalk_barrier(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +1100516 }
517}
518EXPORT_SYMBOL(__d_drop);
519
520void d_drop(struct dentry *dentry)
521{
Nick Piggin789680d2011-01-07 17:49:30 +1100522 spin_lock(&dentry->d_lock);
523 __d_drop(dentry);
524 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100525}
526EXPORT_SYMBOL(d_drop);
527
Nick Piggin77812a12011-01-07 17:49:48 +1100528/*
529 * Finish off a dentry we've decided to kill.
530 * dentry->d_lock must be held, returns with it unlocked.
531 * If ref is non-zero, then decrement the refcount too.
532 * Returns dentry requiring refcount drop, or NULL if we're done.
533 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000534static inline struct dentry *
535dentry_kill(struct dentry *dentry, int unlock_on_failure)
Nick Piggin77812a12011-01-07 17:49:48 +1100536 __releases(dentry->d_lock)
537{
Nick Piggin873feea2011-01-07 17:50:06 +1100538 struct inode *inode;
Nick Piggin77812a12011-01-07 17:49:48 +1100539 struct dentry *parent;
540
Nick Piggin873feea2011-01-07 17:50:06 +1100541 inode = dentry->d_inode;
542 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin77812a12011-01-07 17:49:48 +1100543relock:
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000544 if (unlock_on_failure) {
545 spin_unlock(&dentry->d_lock);
546 cpu_relax();
547 }
Nick Piggin77812a12011-01-07 17:49:48 +1100548 return dentry; /* try again with same dentry */
549 }
550 if (IS_ROOT(dentry))
551 parent = NULL;
552 else
553 parent = dentry->d_parent;
554 if (parent && !spin_trylock(&parent->d_lock)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100555 if (inode)
556 spin_unlock(&inode->i_lock);
Nick Piggin77812a12011-01-07 17:49:48 +1100557 goto relock;
558 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100559
Linus Torvalds0d984392013-09-08 13:46:52 -0700560 /*
561 * The dentry is now unrecoverably dead to the world.
562 */
563 lockref_mark_dead(&dentry->d_lockref);
564
Sage Weilf0023bc2011-10-28 10:02:42 -0700565 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700566 * inform the fs via d_prune that this dentry is about to be
567 * unhashed and destroyed.
568 */
Yan, Zheng590fb512013-08-13 15:42:02 +0800569 if ((dentry->d_flags & DCACHE_OP_PRUNE) && !d_unhashed(dentry))
Yan, Zheng61572bb2013-04-15 14:13:21 +0800570 dentry->d_op->d_prune(dentry);
571
572 dentry_lru_del(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100573 /* if it was on the hash then remove it */
574 __d_drop(dentry);
575 return d_kill(dentry, parent);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * This is dput
580 *
581 * This is complicated by the fact that we do not want to put
582 * dentries that are no longer on any hash chain on the unused
583 * list: we'd much rather just get rid of them immediately.
584 *
585 * However, that implies that we have to traverse the dentry
586 * tree upwards to the parents which might _also_ now be
587 * scheduled for deletion (it may have been only waiting for
588 * its last child to go away).
589 *
590 * This tail recursion is done by hand as we don't want to depend
591 * on the compiler to always get this right (gcc generally doesn't).
592 * Real recursion would eat up our stack space.
593 */
594
595/*
596 * dput - release a dentry
597 * @dentry: dentry to release
598 *
599 * Release a dentry. This will drop the usage count and if appropriate
600 * call the dentry unlink method as well as removing it from the queues and
601 * releasing its resources. If the parent dentries were scheduled for release
602 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604void dput(struct dentry *dentry)
605{
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700606 if (unlikely(!dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return;
608
609repeat:
Waiman Long98474232013-08-28 18:24:59 -0700610 if (lockref_put_or_lock(&dentry->d_lockref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700613 /* Unreachable? Get rid of it */
614 if (unlikely(d_unhashed(dentry)))
615 goto kill_it;
616
617 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100619 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Nick Piggin265ac902010-10-10 05:36:24 -0400621
Jeff Layton39e3c952012-11-28 11:30:53 -0500622 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400623 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400624
Waiman Long98474232013-08-28 18:24:59 -0700625 dentry->d_lockref.count--;
Nick Piggin61f3dee2011-01-07 17:49:40 +1100626 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return;
628
Miklos Szeredid52b9082007-05-08 00:23:46 -0700629kill_it:
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000630 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700631 if (dentry)
632 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700634EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/**
637 * d_invalidate - invalidate a dentry
638 * @dentry: dentry to invalidate
639 *
640 * Try to invalidate the dentry if it turns out to be
641 * possible. If there are other dentries that can be
642 * reached through this one we can't delete it and we
643 * return -EBUSY. On success we return 0.
644 *
645 * no dcache lock.
646 */
647
648int d_invalidate(struct dentry * dentry)
649{
650 /*
651 * If it's already been dropped, return OK.
652 */
Nick Pigginda502952011-01-07 17:49:33 +1100653 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100655 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657 }
658 /*
659 * Check whether to do a partial shrink_dcache
660 * to get rid of unused child entries.
661 */
662 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100663 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100665 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 /*
669 * Somebody else still using it?
670 *
671 * If it's a directory, we can't drop it
672 * for fear of somebody re-populating it
673 * with children (even though dropping it
674 * would make it unreachable from the root,
675 * we might still populate it if it was a
676 * working directory or similar).
Al Viro50e69632011-11-07 16:39:57 +0000677 * We also need to leave mountpoints alone,
678 * directory or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Waiman Long98474232013-08-28 18:24:59 -0700680 if (dentry->d_lockref.count > 1 && dentry->d_inode) {
Al Viro50e69632011-11-07 16:39:57 +0000681 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -EBUSY;
684 }
685 }
686
687 __d_drop(dentry);
688 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700691EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100693/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100694static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Waiman Long98474232013-08-28 18:24:59 -0700696 dentry->d_lockref.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Nick Piggindc0474b2011-01-07 17:49:43 +1100699static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100700{
Waiman Long98474232013-08-28 18:24:59 -0700701 lockref_get(&dentry->d_lockref);
Nick Piggin23044502011-01-07 17:49:31 +1100702}
703
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100704struct dentry *dget_parent(struct dentry *dentry)
705{
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700706 int gotref;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100707 struct dentry *ret;
708
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700709 /*
710 * Do optimistic parent lookup without any
711 * locking.
712 */
713 rcu_read_lock();
714 ret = ACCESS_ONCE(dentry->d_parent);
715 gotref = lockref_get_not_zero(&ret->d_lockref);
716 rcu_read_unlock();
717 if (likely(gotref)) {
718 if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
719 return ret;
720 dput(ret);
721 }
722
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100723repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100724 /*
725 * Don't need rcu_dereference because we re-check it was correct under
726 * the lock.
727 */
728 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100729 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100730 spin_lock(&ret->d_lock);
731 if (unlikely(ret != dentry->d_parent)) {
732 spin_unlock(&ret->d_lock);
733 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100734 goto repeat;
735 }
Nick Piggina734eb42011-01-07 17:49:44 +1100736 rcu_read_unlock();
Waiman Long98474232013-08-28 18:24:59 -0700737 BUG_ON(!ret->d_lockref.count);
738 ret->d_lockref.count++;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100739 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100740 return ret;
741}
742EXPORT_SYMBOL(dget_parent);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/**
745 * d_find_alias - grab a hashed alias of inode
746 * @inode: inode in question
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700747 * @want_discon: flag, used by d_splice_alias, to request
748 * that only a DISCONNECTED alias be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
750 * If inode has a hashed alias, or is a directory and has any alias,
751 * acquire the reference to alias and return it. Otherwise return NULL.
752 * Notice that if inode is a directory there can be only one alias and
753 * it can be unhashed only if it has no children, or if it is the root
754 * of a filesystem.
755 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700756 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700757 * any other hashed alias over that one unless @want_discon is set,
758 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700760static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Nick Pigginda502952011-01-07 17:49:33 +1100762 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Nick Pigginda502952011-01-07 17:49:33 +1100764again:
765 discon_alias = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800766 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100767 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700769 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100770 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 discon_alias = alias;
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700772 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100773 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100774 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return alias;
776 }
777 }
Nick Pigginda502952011-01-07 17:49:33 +1100778 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Nick Pigginda502952011-01-07 17:49:33 +1100780 if (discon_alias) {
781 alias = discon_alias;
782 spin_lock(&alias->d_lock);
783 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
784 if (IS_ROOT(alias) &&
785 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100786 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100787 spin_unlock(&alias->d_lock);
788 return alias;
789 }
790 }
791 spin_unlock(&alias->d_lock);
792 goto again;
793 }
794 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Nick Pigginda502952011-01-07 17:49:33 +1100797struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
David Howells214fda12006-03-25 03:06:36 -0800799 struct dentry *de = NULL;
800
Al Virob3d9b7a2012-06-09 13:51:19 -0400801 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100802 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700803 de = __d_find_alias(inode, 0);
Nick Piggin873feea2011-01-07 17:50:06 +1100804 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return de;
807}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700808EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/*
811 * Try to kill dentries associated with this inode.
812 * WARNING: you must own a reference to inode.
813 */
814void d_prune_aliases(struct inode *inode)
815{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700816 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100818 spin_lock(&inode->i_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800819 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -0700821 if (!dentry->d_lockref.count) {
Yan, Zheng590fb512013-08-13 15:42:02 +0800822 /*
823 * inform the fs via d_prune that this dentry
824 * is about to be unhashed and destroyed.
825 */
826 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
827 !d_unhashed(dentry))
828 dentry->d_op->d_prune(dentry);
829
Nick Piggindc0474b2011-01-07 17:49:43 +1100830 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 __d_drop(dentry);
832 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100833 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 dput(dentry);
835 goto restart;
836 }
837 spin_unlock(&dentry->d_lock);
838 }
Nick Piggin873feea2011-01-07 17:50:06 +1100839 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700841EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843/*
Nick Piggin77812a12011-01-07 17:49:48 +1100844 * Try to throw away a dentry - free the inode, dput the parent.
845 * Requires dentry->d_lock is held, and dentry->d_count == 0.
846 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700847 *
Nick Piggin77812a12011-01-07 17:49:48 +1100848 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000850static struct dentry * try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200851 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Nick Piggin77812a12011-01-07 17:49:48 +1100853 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700854
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000855 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700856 /*
Nick Piggin77812a12011-01-07 17:49:48 +1100857 * If dentry_kill returns NULL, we have nothing more to do.
858 * if it returns the same dentry, trylocks failed. In either
859 * case, just loop again.
860 *
861 * Otherwise, we need to prune ancestors too. This is necessary
862 * to prevent quadratic behavior of shrink_dcache_parent(), but
863 * is also expected to be beneficial in reducing dentry cache
864 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700865 */
Nick Piggin77812a12011-01-07 17:49:48 +1100866 if (!parent)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000867 return NULL;
Nick Piggin77812a12011-01-07 17:49:48 +1100868 if (parent == dentry)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000869 return dentry;
Nick Piggin77812a12011-01-07 17:49:48 +1100870
871 /* Prune ancestors. */
872 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700873 while (dentry) {
Waiman Long98474232013-08-28 18:24:59 -0700874 if (lockref_put_or_lock(&dentry->d_lockref))
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000875 return NULL;
876 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700877 }
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000878 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400881static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700883 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700884
Nick Pigginec336792011-01-07 17:49:47 +1100885 rcu_read_lock();
886 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100887 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
888 if (&dentry->d_lru == list)
889 break; /* empty */
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400890
891 /*
892 * Get the dentry lock, and re-verify that the dentry is
893 * this on the shrinking list. If it is, we know that
894 * DCACHE_SHRINK_LIST and DCACHE_LRU_LIST are set.
895 */
Nick Pigginec336792011-01-07 17:49:47 +1100896 spin_lock(&dentry->d_lock);
897 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
898 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100899 continue;
900 }
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000903 * The dispose list is isolated and dentries are not accounted
904 * to the LRU here, so we can simply remove it from the list
905 * here regardless of whether it is referenced or not.
906 */
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400907 d_shrink_del(dentry);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000908
909 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * We found an inuse dentry which was not removed from
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000911 * the LRU because of laziness during lookup. Do not free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Waiman Long98474232013-08-28 18:24:59 -0700913 if (dentry->d_lockref.count) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700914 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 continue;
916 }
Nick Pigginec336792011-01-07 17:49:47 +1100917 rcu_read_unlock();
Nick Piggin77812a12011-01-07 17:49:48 +1100918
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400919 /*
920 * If 'try_to_prune()' returns a dentry, it will
921 * be the same one we passed in, and d_lock will
922 * have been held the whole time, so it will not
923 * have been added to any other lists. We failed
924 * to get the inode lock.
925 *
926 * We just add it back to the shrink list.
927 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000928 dentry = try_prune_one_dentry(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100929
Nick Pigginec336792011-01-07 17:49:47 +1100930 rcu_read_lock();
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000931 if (dentry) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400932 d_shrink_add(dentry, list);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000933 spin_unlock(&dentry->d_lock);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Nick Pigginec336792011-01-07 17:49:47 +1100936 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400937}
938
Dave Chinnerf6041562013-08-28 10:18:00 +1000939static enum lru_status
940dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)
941{
942 struct list_head *freeable = arg;
943 struct dentry *dentry = container_of(item, struct dentry, d_lru);
944
945
946 /*
947 * we are inverting the lru lock/dentry->d_lock here,
948 * so use a trylock. If we fail to get the lock, just skip
949 * it
950 */
951 if (!spin_trylock(&dentry->d_lock))
952 return LRU_SKIP;
953
954 /*
955 * Referenced dentries are still in use. If they have active
956 * counts, just remove them from the LRU. Otherwise give them
957 * another pass through the LRU.
958 */
959 if (dentry->d_lockref.count) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400960 d_lru_isolate(dentry);
Dave Chinnerf6041562013-08-28 10:18:00 +1000961 spin_unlock(&dentry->d_lock);
962 return LRU_REMOVED;
963 }
964
965 if (dentry->d_flags & DCACHE_REFERENCED) {
966 dentry->d_flags &= ~DCACHE_REFERENCED;
967 spin_unlock(&dentry->d_lock);
968
969 /*
970 * The list move itself will be made by the common LRU code. At
971 * this point, we've dropped the dentry->d_lock but keep the
972 * lru lock. This is safe to do, since every list movement is
973 * protected by the lru lock even if both locks are held.
974 *
975 * This is guaranteed by the fact that all LRU management
976 * functions are intermediated by the LRU API calls like
977 * list_lru_add and list_lru_del. List movement in this file
978 * only ever occur through this functions or through callbacks
979 * like this one, that are called from the LRU API.
980 *
981 * The only exceptions to this are functions like
982 * shrink_dentry_list, and code that first checks for the
983 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
984 * operating only with stack provided lists after they are
985 * properly isolated from the main list. It is thus, always a
986 * local access.
987 */
988 return LRU_ROTATE;
989 }
990
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400991 d_lru_shrink_move(dentry, freeable);
Dave Chinnerf6041562013-08-28 10:18:00 +1000992 spin_unlock(&dentry->d_lock);
993
994 return LRU_REMOVED;
995}
996
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400997/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000998 * prune_dcache_sb - shrink the dcache
999 * @sb: superblock
Dave Chinnerf6041562013-08-28 10:18:00 +10001000 * @nr_to_scan : number of entries to try to free
Dave Chinner9b17c622013-08-28 10:18:05 +10001001 * @nid: which node to scan for freeable entities
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001002 *
Dave Chinnerf6041562013-08-28 10:18:00 +10001003 * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001004 * done when we need more memory an called from the superblock shrinker
1005 * function.
1006 *
1007 * This function may fail to free any resources if all the dentries are in
1008 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001009 */
Dave Chinner9b17c622013-08-28 10:18:05 +10001010long prune_dcache_sb(struct super_block *sb, unsigned long nr_to_scan,
1011 int nid)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001012{
Dave Chinnerf6041562013-08-28 10:18:00 +10001013 LIST_HEAD(dispose);
1014 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001015
Dave Chinner9b17c622013-08-28 10:18:05 +10001016 freed = list_lru_walk_node(&sb->s_dentry_lru, nid, dentry_lru_isolate,
1017 &dispose, &nr_to_scan);
Dave Chinnerf6041562013-08-28 10:18:00 +10001018 shrink_dentry_list(&dispose);
Dave Chinner0a234c62013-08-28 10:17:57 +10001019 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Glauber Costa4e717f52013-08-28 10:18:03 +10001022static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1023 spinlock_t *lru_lock, void *arg)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001024{
Glauber Costa4e717f52013-08-28 10:18:03 +10001025 struct list_head *freeable = arg;
1026 struct dentry *dentry = container_of(item, struct dentry, d_lru);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001027
Glauber Costa4e717f52013-08-28 10:18:03 +10001028 /*
1029 * we are inverting the lru lock/dentry->d_lock here,
1030 * so use a trylock. If we fail to get the lock, just skip
1031 * it
1032 */
1033 if (!spin_trylock(&dentry->d_lock))
1034 return LRU_SKIP;
1035
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001036 d_lru_shrink_move(dentry, freeable);
Glauber Costa4e717f52013-08-28 10:18:03 +10001037 spin_unlock(&dentry->d_lock);
1038
1039 return LRU_REMOVED;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001040}
1041
Glauber Costa4e717f52013-08-28 10:18:03 +10001042
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * shrink_dcache_sb - shrink dcache for a superblock
1045 * @sb: superblock
1046 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001047 * Shrink the dcache for the specified super block. This is used to free
1048 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001050void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Glauber Costa4e717f52013-08-28 10:18:03 +10001052 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001053
Glauber Costa4e717f52013-08-28 10:18:03 +10001054 do {
1055 LIST_HEAD(dispose);
1056
1057 freed = list_lru_walk(&sb->s_dentry_lru,
1058 dentry_lru_isolate_shrink, &dispose, UINT_MAX);
1059
1060 this_cpu_sub(nr_dentry_unused, freed);
1061 shrink_dentry_list(&dispose);
1062 } while (freed > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001064EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066/*
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001067 * This tries to ascend one level of parenthood, but
1068 * we can race with renaming, so we need to re-check
1069 * the parenthood after dropping the lock and check
1070 * that the sequence number still matches.
1071 */
Al Viro48f5ec22013-09-09 15:22:25 -04001072static struct dentry *try_to_ascend(struct dentry *old, unsigned seq)
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001073{
1074 struct dentry *new = old->d_parent;
1075
1076 rcu_read_lock();
1077 spin_unlock(&old->d_lock);
1078 spin_lock(&new->d_lock);
1079
1080 /*
1081 * might go back up the wrong parent if we have had a rename
1082 * or deletion
1083 */
1084 if (new != old->d_parent ||
Miklos Szeredib161dfa62012-09-17 22:31:38 +02001085 (old->d_flags & DCACHE_DENTRY_KILLED) ||
Al Viro48f5ec22013-09-09 15:22:25 -04001086 need_seqretry(&rename_lock, seq)) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001087 spin_unlock(&new->d_lock);
1088 new = NULL;
1089 }
1090 rcu_read_unlock();
1091 return new;
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/**
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001095 * enum d_walk_ret - action to talke during tree walk
1096 * @D_WALK_CONTINUE: contrinue walk
1097 * @D_WALK_QUIT: quit walk
1098 * @D_WALK_NORETRY: quit when retry is needed
1099 * @D_WALK_SKIP: skip this dentry and its children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001101enum d_walk_ret {
1102 D_WALK_CONTINUE,
1103 D_WALK_QUIT,
1104 D_WALK_NORETRY,
1105 D_WALK_SKIP,
1106};
1107
1108/**
1109 * d_walk - walk the dentry tree
1110 * @parent: start of walk
1111 * @data: data passed to @enter() and @finish()
1112 * @enter: callback when first entering the dentry
1113 * @finish: callback when successfully finished the walk
1114 *
1115 * The @enter() and @finish() callbacks are called with d_lock held.
1116 */
1117static void d_walk(struct dentry *parent, void *data,
1118 enum d_walk_ret (*enter)(void *, struct dentry *),
1119 void (*finish)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Nick Piggin949854d2011-01-07 17:49:37 +11001121 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct list_head *next;
Al Viro48f5ec22013-09-09 15:22:25 -04001123 unsigned seq = 0;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001124 enum d_walk_ret ret;
1125 bool retry = true;
Nick Piggin949854d2011-01-07 17:49:37 +11001126
Nick Piggin58db63d2011-01-07 17:49:39 +11001127again:
Al Viro48f5ec22013-09-09 15:22:25 -04001128 read_seqbegin_or_lock(&rename_lock, &seq);
Nick Piggin58db63d2011-01-07 17:49:39 +11001129 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001130 spin_lock(&this_parent->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001131
1132 ret = enter(data, this_parent);
1133 switch (ret) {
1134 case D_WALK_CONTINUE:
1135 break;
1136 case D_WALK_QUIT:
1137 case D_WALK_SKIP:
1138 goto out_unlock;
1139 case D_WALK_NORETRY:
1140 retry = false;
1141 break;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143repeat:
1144 next = this_parent->d_subdirs.next;
1145resume:
1146 while (next != &this_parent->d_subdirs) {
1147 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001148 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001150
1151 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001152
1153 ret = enter(data, dentry);
1154 switch (ret) {
1155 case D_WALK_CONTINUE:
1156 break;
1157 case D_WALK_QUIT:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001158 spin_unlock(&dentry->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001159 goto out_unlock;
1160 case D_WALK_NORETRY:
1161 retry = false;
1162 break;
1163 case D_WALK_SKIP:
1164 spin_unlock(&dentry->d_lock);
1165 continue;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001166 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001169 spin_unlock(&this_parent->d_lock);
1170 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001172 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto repeat;
1174 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001175 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 /*
1178 * All done at this level ... ascend and resume the search.
1179 */
1180 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001181 struct dentry *child = this_parent;
Al Viro48f5ec22013-09-09 15:22:25 -04001182 this_parent = try_to_ascend(this_parent, seq);
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001183 if (!this_parent)
Nick Piggin949854d2011-01-07 17:49:37 +11001184 goto rename_retry;
Nick Piggin949854d2011-01-07 17:49:37 +11001185 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto resume;
1187 }
Al Viro48f5ec22013-09-09 15:22:25 -04001188 if (need_seqretry(&rename_lock, seq)) {
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001189 spin_unlock(&this_parent->d_lock);
1190 goto rename_retry;
1191 }
1192 if (finish)
1193 finish(data);
1194
1195out_unlock:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001196 spin_unlock(&this_parent->d_lock);
Al Viro48f5ec22013-09-09 15:22:25 -04001197 done_seqretry(&rename_lock, seq);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001198 return;
Nick Piggin58db63d2011-01-07 17:49:39 +11001199
1200rename_retry:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001201 if (!retry)
1202 return;
Al Viro48f5ec22013-09-09 15:22:25 -04001203 seq = 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001204 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001206
1207/*
1208 * Search for at least 1 mount point in the dentry's subdirs.
1209 * We descend to the next level whenever the d_subdirs
1210 * list is non-empty and continue searching.
1211 */
1212
1213/**
1214 * have_submounts - check for mounts over a dentry
1215 * @parent: dentry to check.
1216 *
1217 * Return true if the parent or its subdirectories contain
1218 * a mount point
1219 */
1220
1221static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
1222{
1223 int *ret = data;
1224 if (d_mountpoint(dentry)) {
1225 *ret = 1;
1226 return D_WALK_QUIT;
1227 }
1228 return D_WALK_CONTINUE;
1229}
1230
1231int have_submounts(struct dentry *parent)
1232{
1233 int ret = 0;
1234
1235 d_walk(parent, &ret, check_mount, NULL);
1236
1237 return ret;
1238}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001239EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241/*
Miklos Szeredieed81002013-09-05 14:39:11 +02001242 * Called by mount code to set a mountpoint and check if the mountpoint is
1243 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1244 * subtree can become unreachable).
1245 *
1246 * Only one of check_submounts_and_drop() and d_set_mounted() must succeed. For
1247 * this reason take rename_lock and d_lock on dentry and ancestors.
1248 */
1249int d_set_mounted(struct dentry *dentry)
1250{
1251 struct dentry *p;
1252 int ret = -ENOENT;
1253 write_seqlock(&rename_lock);
1254 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1255 /* Need exclusion wrt. check_submounts_and_drop() */
1256 spin_lock(&p->d_lock);
1257 if (unlikely(d_unhashed(p))) {
1258 spin_unlock(&p->d_lock);
1259 goto out;
1260 }
1261 spin_unlock(&p->d_lock);
1262 }
1263 spin_lock(&dentry->d_lock);
1264 if (!d_unlinked(dentry)) {
1265 dentry->d_flags |= DCACHE_MOUNTED;
1266 ret = 0;
1267 }
1268 spin_unlock(&dentry->d_lock);
1269out:
1270 write_sequnlock(&rename_lock);
1271 return ret;
1272}
1273
1274/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001275 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * and move any unused dentries to the end of the unused
1277 * list for prune_dcache(). We descend to the next level
1278 * whenever the d_subdirs list is non-empty and continue
1279 * searching.
1280 *
1281 * It returns zero iff there are no unused children,
1282 * otherwise it returns the number of children moved to
1283 * the end of the unused list. This may not be the total
1284 * number of unused children, because select_parent can
1285 * drop the lock and return early due to latency
1286 * constraints.
1287 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001288
1289struct select_data {
1290 struct dentry *start;
1291 struct list_head dispose;
1292 int found;
1293};
1294
1295static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001297 struct select_data *data = _data;
1298 enum d_walk_ret ret = D_WALK_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001300 if (data->start == dentry)
1301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001303 /*
1304 * move only zero ref count dentries to the dispose list.
1305 *
1306 * Those which are presently on the shrink list, being processed
1307 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1308 * loop in shrink_dcache_parent() might not make any progress
1309 * and loop forever.
1310 */
1311 if (dentry->d_lockref.count) {
1312 dentry_lru_del(dentry);
1313 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001314 /*
1315 * We can't use d_lru_shrink_move() because we
1316 * need to get the global LRU lock and do the
Linus Torvalds05a82522013-09-15 07:11:01 -04001317 * LRU accounting.
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001318 */
1319 d_lru_del(dentry);
1320 d_shrink_add(dentry, &data->dispose);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001321 data->found++;
1322 ret = D_WALK_NORETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324 /*
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001325 * We can return to the caller if we have found some (this
1326 * ensures forward progress). We'll be coming back to find
1327 * the rest.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001329 if (data->found && need_resched())
1330 ret = D_WALK_QUIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331out:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335/**
1336 * shrink_dcache_parent - prune dcache
1337 * @parent: parent of entries to prune
1338 *
1339 * Prune the dcache to remove unused children of the parent dentry.
1340 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001341void shrink_dcache_parent(struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001343 for (;;) {
1344 struct select_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001346 INIT_LIST_HEAD(&data.dispose);
1347 data.start = parent;
1348 data.found = 0;
1349
1350 d_walk(parent, &data, select_collect, NULL);
1351 if (!data.found)
1352 break;
1353
1354 shrink_dentry_list(&data.dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001355 cond_resched();
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001358EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Al Viro42c32602013-11-08 12:31:16 -05001360static enum d_walk_ret umount_collect(void *_data, struct dentry *dentry)
1361{
1362 struct select_data *data = _data;
1363 enum d_walk_ret ret = D_WALK_CONTINUE;
1364
1365 if (dentry->d_lockref.count) {
1366 dentry_lru_del(dentry);
1367 if (likely(!list_empty(&dentry->d_subdirs)))
1368 goto out;
1369 if (dentry == data->start && dentry->d_lockref.count == 1)
1370 goto out;
1371 printk(KERN_ERR
1372 "BUG: Dentry %p{i=%lx,n=%s}"
1373 " still in use (%d)"
1374 " [unmount of %s %s]\n",
1375 dentry,
1376 dentry->d_inode ?
1377 dentry->d_inode->i_ino : 0UL,
1378 dentry->d_name.name,
1379 dentry->d_lockref.count,
1380 dentry->d_sb->s_type->name,
1381 dentry->d_sb->s_id);
1382 BUG();
1383 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1384 /*
1385 * We can't use d_lru_shrink_move() because we
1386 * need to get the global LRU lock and do the
1387 * LRU accounting.
1388 */
1389 if (dentry->d_flags & DCACHE_LRU_LIST)
1390 d_lru_del(dentry);
1391 d_shrink_add(dentry, &data->dispose);
1392 data->found++;
1393 ret = D_WALK_NORETRY;
1394 }
1395out:
1396 if (data->found && need_resched())
1397 ret = D_WALK_QUIT;
1398 return ret;
1399}
1400
1401/*
1402 * destroy the dentries attached to a superblock on unmounting
1403 */
1404void shrink_dcache_for_umount(struct super_block *sb)
1405{
1406 struct dentry *dentry;
1407
1408 if (down_read_trylock(&sb->s_umount))
1409 BUG();
1410
1411 dentry = sb->s_root;
1412 sb->s_root = NULL;
1413 for (;;) {
1414 struct select_data data;
1415
1416 INIT_LIST_HEAD(&data.dispose);
1417 data.start = dentry;
1418 data.found = 0;
1419
1420 d_walk(dentry, &data, umount_collect, NULL);
1421 if (!data.found)
1422 break;
1423
1424 shrink_dentry_list(&data.dispose);
1425 cond_resched();
1426 }
1427 d_drop(dentry);
1428 dput(dentry);
1429
1430 while (!hlist_bl_empty(&sb->s_anon)) {
1431 struct select_data data;
1432 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
1433
1434 INIT_LIST_HEAD(&data.dispose);
1435 data.start = NULL;
1436 data.found = 0;
1437
1438 d_walk(dentry, &data, umount_collect, NULL);
1439 if (data.found)
1440 shrink_dentry_list(&data.dispose);
1441 cond_resched();
1442 }
1443}
1444
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001445static enum d_walk_ret check_and_collect(void *_data, struct dentry *dentry)
1446{
1447 struct select_data *data = _data;
1448
1449 if (d_mountpoint(dentry)) {
1450 data->found = -EBUSY;
1451 return D_WALK_QUIT;
1452 }
1453
1454 return select_collect(_data, dentry);
1455}
1456
1457static void check_and_drop(void *_data)
1458{
1459 struct select_data *data = _data;
1460
1461 if (d_mountpoint(data->start))
1462 data->found = -EBUSY;
1463 if (!data->found)
1464 __d_drop(data->start);
1465}
1466
1467/**
1468 * check_submounts_and_drop - prune dcache, check for submounts and drop
1469 *
1470 * All done as a single atomic operation relative to has_unlinked_ancestor().
1471 * Returns 0 if successfully unhashed @parent. If there were submounts then
1472 * return -EBUSY.
1473 *
1474 * @dentry: dentry to prune and drop
1475 */
1476int check_submounts_and_drop(struct dentry *dentry)
1477{
1478 int ret = 0;
1479
1480 /* Negative dentries can be dropped without further checks */
1481 if (!dentry->d_inode) {
1482 d_drop(dentry);
1483 goto out;
1484 }
1485
1486 for (;;) {
1487 struct select_data data;
1488
1489 INIT_LIST_HEAD(&data.dispose);
1490 data.start = dentry;
1491 data.found = 0;
1492
1493 d_walk(dentry, &data, check_and_collect, check_and_drop);
1494 ret = data.found;
1495
1496 if (!list_empty(&data.dispose))
1497 shrink_dentry_list(&data.dispose);
1498
1499 if (ret <= 0)
1500 break;
1501
1502 cond_resched();
1503 }
1504
1505out:
1506 return ret;
1507}
1508EXPORT_SYMBOL(check_submounts_and_drop);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/**
Al Viroa4464db2011-07-07 15:03:58 -04001511 * __d_alloc - allocate a dcache entry
1512 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * @name: qstr of the name
1514 *
1515 * Allocates a dentry. It returns %NULL if there is insufficient memory
1516 * available. On a success the dentry is returned. The name passed in is
1517 * copied and the copy passed in may be reused after this call.
1518 */
1519
Al Viroa4464db2011-07-07 15:03:58 -04001520struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct dentry *dentry;
1523 char *dname;
1524
Mel Gormane12ba742007-10-16 01:25:52 -07001525 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!dentry)
1527 return NULL;
1528
Linus Torvalds6326c712012-05-21 16:14:04 -07001529 /*
1530 * We guarantee that the inline name is always NUL-terminated.
1531 * This way the memcpy() done by the name switching in rename
1532 * will still always have a NUL at the end, even if we might
1533 * be overwriting an internal NUL character
1534 */
1535 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (name->len > DNAME_INLINE_LEN-1) {
1537 dname = kmalloc(name->len + 1, GFP_KERNEL);
1538 if (!dname) {
1539 kmem_cache_free(dentry_cache, dentry);
1540 return NULL;
1541 }
1542 } else {
1543 dname = dentry->d_iname;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 dentry->d_name.len = name->len;
1547 dentry->d_name.hash = name->hash;
1548 memcpy(dname, name->name, name->len);
1549 dname[name->len] = 0;
1550
Linus Torvalds6326c712012-05-21 16:14:04 -07001551 /* Make sure we always see the terminating NUL character */
1552 smp_wmb();
1553 dentry->d_name.name = dname;
1554
Waiman Long98474232013-08-28 18:24:59 -07001555 dentry->d_lockref.count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001556 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001558 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001560 dentry->d_parent = dentry;
1561 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 dentry->d_op = NULL;
1563 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001564 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 INIT_LIST_HEAD(&dentry->d_lru);
1566 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Virob3d9b7a2012-06-09 13:51:19 -04001567 INIT_HLIST_NODE(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001568 INIT_LIST_HEAD(&dentry->d_u.d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001569 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Nick Piggin3e880fb2011-01-07 17:49:19 +11001571 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return dentry;
1574}
Al Viroa4464db2011-07-07 15:03:58 -04001575
1576/**
1577 * d_alloc - allocate a dcache entry
1578 * @parent: parent of entry to allocate
1579 * @name: qstr of the name
1580 *
1581 * Allocates a dentry. It returns %NULL if there is insufficient memory
1582 * available. On a success the dentry is returned. The name passed in is
1583 * copied and the copy passed in may be reused after this call.
1584 */
1585struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1586{
1587 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1588 if (!dentry)
1589 return NULL;
1590
1591 spin_lock(&parent->d_lock);
1592 /*
1593 * don't need child lock because it is not subject
1594 * to concurrency here
1595 */
1596 __dget_dlock(parent);
1597 dentry->d_parent = parent;
1598 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1599 spin_unlock(&parent->d_lock);
1600
1601 return dentry;
1602}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001603EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Nick Piggin4b936882011-01-07 17:50:07 +11001605struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1606{
Al Viroa4464db2011-07-07 15:03:58 -04001607 struct dentry *dentry = __d_alloc(sb, name);
1608 if (dentry)
Nick Piggin4b936882011-01-07 17:50:07 +11001609 dentry->d_flags |= DCACHE_DISCONNECTED;
Nick Piggin4b936882011-01-07 17:50:07 +11001610 return dentry;
1611}
1612EXPORT_SYMBOL(d_alloc_pseudo);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1615{
1616 struct qstr q;
1617
1618 q.name = name;
1619 q.len = strlen(name);
1620 q.hash = full_name_hash(q.name, q.len);
1621 return d_alloc(parent, &q);
1622}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001623EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Nick Pigginfb045ad2011-01-07 17:49:55 +11001625void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1626{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001627 WARN_ON_ONCE(dentry->d_op);
1628 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001629 DCACHE_OP_COMPARE |
1630 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001631 DCACHE_OP_WEAK_REVALIDATE |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001632 DCACHE_OP_DELETE ));
1633 dentry->d_op = op;
1634 if (!op)
1635 return;
1636 if (op->d_hash)
1637 dentry->d_flags |= DCACHE_OP_HASH;
1638 if (op->d_compare)
1639 dentry->d_flags |= DCACHE_OP_COMPARE;
1640 if (op->d_revalidate)
1641 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001642 if (op->d_weak_revalidate)
1643 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001644 if (op->d_delete)
1645 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001646 if (op->d_prune)
1647 dentry->d_flags |= DCACHE_OP_PRUNE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001648
1649}
1650EXPORT_SYMBOL(d_set_d_op);
1651
David Howellsb18825a2013-09-12 19:22:53 +01001652static unsigned d_flags_for_inode(struct inode *inode)
1653{
1654 unsigned add_flags = DCACHE_FILE_TYPE;
1655
1656 if (!inode)
1657 return DCACHE_MISS_TYPE;
1658
1659 if (S_ISDIR(inode->i_mode)) {
1660 add_flags = DCACHE_DIRECTORY_TYPE;
1661 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1662 if (unlikely(!inode->i_op->lookup))
1663 add_flags = DCACHE_AUTODIR_TYPE;
1664 else
1665 inode->i_opflags |= IOP_LOOKUP;
1666 }
1667 } else if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1668 if (unlikely(inode->i_op->follow_link))
1669 add_flags = DCACHE_SYMLINK_TYPE;
1670 else
1671 inode->i_opflags |= IOP_NOFOLLOW;
1672 }
1673
1674 if (unlikely(IS_AUTOMOUNT(inode)))
1675 add_flags |= DCACHE_NEED_AUTOMOUNT;
1676 return add_flags;
1677}
1678
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001679static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1680{
David Howellsb18825a2013-09-12 19:22:53 +01001681 unsigned add_flags = d_flags_for_inode(inode);
1682
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001683 spin_lock(&dentry->d_lock);
David Howellsb18825a2013-09-12 19:22:53 +01001684 dentry->d_flags &= ~DCACHE_ENTRY_TYPE;
1685 dentry->d_flags |= add_flags;
1686 if (inode)
Al Virob3d9b7a2012-06-09 13:51:19 -04001687 hlist_add_head(&dentry->d_alias, &inode->i_dentry);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001688 dentry->d_inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001689 dentry_rcuwalk_barrier(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001690 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001691 fsnotify_d_instantiate(dentry, inode);
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/**
1695 * d_instantiate - fill in inode information for a dentry
1696 * @entry: dentry to complete
1697 * @inode: inode to attach to this dentry
1698 *
1699 * Fill in inode information in the entry.
1700 *
1701 * This turns negative dentries into productive full members
1702 * of society.
1703 *
1704 * NOTE! This assumes that the inode count has been incremented
1705 * (or otherwise set) by the caller to indicate that it is now
1706 * in use by the dcache.
1707 */
1708
1709void d_instantiate(struct dentry *entry, struct inode * inode)
1710{
Al Virob3d9b7a2012-06-09 13:51:19 -04001711 BUG_ON(!hlist_unhashed(&entry->d_alias));
Nick Piggin873feea2011-01-07 17:50:06 +11001712 if (inode)
1713 spin_lock(&inode->i_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001714 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001715 if (inode)
1716 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 security_d_instantiate(entry, inode);
1718}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001719EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/**
1722 * d_instantiate_unique - instantiate a non-aliased dentry
1723 * @entry: dentry to instantiate
1724 * @inode: inode to attach to this dentry
1725 *
1726 * Fill in inode information in the entry. On success, it returns NULL.
1727 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001728 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
1730 * Note that in order to avoid conflicts with rename() etc, the caller
1731 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001732 *
1733 * This also assumes that the inode count has been incremented
1734 * (or otherwise set) by the caller to indicate that it is now
1735 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
David Howells770bfad2006-08-22 20:06:07 -04001737static struct dentry *__d_instantiate_unique(struct dentry *entry,
1738 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct dentry *alias;
1741 int len = entry->d_name.len;
1742 const char *name = entry->d_name.name;
1743 unsigned int hash = entry->d_name.hash;
1744
David Howells770bfad2006-08-22 20:06:07 -04001745 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001746 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001747 return NULL;
1748 }
1749
Sasha Levinb67bfe02013-02-27 17:06:00 -08001750 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
Nick Piggin9abca362011-01-07 17:49:36 +11001751 /*
1752 * Don't need alias->d_lock here, because aliases with
1753 * d_parent == entry->d_parent are not subject to name or
1754 * parent changes, because the parent inode i_mutex is held.
1755 */
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001756 if (alias->d_name.hash != hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 continue;
1758 if (alias->d_parent != entry->d_parent)
1759 continue;
Linus Torvaldsee983e82012-05-10 12:37:10 -07001760 if (alias->d_name.len != len)
1761 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001762 if (dentry_cmp(alias, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001764 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return alias;
1766 }
David Howells770bfad2006-08-22 20:06:07 -04001767
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001768 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return NULL;
1770}
David Howells770bfad2006-08-22 20:06:07 -04001771
1772struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1773{
1774 struct dentry *result;
1775
Al Virob3d9b7a2012-06-09 13:51:19 -04001776 BUG_ON(!hlist_unhashed(&entry->d_alias));
David Howells770bfad2006-08-22 20:06:07 -04001777
Nick Piggin873feea2011-01-07 17:50:06 +11001778 if (inode)
1779 spin_lock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001780 result = __d_instantiate_unique(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001781 if (inode)
1782 spin_unlock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001783
1784 if (!result) {
1785 security_d_instantiate(entry, inode);
1786 return NULL;
1787 }
1788
1789 BUG_ON(!d_unhashed(result));
1790 iput(inode);
1791 return result;
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794EXPORT_SYMBOL(d_instantiate_unique);
1795
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001796/**
1797 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1798 * @entry: dentry to complete
1799 * @inode: inode to attach to this dentry
1800 *
1801 * Fill in inode information in the entry. If a directory alias is found, then
1802 * return an error (and drop inode). Together with d_materialise_unique() this
1803 * guarantees that a directory inode may never have more than one alias.
1804 */
1805int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1806{
1807 BUG_ON(!hlist_unhashed(&entry->d_alias));
1808
1809 spin_lock(&inode->i_lock);
1810 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1811 spin_unlock(&inode->i_lock);
1812 iput(inode);
1813 return -EBUSY;
1814 }
1815 __d_instantiate(entry, inode);
1816 spin_unlock(&inode->i_lock);
1817 security_d_instantiate(entry, inode);
1818
1819 return 0;
1820}
1821EXPORT_SYMBOL(d_instantiate_no_diralias);
1822
Al Viroadc0e912012-01-08 16:49:21 -05001823struct dentry *d_make_root(struct inode *root_inode)
1824{
1825 struct dentry *res = NULL;
1826
1827 if (root_inode) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07001828 static const struct qstr name = QSTR_INIT("/", 1);
Al Viroadc0e912012-01-08 16:49:21 -05001829
1830 res = __d_alloc(root_inode->i_sb, &name);
1831 if (res)
1832 d_instantiate(res, root_inode);
1833 else
1834 iput(root_inode);
1835 }
1836 return res;
1837}
1838EXPORT_SYMBOL(d_make_root);
1839
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001840static struct dentry * __d_find_any_alias(struct inode *inode)
1841{
1842 struct dentry *alias;
1843
Al Virob3d9b7a2012-06-09 13:51:19 -04001844 if (hlist_empty(&inode->i_dentry))
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001845 return NULL;
Al Virob3d9b7a2012-06-09 13:51:19 -04001846 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001847 __dget(alias);
1848 return alias;
1849}
1850
Sage Weil46f72b32012-01-10 09:04:37 -08001851/**
1852 * d_find_any_alias - find any alias for a given inode
1853 * @inode: inode to find an alias for
1854 *
1855 * If any aliases exist for the given inode, take and return a
1856 * reference for one of them. If no aliases exist, return %NULL.
1857 */
1858struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001859{
1860 struct dentry *de;
1861
1862 spin_lock(&inode->i_lock);
1863 de = __d_find_any_alias(inode);
1864 spin_unlock(&inode->i_lock);
1865 return de;
1866}
Sage Weil46f72b32012-01-10 09:04:37 -08001867EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001870 * d_obtain_alias - find or allocate a dentry for a given inode
1871 * @inode: inode to allocate the dentry for
1872 *
1873 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1874 * similar open by handle operations. The returned dentry may be anonymous,
1875 * or may have a full name (if the inode was already in the cache).
1876 *
1877 * When called on a directory inode, we must ensure that the inode only ever
1878 * has one dentry. If a dentry is found, that is returned instead of
1879 * allocating a new one.
1880 *
1881 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001882 * to the dentry. In case of an error the reference on the inode is released.
1883 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1884 * be passed in and will be the error will be propagate to the return value,
1885 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001886 */
1887struct dentry *d_obtain_alias(struct inode *inode)
1888{
NeilBrownb911a6b2012-11-08 16:09:37 -08001889 static const struct qstr anonstring = QSTR_INIT("/", 1);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001890 struct dentry *tmp;
1891 struct dentry *res;
David Howellsb18825a2013-09-12 19:22:53 +01001892 unsigned add_flags;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001893
1894 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001895 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001896 if (IS_ERR(inode))
1897 return ERR_CAST(inode);
1898
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001899 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001900 if (res)
1901 goto out_iput;
1902
Al Viroa4464db2011-07-07 15:03:58 -04001903 tmp = __d_alloc(inode->i_sb, &anonstring);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001904 if (!tmp) {
1905 res = ERR_PTR(-ENOMEM);
1906 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001907 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001908
Nick Piggin873feea2011-01-07 17:50:06 +11001909 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001910 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001911 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001912 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001913 dput(tmp);
1914 goto out_iput;
1915 }
1916
1917 /* attach a disconnected dentry */
David Howellsb18825a2013-09-12 19:22:53 +01001918 add_flags = d_flags_for_inode(inode) | DCACHE_DISCONNECTED;
1919
Christoph Hellwig9308a612008-08-11 15:49:12 +02001920 spin_lock(&tmp->d_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001921 tmp->d_inode = inode;
David Howellsb18825a2013-09-12 19:22:53 +01001922 tmp->d_flags |= add_flags;
Al Virob3d9b7a2012-06-09 13:51:19 -04001923 hlist_add_head(&tmp->d_alias, &inode->i_dentry);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001924 hlist_bl_lock(&tmp->d_sb->s_anon);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001925 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001926 hlist_bl_unlock(&tmp->d_sb->s_anon);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001927 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001928 spin_unlock(&inode->i_lock);
Josef Bacik24ff6662010-11-18 20:52:55 -05001929 security_d_instantiate(tmp, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001930
Christoph Hellwig9308a612008-08-11 15:49:12 +02001931 return tmp;
1932
1933 out_iput:
Josef Bacik24ff6662010-11-18 20:52:55 -05001934 if (res && !IS_ERR(res))
1935 security_d_instantiate(res, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001936 iput(inode);
1937 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001938}
Benny Halevyadc48722009-02-27 14:02:59 -08001939EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941/**
1942 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1943 * @inode: the inode which may have a disconnected dentry
1944 * @dentry: a negative dentry which we want to point to the inode.
1945 *
1946 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1947 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1948 * and return it, else simply d_add the inode to the dentry and return NULL.
1949 *
1950 * This is needed in the lookup routine of any filesystem that is exportable
1951 * (via knfsd) so that we can build dcache paths to directories effectively.
1952 *
1953 * If a dentry was found and moved, then it is returned. Otherwise NULL
1954 * is returned. This matches the expected return value of ->lookup.
1955 *
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001956 * Cluster filesystems may call this function with a negative, hashed dentry.
1957 * In that case, we know that the inode will be a regular file, and also this
1958 * will only occur during atomic_open. So we need to check for the dentry
1959 * being already hashed only in the final case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
1961struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1962{
1963 struct dentry *new = NULL;
1964
Al Viroa9049372011-07-08 21:20:11 -04001965 if (IS_ERR(inode))
1966 return ERR_CAST(inode);
1967
NeilBrown21c0d8f2006-10-04 02:16:16 -07001968 if (inode && S_ISDIR(inode->i_mode)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001969 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001970 new = __d_find_alias(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (new) {
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001972 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Piggin873feea2011-01-07 17:50:06 +11001973 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 d_move(new, dentry);
1976 iput(inode);
1977 } else {
Nick Piggin873feea2011-01-07 17:50:06 +11001978 /* already taking inode->i_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001979 __d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001980 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 security_d_instantiate(dentry, inode);
1982 d_rehash(dentry);
1983 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001984 } else {
1985 d_instantiate(dentry, inode);
1986 if (d_unhashed(dentry))
1987 d_rehash(dentry);
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return new;
1990}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001991EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Barry Naujok94035402008-05-21 16:50:46 +10001993/**
1994 * d_add_ci - lookup or allocate new dentry with case-exact name
1995 * @inode: the inode case-insensitive lookup has found
1996 * @dentry: the negative dentry that was passed to the parent's lookup func
1997 * @name: the case-exact name to be associated with the returned dentry
1998 *
1999 * This is to avoid filling the dcache with case-insensitive names to the
2000 * same inode, only the actual correct case is stored in the dcache for
2001 * case-insensitive filesystems.
2002 *
2003 * For a case-insensitive lookup match and if the the case-exact dentry
2004 * already exists in in the dcache, use it and return it.
2005 *
2006 * If no entry exists with the exact case name, allocate new dentry with
2007 * the exact case, and return the spliced entry.
2008 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02002009struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10002010 struct qstr *name)
2011{
Barry Naujok94035402008-05-21 16:50:46 +10002012 struct dentry *found;
2013 struct dentry *new;
2014
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002015 /*
2016 * First check if a dentry matching the name already exists,
2017 * if not go ahead and create it now.
2018 */
Barry Naujok94035402008-05-21 16:50:46 +10002019 found = d_hash_and_lookup(dentry->d_parent, name);
Al Viro4f522a22013-02-11 23:20:37 -05002020 if (unlikely(IS_ERR(found)))
2021 goto err_out;
Barry Naujok94035402008-05-21 16:50:46 +10002022 if (!found) {
2023 new = d_alloc(dentry->d_parent, name);
2024 if (!new) {
Al Viro4f522a22013-02-11 23:20:37 -05002025 found = ERR_PTR(-ENOMEM);
Barry Naujok94035402008-05-21 16:50:46 +10002026 goto err_out;
2027 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002028
Barry Naujok94035402008-05-21 16:50:46 +10002029 found = d_splice_alias(inode, new);
2030 if (found) {
2031 dput(new);
2032 return found;
2033 }
2034 return new;
2035 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002036
2037 /*
2038 * If a matching dentry exists, and it's not negative use it.
2039 *
2040 * Decrement the reference count to balance the iget() done
2041 * earlier on.
2042 */
Barry Naujok94035402008-05-21 16:50:46 +10002043 if (found->d_inode) {
2044 if (unlikely(found->d_inode != inode)) {
2045 /* This can't happen because bad inodes are unhashed. */
2046 BUG_ON(!is_bad_inode(inode));
2047 BUG_ON(!is_bad_inode(found->d_inode));
2048 }
Barry Naujok94035402008-05-21 16:50:46 +10002049 iput(inode);
2050 return found;
2051 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002052
Barry Naujok94035402008-05-21 16:50:46 +10002053 /*
2054 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002055 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10002056 */
Al Viro4513d892011-07-17 10:52:14 -04002057 new = d_splice_alias(inode, found);
2058 if (new) {
2059 dput(found);
2060 found = new;
Barry Naujok94035402008-05-21 16:50:46 +10002061 }
Al Viro4513d892011-07-17 10:52:14 -04002062 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002063
2064err_out:
2065 iput(inode);
Al Viro4f522a22013-02-11 23:20:37 -05002066 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002067}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002068EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002070/*
2071 * Do the slow-case of the dentry name compare.
2072 *
2073 * Unlike the dentry_cmp() function, we need to atomically
Linus Torvaldsda53be12013-05-21 15:22:44 -07002074 * load the name and length information, so that the
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002075 * filesystem can rely on them, and can use the 'name' and
2076 * 'len' information without worrying about walking off the
2077 * end of memory etc.
2078 *
2079 * Thus the read_seqcount_retry() and the "duplicate" info
2080 * in arguments (the low-level filesystem should not look
2081 * at the dentry inode or name contents directly, since
2082 * rename can change them while we're in RCU mode).
2083 */
2084enum slow_d_compare {
2085 D_COMP_OK,
2086 D_COMP_NOMATCH,
2087 D_COMP_SEQRETRY,
2088};
2089
2090static noinline enum slow_d_compare slow_dentry_cmp(
2091 const struct dentry *parent,
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002092 struct dentry *dentry,
2093 unsigned int seq,
2094 const struct qstr *name)
2095{
2096 int tlen = dentry->d_name.len;
2097 const char *tname = dentry->d_name.name;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002098
2099 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2100 cpu_relax();
2101 return D_COMP_SEQRETRY;
2102 }
Linus Torvaldsda53be12013-05-21 15:22:44 -07002103 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002104 return D_COMP_NOMATCH;
2105 return D_COMP_OK;
2106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/**
Nick Piggin31e6b012011-01-07 17:49:52 +11002109 * __d_lookup_rcu - search for a dentry (racy, store-free)
2110 * @parent: parent dentry
2111 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07002112 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11002113 * Returns: dentry, or NULL
2114 *
2115 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2116 * resolution (store-free path walking) design described in
2117 * Documentation/filesystems/path-lookup.txt.
2118 *
2119 * This is not to be used outside core vfs.
2120 *
2121 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2122 * held, and rcu_read_lock held. The returned dentry must not be stored into
2123 * without taking d_lock and checking d_seq sequence count against @seq
2124 * returned here.
2125 *
Linus Torvalds15570082013-09-02 11:38:06 -07002126 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
Nick Piggin31e6b012011-01-07 17:49:52 +11002127 * function.
2128 *
2129 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2130 * the returned dentry, so long as its parent's seqlock is checked after the
2131 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2132 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002133 *
2134 * NOTE! The caller *has* to check the resulting dentry against the sequence
2135 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11002136 */
Linus Torvalds8966be92012-03-02 14:23:30 -08002137struct dentry *__d_lookup_rcu(const struct dentry *parent,
2138 const struct qstr *name,
Linus Torvaldsda53be12013-05-21 15:22:44 -07002139 unsigned *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11002140{
Linus Torvalds26fe5752012-05-10 13:14:12 -07002141 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11002142 const unsigned char *str = name->name;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002143 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002144 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002145 struct dentry *dentry;
2146
2147 /*
2148 * Note: There is significant duplication with __d_lookup_rcu which is
2149 * required to prevent single threaded performance regressions
2150 * especially on architectures where smp_rmb (in seqcounts) are costly.
2151 * Keep the two functions in sync.
2152 */
2153
2154 /*
2155 * The hash list is protected using RCU.
2156 *
2157 * Carefully use d_seq when comparing a candidate dentry, to avoid
2158 * races with d_move().
2159 *
2160 * It is possible that concurrent renames can mess up our list
2161 * walk here and result in missing our dentry, resulting in the
2162 * false-negative result. d_lookup() protects against concurrent
2163 * renames using rename_lock seqlock.
2164 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002165 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11002166 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002167 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08002168 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11002169
Nick Piggin31e6b012011-01-07 17:49:52 +11002170seqretry:
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002171 /*
2172 * The dentry sequence count protects us from concurrent
Linus Torvaldsda53be12013-05-21 15:22:44 -07002173 * renames, and thus protects parent and name fields.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002174 *
2175 * The caller must perform a seqcount check in order
Linus Torvaldsda53be12013-05-21 15:22:44 -07002176 * to do anything useful with the returned dentry.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002177 *
2178 * NOTE! We do a "raw" seqcount_begin here. That means that
2179 * we don't wait for the sequence count to stabilize if it
2180 * is in the middle of a sequence change. If we do the slow
2181 * dentry compare, we will do seqretries until it is stable,
2182 * and if we end up with a successful lookup, we actually
2183 * want to exit RCU lookup anyway.
2184 */
2185 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11002186 if (dentry->d_parent != parent)
2187 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07002188 if (d_unhashed(dentry))
2189 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002190
2191 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07002192 if (dentry->d_name.hash != hashlen_hash(hashlen))
2193 continue;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002194 *seqp = seq;
2195 switch (slow_dentry_cmp(parent, dentry, seq, name)) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002196 case D_COMP_OK:
2197 return dentry;
2198 case D_COMP_NOMATCH:
2199 continue;
2200 default:
2201 goto seqretry;
2202 }
2203 }
2204
Linus Torvalds26fe5752012-05-10 13:14:12 -07002205 if (dentry->d_name.hash_len != hashlen)
Linus Torvaldsee983e82012-05-10 12:37:10 -07002206 continue;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002207 *seqp = seq;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002208 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002209 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002210 }
2211 return NULL;
2212}
2213
2214/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * d_lookup - search for a dentry
2216 * @parent: parent dentry
2217 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10002218 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 *
Nick Pigginb04f7842010-08-18 04:37:34 +10002220 * d_lookup searches the children of the parent dentry for the name in
2221 * question. If the dentry is found its reference count is incremented and the
2222 * dentry is returned. The caller must use dput to free the entry when it has
2223 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
Al Viroda2d8452013-01-24 18:29:34 -05002225struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Nick Piggin31e6b012011-01-07 17:49:52 +11002227 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002228 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 do {
2231 seq = read_seqbegin(&rename_lock);
2232 dentry = __d_lookup(parent, name);
2233 if (dentry)
2234 break;
2235 } while (read_seqretry(&rename_lock, seq));
2236 return dentry;
2237}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002238EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Nick Piggin31e6b012011-01-07 17:49:52 +11002240/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002241 * __d_lookup - search for a dentry (racy)
2242 * @parent: parent dentry
2243 * @name: qstr of name we wish to find
2244 * Returns: dentry, or NULL
2245 *
2246 * __d_lookup is like d_lookup, however it may (rarely) return a
2247 * false-negative result due to unrelated rename activity.
2248 *
2249 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2250 * however it must be used carefully, eg. with a following d_lookup in
2251 * the case of failure.
2252 *
2253 * __d_lookup callers must be commented.
2254 */
Al Viroa713ca22013-01-24 18:27:00 -05002255struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 unsigned int len = name->len;
2258 unsigned int hash = name->hash;
2259 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002260 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002261 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002262 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002263 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Nick Pigginb04f7842010-08-18 04:37:34 +10002265 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002266 * Note: There is significant duplication with __d_lookup_rcu which is
2267 * required to prevent single threaded performance regressions
2268 * especially on architectures where smp_rmb (in seqcounts) are costly.
2269 * Keep the two functions in sync.
2270 */
2271
2272 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002273 * The hash list is protected using RCU.
2274 *
2275 * Take d_lock when comparing a candidate dentry, to avoid races
2276 * with d_move().
2277 *
2278 * It is possible that concurrent renames can mess up our list
2279 * walk here and result in missing our dentry, resulting in the
2280 * false-negative result. d_lookup() protects against concurrent
2281 * renames using rename_lock seqlock.
2282 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002283 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 rcu_read_lock();
2286
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002287 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (dentry->d_name.hash != hash)
2290 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (dentry->d_parent != parent)
2294 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002295 if (d_unhashed(dentry))
2296 goto next;
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * It is safe to compare names since d_move() cannot
2300 * change the qstr (protected by d_lock).
2301 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11002302 if (parent->d_flags & DCACHE_OP_COMPARE) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002303 int tlen = dentry->d_name.len;
2304 const char *tname = dentry->d_name.name;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002305 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto next;
2307 } else {
Linus Torvaldsee983e82012-05-10 12:37:10 -07002308 if (dentry->d_name.len != len)
2309 goto next;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002310 if (dentry_cmp(dentry, str, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 goto next;
2312 }
2313
Waiman Long98474232013-08-28 18:24:59 -07002314 dentry->d_lockref.count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002315 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 spin_unlock(&dentry->d_lock);
2317 break;
2318next:
2319 spin_unlock(&dentry->d_lock);
2320 }
2321 rcu_read_unlock();
2322
2323 return found;
2324}
2325
2326/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002327 * d_hash_and_lookup - hash the qstr then search for a dentry
2328 * @dir: Directory to search in
2329 * @name: qstr of name we wish to find
2330 *
Al Viro4f522a22013-02-11 23:20:37 -05002331 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002332 */
2333struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2334{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002335 /*
2336 * Check for a fs-specific hash function. Note that we must
2337 * calculate the standard hash first, as the d_op->d_hash()
2338 * routine may choose to leave the hash value unchanged.
2339 */
2340 name->hash = full_name_hash(name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002341 if (dir->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002342 int err = dir->d_op->d_hash(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05002343 if (unlikely(err < 0))
2344 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002345 }
Al Viro4f522a22013-02-11 23:20:37 -05002346 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002347}
Al Viro4f522a22013-02-11 23:20:37 -05002348EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002349
2350/**
Nick Piggin786a5e12011-01-07 17:49:16 +11002351 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 * @dentry: The dentry alleged to be valid child of @dparent
Randy Dunlapff5fdb62011-01-22 20:16:06 -08002353 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
2355 * An insecure source has sent us a dentry, here we verify it and dget() it.
2356 * This is used by ncpfs in its readdir implementation.
2357 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11002358 *
2359 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
Nick Piggind3a23e12011-01-05 20:01:21 +11002361int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Nick Piggin786a5e12011-01-07 17:49:16 +11002363 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11002364
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002365 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002366 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
2367 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002368 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggindc0474b2011-01-07 17:49:43 +11002369 __dget_dlock(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002370 spin_unlock(&dentry->d_lock);
2371 spin_unlock(&dparent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return 1;
2373 }
2374 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002375 spin_unlock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return 0;
2378}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002379EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381/*
2382 * When a file is deleted, we have two options:
2383 * - turn this dentry into a negative dentry
2384 * - unhash this dentry and free it.
2385 *
2386 * Usually, we want to just turn this into
2387 * a negative dentry, but if anybody else is
2388 * currently using the dentry or the inode
2389 * we can't do that and we fall back on removing
2390 * it from the hash queues and waiting for
2391 * it to be deleted later when it has no users
2392 */
2393
2394/**
2395 * d_delete - delete a dentry
2396 * @dentry: The dentry to delete
2397 *
2398 * Turn the dentry into a negative dentry if possible, otherwise
2399 * remove it from the hash queues so it can be deleted later
2400 */
2401
2402void d_delete(struct dentry * dentry)
2403{
Nick Piggin873feea2011-01-07 17:50:06 +11002404 struct inode *inode;
John McCutchan7a91bf72005-08-08 13:52:16 -04002405 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /*
2407 * Are we the only user?
2408 */
Nick Piggin357f8e62011-01-07 17:49:42 +11002409again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 spin_lock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002411 inode = dentry->d_inode;
2412 isdir = S_ISDIR(inode->i_mode);
Waiman Long98474232013-08-28 18:24:59 -07002413 if (dentry->d_lockref.count == 1) {
Alan Cox1fe0c022012-09-19 15:49:51 +01002414 if (!spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002415 spin_unlock(&dentry->d_lock);
2416 cpu_relax();
2417 goto again;
2418 }
Al Viro13e3c5e2010-05-21 16:11:04 -04002419 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002420 dentry_unlink_inode(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04002421 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return;
2423 }
2424
2425 if (!d_unhashed(dentry))
2426 __d_drop(dentry);
2427
2428 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04002429
2430 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002432EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002434static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002436 BUG_ON(!d_unhashed(entry));
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002437 hlist_bl_lock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -07002438 entry->d_flags |= DCACHE_RCUACCESS;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002439 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002440 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
David Howells770bfad2006-08-22 20:06:07 -04002443static void _d_rehash(struct dentry * entry)
2444{
2445 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/**
2449 * d_rehash - add an entry back to the hash
2450 * @entry: dentry to add to the hash
2451 *
2452 * Adds a dentry to the hash according to its name.
2453 */
2454
2455void d_rehash(struct dentry * entry)
2456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04002458 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002461EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002463/**
2464 * dentry_update_name_case - update case insensitive dentry with a new name
2465 * @dentry: dentry to be updated
2466 * @name: new name
2467 *
2468 * Update a case insensitive dentry with new case of name.
2469 *
2470 * dentry must have been returned by d_lookup with name @name. Old and new
2471 * name lengths must match (ie. no d_compare which allows mismatched name
2472 * lengths).
2473 *
2474 * Parent inode i_mutex must be held over d_lookup and into this call (to
2475 * keep renames and concurrent inserts, and readdir(2) away).
2476 */
2477void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2478{
Linus Torvalds7ebfa572011-04-15 07:34:26 -07002479 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002480 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2481
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002482 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002483 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002484 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002485 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002486 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002487}
2488EXPORT_SYMBOL(dentry_update_name_case);
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490static void switch_names(struct dentry *dentry, struct dentry *target)
2491{
2492 if (dname_external(target)) {
2493 if (dname_external(dentry)) {
2494 /*
2495 * Both external: swap the pointers
2496 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002497 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 } else {
2499 /*
2500 * dentry:internal, target:external. Steal target's
2501 * storage and make target internal.
2502 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002503 memcpy(target->d_iname, dentry->d_name.name,
2504 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 dentry->d_name.name = target->d_name.name;
2506 target->d_name.name = target->d_iname;
2507 }
2508 } else {
2509 if (dname_external(dentry)) {
2510 /*
2511 * dentry:external, target:internal. Give dentry's
2512 * storage to target and make dentry internal
2513 */
2514 memcpy(dentry->d_iname, target->d_name.name,
2515 target->d_name.len + 1);
2516 target->d_name.name = dentry->d_name.name;
2517 dentry->d_name.name = dentry->d_iname;
2518 } else {
2519 /*
2520 * Both are internal. Just copy target to dentry
2521 */
2522 memcpy(dentry->d_iname, target->d_name.name,
2523 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05002524 dentry->d_name.len = target->d_name.len;
2525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002528 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002531static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2532{
2533 /*
2534 * XXXX: do we really need to take target->d_lock?
2535 */
2536 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2537 spin_lock(&target->d_parent->d_lock);
2538 else {
2539 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2540 spin_lock(&dentry->d_parent->d_lock);
2541 spin_lock_nested(&target->d_parent->d_lock,
2542 DENTRY_D_LOCK_NESTED);
2543 } else {
2544 spin_lock(&target->d_parent->d_lock);
2545 spin_lock_nested(&dentry->d_parent->d_lock,
2546 DENTRY_D_LOCK_NESTED);
2547 }
2548 }
2549 if (target < dentry) {
2550 spin_lock_nested(&target->d_lock, 2);
2551 spin_lock_nested(&dentry->d_lock, 3);
2552 } else {
2553 spin_lock_nested(&dentry->d_lock, 2);
2554 spin_lock_nested(&target->d_lock, 3);
2555 }
2556}
2557
2558static void dentry_unlock_parents_for_move(struct dentry *dentry,
2559 struct dentry *target)
2560{
2561 if (target->d_parent != dentry->d_parent)
2562 spin_unlock(&dentry->d_parent->d_lock);
2563 if (target->d_parent != target)
2564 spin_unlock(&target->d_parent->d_lock);
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002568 * When switching names, the actual string doesn't strictly have to
2569 * be preserved in the target - because we're dropping the target
2570 * anyway. As such, we can just do a simple memcpy() to copy over
2571 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002573 * Note that we have to be a lot more careful about getting the hash
2574 * switched - we have to switch the hash value properly even if it
2575 * then no longer matches the actual (corrupted) string of the target.
2576 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002578/*
Al Viro18367502011-07-12 21:42:24 -04002579 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 * @dentry: entry to move
2581 * @target: new dentry
2582 *
2583 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002584 * dcache entries should not be moved in this way. Caller must hold
2585 * rename_lock, the i_mutex of the source and target directories,
2586 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
Al Viro18367502011-07-12 21:42:24 -04002588static void __d_move(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (!dentry->d_inode)
2591 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2592
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002593 BUG_ON(d_ancestor(dentry, target));
2594 BUG_ON(d_ancestor(target, dentry));
2595
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002596 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Nick Piggin31e6b012011-01-07 17:49:52 +11002598 write_seqcount_begin(&dentry->d_seq);
2599 write_seqcount_begin(&target->d_seq);
2600
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002601 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2602
2603 /*
2604 * Move the dentry to the target hash queue. Don't bother checking
2605 * for the same hash queue because of how unlikely it is.
2606 */
2607 __d_drop(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11002608 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 /* Unhash the target: dput() will then get rid of it */
2611 __d_drop(target);
2612
Eric Dumazet5160ee62006-01-08 01:03:32 -08002613 list_del(&dentry->d_u.d_child);
2614 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* Switch the names.. */
2617 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002618 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 /* ... and switch the parents */
2621 if (IS_ROOT(dentry)) {
2622 dentry->d_parent = target->d_parent;
2623 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002624 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002626 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002629 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631
Eric Dumazet5160ee62006-01-08 01:03:32 -08002632 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002633
Nick Piggin31e6b012011-01-07 17:49:52 +11002634 write_seqcount_end(&target->d_seq);
2635 write_seqcount_end(&dentry->d_seq);
2636
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002637 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002639 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 spin_unlock(&dentry->d_lock);
Al Viro18367502011-07-12 21:42:24 -04002641}
2642
2643/*
2644 * d_move - move a dentry
2645 * @dentry: entry to move
2646 * @target: new dentry
2647 *
2648 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002649 * dcache entries should not be moved in this way. See the locking
2650 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002651 */
2652void d_move(struct dentry *dentry, struct dentry *target)
2653{
2654 write_seqlock(&rename_lock);
2655 __d_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002657}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002658EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002660/**
2661 * d_ancestor - search for an ancestor
2662 * @p1: ancestor dentry
2663 * @p2: child dentry
2664 *
2665 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2666 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002667 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002668struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002669{
2670 struct dentry *p;
2671
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002672 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002673 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002674 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002675 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002676 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002677}
2678
2679/*
2680 * This helper attempts to cope with remotely renamed directories
2681 *
2682 * It assumes that the caller is already holding
Al Viro18367502011-07-12 21:42:24 -04002683 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002684 *
2685 * Note: If ever the locking in lock_rename() changes, then please
2686 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002687 */
Nick Piggin873feea2011-01-07 17:50:06 +11002688static struct dentry *__d_unalias(struct inode *inode,
2689 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002690{
2691 struct mutex *m1 = NULL, *m2 = NULL;
Al Viroee3efa92012-06-08 15:59:33 -04002692 struct dentry *ret = ERR_PTR(-EBUSY);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002693
2694 /* If alias and dentry share a parent, then no extra locks required */
2695 if (alias->d_parent == dentry->d_parent)
2696 goto out_unalias;
2697
Trond Myklebust9eaef272006-10-21 10:24:20 -07002698 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002699 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2700 goto out_err;
2701 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2702 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2703 goto out_err;
2704 m2 = &alias->d_parent->d_inode->i_mutex;
2705out_unalias:
Al Viroee3efa92012-06-08 15:59:33 -04002706 if (likely(!d_mountpoint(alias))) {
2707 __d_move(alias, dentry);
2708 ret = alias;
2709 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002710out_err:
Nick Piggin873feea2011-01-07 17:50:06 +11002711 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002712 if (m2)
2713 mutex_unlock(m2);
2714 if (m1)
2715 mutex_unlock(m1);
2716 return ret;
2717}
2718
2719/*
David Howells770bfad2006-08-22 20:06:07 -04002720 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2721 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002722 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002723 */
2724static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2725{
Al Viro740da422013-01-30 10:13:38 -05002726 struct dentry *dparent;
David Howells770bfad2006-08-22 20:06:07 -04002727
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002728 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002729
Nick Piggin31e6b012011-01-07 17:49:52 +11002730 write_seqcount_begin(&dentry->d_seq);
2731 write_seqcount_begin(&anon->d_seq);
2732
David Howells770bfad2006-08-22 20:06:07 -04002733 dparent = dentry->d_parent;
David Howells770bfad2006-08-22 20:06:07 -04002734
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002735 switch_names(dentry, anon);
2736 swap(dentry->d_name.hash, anon->d_name.hash);
2737
Al Viro740da422013-01-30 10:13:38 -05002738 dentry->d_parent = dentry;
2739 list_del_init(&dentry->d_u.d_child);
2740 anon->d_parent = dparent;
Wei Yongjun9ed53b12013-03-12 00:10:50 +08002741 list_move(&anon->d_u.d_child, &dparent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002742
Nick Piggin31e6b012011-01-07 17:49:52 +11002743 write_seqcount_end(&dentry->d_seq);
2744 write_seqcount_end(&anon->d_seq);
2745
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002746 dentry_unlock_parents_for_move(anon, dentry);
2747 spin_unlock(&dentry->d_lock);
2748
2749 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002750 anon->d_flags &= ~DCACHE_DISCONNECTED;
2751}
2752
2753/**
2754 * d_materialise_unique - introduce an inode into the tree
2755 * @dentry: candidate dentry
2756 * @inode: inode to bind to the dentry, to which aliases may be attached
2757 *
2758 * Introduces an dentry into the tree, substituting an extant disconnected
Jeff Laytonc46c8872011-07-26 13:33:16 -04002759 * root directory alias in its place if there is one. Caller must hold the
2760 * i_mutex of the parent directory.
David Howells770bfad2006-08-22 20:06:07 -04002761 */
2762struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2763{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002764 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002765
2766 BUG_ON(!d_unhashed(dentry));
2767
David Howells770bfad2006-08-22 20:06:07 -04002768 if (!inode) {
2769 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002770 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002771 d_rehash(actual);
2772 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002773 }
2774
Nick Piggin873feea2011-01-07 17:50:06 +11002775 spin_lock(&inode->i_lock);
Nick Piggin357f8e62011-01-07 17:49:42 +11002776
Trond Myklebust9eaef272006-10-21 10:24:20 -07002777 if (S_ISDIR(inode->i_mode)) {
2778 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002779
Trond Myklebust9eaef272006-10-21 10:24:20 -07002780 /* Does an aliased dentry already exist? */
Linus Torvalds32ba9c32012-06-08 10:34:03 -07002781 alias = __d_find_alias(inode, 0);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002782 if (alias) {
2783 actual = alias;
Al Viro18367502011-07-12 21:42:24 -04002784 write_seqlock(&rename_lock);
2785
2786 if (d_ancestor(alias, dentry)) {
2787 /* Check for loops */
2788 actual = ERR_PTR(-ELOOP);
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002789 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04002790 } else if (IS_ROOT(alias)) {
2791 /* Is this an anonymous mountpoint that we
2792 * could splice into our tree? */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002793 __d_materialise_dentry(dentry, alias);
Al Viro18367502011-07-12 21:42:24 -04002794 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002795 __d_drop(alias);
2796 goto found;
Al Viro18367502011-07-12 21:42:24 -04002797 } else {
2798 /* Nope, but we must(!) avoid directory
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002799 * aliasing. This drops inode->i_lock */
Al Viro18367502011-07-12 21:42:24 -04002800 actual = __d_unalias(inode, dentry, alias);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002801 }
Al Viro18367502011-07-12 21:42:24 -04002802 write_sequnlock(&rename_lock);
David Howellsdd179942011-08-16 15:31:30 +01002803 if (IS_ERR(actual)) {
2804 if (PTR_ERR(actual) == -ELOOP)
2805 pr_warn_ratelimited(
2806 "VFS: Lookup of '%s' in %s %s"
2807 " would have caused loop\n",
2808 dentry->d_name.name,
2809 inode->i_sb->s_type->name,
2810 inode->i_sb->s_id);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002811 dput(alias);
David Howellsdd179942011-08-16 15:31:30 +01002812 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002813 goto out_nolock;
2814 }
David Howells770bfad2006-08-22 20:06:07 -04002815 }
2816
2817 /* Add a unique reference */
2818 actual = __d_instantiate_unique(dentry, inode);
2819 if (!actual)
2820 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002821 else
2822 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002823
David Howells770bfad2006-08-22 20:06:07 -04002824 spin_lock(&actual->d_lock);
2825found:
2826 _d_rehash(actual);
2827 spin_unlock(&actual->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002828 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002829out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002830 if (actual == dentry) {
2831 security_d_instantiate(dentry, inode);
2832 return NULL;
2833 }
2834
2835 iput(inode);
2836 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002837}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002838EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002839
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002840static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002841{
2842 *buflen -= namelen;
2843 if (*buflen < 0)
2844 return -ENAMETOOLONG;
2845 *buffer -= namelen;
2846 memcpy(*buffer, str, namelen);
2847 return 0;
2848}
2849
Waiman Long232d2d62013-09-09 12:18:13 -04002850/**
2851 * prepend_name - prepend a pathname in front of current buffer pointer
Waiman Long18129972013-09-12 10:55:35 -04002852 * @buffer: buffer pointer
2853 * @buflen: allocated length of the buffer
2854 * @name: name string and length qstr structure
Waiman Long232d2d62013-09-09 12:18:13 -04002855 *
2856 * With RCU path tracing, it may race with d_move(). Use ACCESS_ONCE() to
2857 * make sure that either the old or the new name pointer and length are
2858 * fetched. However, there may be mismatch between length and pointer.
2859 * The length cannot be trusted, we need to copy it byte-by-byte until
2860 * the length is reached or a null byte is found. It also prepends "/" at
2861 * the beginning of the name. The sequence number check at the caller will
2862 * retry it again when a d_move() does happen. So any garbage in the buffer
2863 * due to mismatched pointer and length will be discarded.
2864 */
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002865static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2866{
Waiman Long232d2d62013-09-09 12:18:13 -04002867 const char *dname = ACCESS_ONCE(name->name);
2868 u32 dlen = ACCESS_ONCE(name->len);
2869 char *p;
2870
2871 if (*buflen < dlen + 1)
2872 return -ENAMETOOLONG;
2873 *buflen -= dlen + 1;
2874 p = *buffer -= dlen + 1;
2875 *p++ = '/';
2876 while (dlen--) {
2877 char c = *dname++;
2878 if (!c)
2879 break;
2880 *p++ = c;
2881 }
2882 return 0;
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/**
Randy Dunlap208898c2010-11-18 15:02:49 -08002886 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002887 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002888 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002889 * @buffer: pointer to the end of the buffer
2890 * @buflen: pointer to buffer length
2891 *
Waiman Long18129972013-09-12 10:55:35 -04002892 * The function will first try to write out the pathname without taking any
2893 * lock other than the RCU read lock to make sure that dentries won't go away.
2894 * It only checks the sequence number of the global rename_lock as any change
2895 * in the dentry's d_seq will be preceded by changes in the rename_lock
2896 * sequence number. If the sequence number had been changed, it will restart
2897 * the whole pathname back-tracing sequence again by taking the rename_lock.
2898 * In this case, there is no need to take the RCU read lock as the recursive
2899 * parent pointer references will keep the dentry chain alive as long as no
2900 * rename operation is performed.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002901 */
Al Viro02125a82011-12-05 08:43:34 -05002902static int prepend_path(const struct path *path,
2903 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002904 char **buffer, int *buflen)
2905{
2906 struct dentry *dentry = path->dentry;
2907 struct vfsmount *vfsmnt = path->mnt;
Al Viro0714a532011-11-24 22:19:58 -05002908 struct mount *mnt = real_mount(vfsmnt);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002909 int error = 0;
Al Viro48a066e2013-09-29 22:06:07 -04002910 unsigned seq, m_seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002911 char *bptr;
2912 int blen;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002913
Al Viro48f5ec22013-09-09 15:22:25 -04002914 rcu_read_lock();
Al Viro48a066e2013-09-29 22:06:07 -04002915restart_mnt:
2916 read_seqbegin_or_lock(&mount_lock, &m_seq);
2917 seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002918restart:
2919 bptr = *buffer;
2920 blen = *buflen;
Al Viro48a066e2013-09-29 22:06:07 -04002921 error = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002922 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002923 while (dentry != root->dentry || vfsmnt != root->mnt) {
2924 struct dentry * parent;
2925
2926 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Al Viro48a066e2013-09-29 22:06:07 -04002927 struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002928 /* Global root? */
Al Viro48a066e2013-09-29 22:06:07 -04002929 if (mnt != parent) {
2930 dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
2931 mnt = parent;
Waiman Long232d2d62013-09-09 12:18:13 -04002932 vfsmnt = &mnt->mnt;
2933 continue;
2934 }
2935 /*
2936 * Filesystems needing to implement special "root names"
2937 * should do so with ->d_dname()
2938 */
2939 if (IS_ROOT(dentry) &&
2940 (dentry->d_name.len != 1 ||
2941 dentry->d_name.name[0] != '/')) {
2942 WARN(1, "Root dentry has weird name <%.*s>\n",
2943 (int) dentry->d_name.len,
2944 dentry->d_name.name);
2945 }
2946 if (!error)
2947 error = is_mounted(vfsmnt) ? 1 : 2;
2948 break;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002949 }
2950 parent = dentry->d_parent;
2951 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04002952 error = prepend_name(&bptr, &blen, &dentry->d_name);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002953 if (error)
2954 break;
2955
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002956 dentry = parent;
2957 }
Al Viro48f5ec22013-09-09 15:22:25 -04002958 if (!(seq & 1))
2959 rcu_read_unlock();
2960 if (need_seqretry(&rename_lock, seq)) {
2961 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04002962 goto restart;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002963 }
Al Viro48f5ec22013-09-09 15:22:25 -04002964 done_seqretry(&rename_lock, seq);
Al Viro48a066e2013-09-29 22:06:07 -04002965 if (need_seqretry(&mount_lock, m_seq)) {
2966 m_seq = 1;
2967 goto restart_mnt;
2968 }
2969 done_seqretry(&mount_lock, m_seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002970
Waiman Long232d2d62013-09-09 12:18:13 -04002971 if (error >= 0 && bptr == *buffer) {
2972 if (--blen < 0)
2973 error = -ENAMETOOLONG;
2974 else
2975 *--bptr = '/';
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002976 }
Waiman Long232d2d62013-09-09 12:18:13 -04002977 *buffer = bptr;
2978 *buflen = blen;
Al Viro7ea600b2013-03-26 18:25:57 -04002979 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002980}
2981
2982/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002983 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002984 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002985 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07002986 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * @buflen: buffer length
2988 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002989 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002991 * Returns a pointer into the buffer or an error code if the
2992 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002993 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002994 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002995 *
Al Viro02125a82011-12-05 08:43:34 -05002996 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 */
Al Viro02125a82011-12-05 08:43:34 -05002998char *__d_path(const struct path *path,
2999 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003000 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003002 char *res = buf + buflen;
3003 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003005 prepend(&res, &buflen, "\0", 1);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003006 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04003007
Al Viro02125a82011-12-05 08:43:34 -05003008 if (error < 0)
3009 return ERR_PTR(error);
3010 if (error > 0)
3011 return NULL;
3012 return res;
3013}
3014
3015char *d_absolute_path(const struct path *path,
3016 char *buf, int buflen)
3017{
3018 struct path root = {};
3019 char *res = buf + buflen;
3020 int error;
3021
3022 prepend(&res, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003023 error = prepend_path(path, &root, &res, &buflen);
Al Viro02125a82011-12-05 08:43:34 -05003024
3025 if (error > 1)
3026 error = -EINVAL;
3027 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003028 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003029 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003032/*
3033 * same as __d_path but appends "(deleted)" for unlinked files.
3034 */
Al Viro02125a82011-12-05 08:43:34 -05003035static int path_with_deleted(const struct path *path,
3036 const struct path *root,
3037 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003038{
3039 prepend(buf, buflen, "\0", 1);
3040 if (d_unlinked(path->dentry)) {
3041 int error = prepend(buf, buflen, " (deleted)", 10);
3042 if (error)
3043 return error;
3044 }
3045
3046 return prepend_path(path, root, buf, buflen);
3047}
3048
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003049static int prepend_unreachable(char **buffer, int *buflen)
3050{
3051 return prepend(buffer, buflen, "(unreachable)", 13);
3052}
3053
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003054static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3055{
3056 unsigned seq;
3057
3058 do {
3059 seq = read_seqcount_begin(&fs->seq);
3060 *root = fs->root;
3061 } while (read_seqcount_retry(&fs->seq, seq));
3062}
3063
Jan Bluncka03a8a702008-02-14 19:38:32 -08003064/**
3065 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08003066 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08003067 * @buf: buffer to return value in
3068 * @buflen: buffer length
3069 *
3070 * Convert a dentry into an ASCII path name. If the entry has been deleted
3071 * the string " (deleted)" is appended. Note that this is ambiguous.
3072 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003073 * Returns a pointer into the buffer or an error code if the path was
3074 * too long. Note: Callers should use the returned pointer, not the passed
3075 * in buffer, to use the name! The implementation often starts at an offset
3076 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003077 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003078 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003079 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07003080char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003082 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003083 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003084 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003086 /*
3087 * We have various synthetic filesystems that never get mounted. On
3088 * these filesystems dentries are never used for lookup purposes, and
3089 * thus don't need to be hashed. They also don't need a name until a
3090 * user wants to identify the object in /proc/pid/fd/. The little hack
3091 * below allows us to generate a name for these objects on demand:
3092 */
Jan Blunckcf28b482008-02-14 19:38:44 -08003093 if (path->dentry->d_op && path->dentry->d_op->d_dname)
3094 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003095
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003096 rcu_read_lock();
3097 get_fs_root_rcu(current->fs, &root);
Al Viro02125a82011-12-05 08:43:34 -05003098 error = path_with_deleted(path, &root, &res, &buflen);
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003099 rcu_read_unlock();
3100
Al Viro02125a82011-12-05 08:43:34 -05003101 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003102 res = ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return res;
3104}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003105EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003108 * Helper function for dentry_operations.d_dname() members
3109 */
3110char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3111 const char *fmt, ...)
3112{
3113 va_list args;
3114 char temp[64];
3115 int sz;
3116
3117 va_start(args, fmt);
3118 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3119 va_end(args);
3120
3121 if (sz > sizeof(temp) || sz > buflen)
3122 return ERR_PTR(-ENAMETOOLONG);
3123
3124 buffer += buflen - sz;
3125 return memcpy(buffer, temp, sz);
3126}
3127
Al Viro118b2302013-08-24 12:08:17 -04003128char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3129{
3130 char *end = buffer + buflen;
3131 /* these dentries are never renamed, so d_lock is not needed */
3132 if (prepend(&end, &buflen, " (deleted)", 11) ||
Waiman Long232d2d62013-09-09 12:18:13 -04003133 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
Al Viro118b2302013-08-24 12:08:17 -04003134 prepend(&end, &buflen, "/", 1))
3135 end = ERR_PTR(-ENAMETOOLONG);
Waiman Long232d2d62013-09-09 12:18:13 -04003136 return end;
Al Viro118b2302013-08-24 12:08:17 -04003137}
3138
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003139/*
Ram Pai6092d042008-03-27 13:06:20 +01003140 * Write full pathname from the root of the filesystem into the buffer.
3141 */
Nick Pigginec2447c2011-01-07 17:49:29 +11003142static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01003143{
Waiman Long232d2d62013-09-09 12:18:13 -04003144 char *end, *retval;
3145 int len, seq = 0;
3146 int error = 0;
Ram Pai6092d042008-03-27 13:06:20 +01003147
Al Viro48f5ec22013-09-09 15:22:25 -04003148 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003149restart:
3150 end = buf + buflen;
3151 len = buflen;
3152 prepend(&end, &len, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01003153 if (buflen < 1)
3154 goto Elong;
3155 /* Get '/' right */
3156 retval = end-1;
3157 *retval = '/';
Waiman Long232d2d62013-09-09 12:18:13 -04003158 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003159 while (!IS_ROOT(dentry)) {
3160 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11003161 int error;
Ram Pai6092d042008-03-27 13:06:20 +01003162
Ram Pai6092d042008-03-27 13:06:20 +01003163 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003164 error = prepend_name(&end, &len, &dentry->d_name);
3165 if (error)
3166 break;
Ram Pai6092d042008-03-27 13:06:20 +01003167
3168 retval = end;
3169 dentry = parent;
3170 }
Al Viro48f5ec22013-09-09 15:22:25 -04003171 if (!(seq & 1))
3172 rcu_read_unlock();
3173 if (need_seqretry(&rename_lock, seq)) {
3174 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003175 goto restart;
Al Viro48f5ec22013-09-09 15:22:25 -04003176 }
3177 done_seqretry(&rename_lock, seq);
Waiman Long232d2d62013-09-09 12:18:13 -04003178 if (error)
3179 goto Elong;
Al Viroc1031352010-06-06 22:31:14 -04003180 return retval;
3181Elong:
3182 return ERR_PTR(-ENAMETOOLONG);
3183}
Nick Pigginec2447c2011-01-07 17:49:29 +11003184
3185char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3186{
Waiman Long232d2d62013-09-09 12:18:13 -04003187 return __dentry_path(dentry, buf, buflen);
Nick Pigginec2447c2011-01-07 17:49:29 +11003188}
3189EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04003190
3191char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3192{
3193 char *p = NULL;
3194 char *retval;
3195
Al Viroc1031352010-06-06 22:31:14 -04003196 if (d_unlinked(dentry)) {
3197 p = buf + buflen;
3198 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3199 goto Elong;
3200 buflen++;
3201 }
3202 retval = __dentry_path(dentry, buf, buflen);
Al Viroc1031352010-06-06 22:31:14 -04003203 if (!IS_ERR(retval) && p)
3204 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01003205 return retval;
3206Elong:
Ram Pai6092d042008-03-27 13:06:20 +01003207 return ERR_PTR(-ENAMETOOLONG);
3208}
3209
Linus Torvalds8b19e342013-09-12 10:35:47 -07003210static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3211 struct path *pwd)
Linus Torvalds57624822013-09-12 10:12:47 -07003212{
Linus Torvalds8b19e342013-09-12 10:35:47 -07003213 unsigned seq;
3214
3215 do {
3216 seq = read_seqcount_begin(&fs->seq);
3217 *root = fs->root;
3218 *pwd = fs->pwd;
3219 } while (read_seqcount_retry(&fs->seq, seq));
Linus Torvalds57624822013-09-12 10:12:47 -07003220}
3221
Ram Pai6092d042008-03-27 13:06:20 +01003222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 * NOTE! The user-level library version returns a
3224 * character pointer. The kernel system call just
3225 * returns the length of the buffer filled (which
3226 * includes the ending '\0' character), or a negative
3227 * error value. So libc would do something like
3228 *
3229 * char *getcwd(char * buf, size_t size)
3230 * {
3231 * int retval;
3232 *
3233 * retval = sys_getcwd(buf, size);
3234 * if (retval >= 0)
3235 * return buf;
3236 * errno = -retval;
3237 * return NULL;
3238 * }
3239 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01003240SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Linus Torvalds552ce542007-02-13 12:08:18 -08003242 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003243 struct path pwd, root;
Linus Torvalds3272c542013-09-12 12:40:15 -07003244 char *page = __getname();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 if (!page)
3247 return -ENOMEM;
3248
Linus Torvalds8b19e342013-09-12 10:35:47 -07003249 rcu_read_lock();
3250 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Linus Torvalds552ce542007-02-13 12:08:18 -08003252 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003253 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08003254 unsigned long len;
Linus Torvalds3272c542013-09-12 12:40:15 -07003255 char *cwd = page + PATH_MAX;
3256 int buflen = PATH_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003258 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003259 error = prepend_path(&pwd, &root, &cwd, &buflen);
Linus Torvaldsff812d72013-09-12 11:57:01 -07003260 rcu_read_unlock();
Linus Torvalds552ce542007-02-13 12:08:18 -08003261
Al Viro02125a82011-12-05 08:43:34 -05003262 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08003263 goto out;
3264
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003265 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05003266 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003267 error = prepend_unreachable(&cwd, &buflen);
3268 if (error)
3269 goto out;
3270 }
3271
Linus Torvalds552ce542007-02-13 12:08:18 -08003272 error = -ERANGE;
Linus Torvalds3272c542013-09-12 12:40:15 -07003273 len = PATH_MAX + page - cwd;
Linus Torvalds552ce542007-02-13 12:08:18 -08003274 if (len <= size) {
3275 error = len;
3276 if (copy_to_user(buf, cwd, len))
3277 error = -EFAULT;
3278 }
Nick Piggin949854d2011-01-07 17:49:37 +11003279 } else {
Linus Torvaldsff812d72013-09-12 11:57:01 -07003280 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +11003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283out:
Linus Torvalds3272c542013-09-12 12:40:15 -07003284 __putname(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return error;
3286}
3287
3288/*
3289 * Test whether new_dentry is a subdirectory of old_dentry.
3290 *
3291 * Trivially implemented using the dcache structure
3292 */
3293
3294/**
3295 * is_subdir - is new dentry a subdirectory of old_dentry
3296 * @new_dentry: new dentry
3297 * @old_dentry: old dentry
3298 *
3299 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
3300 * Returns 0 otherwise.
3301 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3302 */
3303
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003304int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11003307 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003309 if (new_dentry == old_dentry)
3310 return 1;
3311
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003312 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003315 /*
3316 * Need rcu_readlock to protect against the d_parent trashing
3317 * due to d_move
3318 */
3319 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003320 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003322 else
3323 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11003324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
3327 return result;
3328}
3329
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003330static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003332 struct dentry *root = data;
3333 if (dentry != root) {
3334 if (d_unhashed(dentry) || !dentry->d_inode)
3335 return D_WALK_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Miklos Szeredi01ddc4e2013-09-05 11:44:34 +02003337 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3338 dentry->d_flags |= DCACHE_GENOCIDE;
3339 dentry->d_lockref.count--;
3340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003342 return D_WALK_CONTINUE;
3343}
Nick Piggin58db63d2011-01-07 17:49:39 +11003344
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003345void d_genocide(struct dentry *parent)
3346{
3347 d_walk(parent, parent, d_genocide_kill, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
Al Viro60545d02013-06-07 01:20:27 -04003350void d_tmpfile(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Al Viro60545d02013-06-07 01:20:27 -04003352 inode_dec_link_count(inode);
3353 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3354 !hlist_unhashed(&dentry->d_alias) ||
3355 !d_unlinked(dentry));
3356 spin_lock(&dentry->d_parent->d_lock);
3357 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3358 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3359 (unsigned long long)inode->i_ino);
3360 spin_unlock(&dentry->d_lock);
3361 spin_unlock(&dentry->d_parent->d_lock);
3362 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
Al Viro60545d02013-06-07 01:20:27 -04003364EXPORT_SYMBOL(d_tmpfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366static __initdata unsigned long dhash_entries;
3367static int __init set_dhash_entries(char *str)
3368{
3369 if (!str)
3370 return 0;
3371 dhash_entries = simple_strtoul(str, &str, 0);
3372 return 1;
3373}
3374__setup("dhash_entries=", set_dhash_entries);
3375
3376static void __init dcache_init_early(void)
3377{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003378 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 /* If hashes are distributed across NUMA nodes, defer
3381 * hash allocation until vmalloc space is available.
3382 */
3383 if (hashdist)
3384 return;
3385
3386 dentry_hashtable =
3387 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003388 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 dhash_entries,
3390 13,
3391 HASH_EARLY,
3392 &d_hash_shift,
3393 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003394 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 0);
3396
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003397 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003398 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Denis Cheng74bf17c2007-10-16 23:26:30 -07003401static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003403 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405 /*
3406 * A constructor could be added for stable state like the lists,
3407 * but it is probably not worth it because of the cache nature
3408 * of the dcache.
3409 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003410 dentry_cache = KMEM_CACHE(dentry,
3411 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413 /* Hash may have been set up in dcache_init_early */
3414 if (!hashdist)
3415 return;
3416
3417 dentry_hashtable =
3418 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003419 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 dhash_entries,
3421 13,
3422 0,
3423 &d_hash_shift,
3424 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003425 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 0);
3427
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003428 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003429 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003433struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003434EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436EXPORT_SYMBOL(d_genocide);
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438void __init vfs_caches_init_early(void)
3439{
3440 dcache_init_early();
3441 inode_init_early();
3442}
3443
3444void __init vfs_caches_init(unsigned long mempages)
3445{
3446 unsigned long reserve;
3447
3448 /* Base hash sizes on available memory, with a reserve equal to
3449 150% of current kernel size */
3450
3451 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3452 mempages -= reserve;
3453
3454 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003455 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Denis Cheng74bf17c2007-10-16 23:26:30 -07003457 dcache_init();
3458 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07003460 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 bdev_cache_init();
3462 chrdev_init();
3463}