blob: fb7bcf3ba5d6d704379b066ad7e50455f0578360 [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;
346 dentry->d_inode = NULL;
Al Virob3d9b7a2012-06-09 13:51:19 -0400347 hlist_del_init(&dentry->d_alias);
Nick Piggin31e6b012011-01-07 17:49:52 +1100348 dentry_rcuwalk_barrier(dentry);
349 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100350 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100351 if (!inode->i_nlink)
352 fsnotify_inoderemove(inode);
353 if (dentry->d_op && dentry->d_op->d_iput)
354 dentry->d_op->d_iput(dentry, inode);
355 else
356 iput(inode);
357}
358
359/*
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400360 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
361 * is in use - which includes both the "real" per-superblock
362 * LRU list _and_ the DCACHE_SHRINK_LIST use.
363 *
364 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
365 * on the shrink list (ie not on the superblock LRU list).
366 *
367 * The per-cpu "nr_dentry_unused" counters are updated with
368 * the DCACHE_LRU_LIST bit.
369 *
370 * These helper functions make sure we always follow the
371 * rules. d_lock must be held by the caller.
372 */
373#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
374static void d_lru_add(struct dentry *dentry)
375{
376 D_FLAG_VERIFY(dentry, 0);
377 dentry->d_flags |= DCACHE_LRU_LIST;
378 this_cpu_inc(nr_dentry_unused);
379 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
380}
381
382static void d_lru_del(struct dentry *dentry)
383{
384 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
385 dentry->d_flags &= ~DCACHE_LRU_LIST;
386 this_cpu_dec(nr_dentry_unused);
387 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
388}
389
390static void d_shrink_del(struct dentry *dentry)
391{
392 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
393 list_del_init(&dentry->d_lru);
394 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
395 this_cpu_dec(nr_dentry_unused);
396}
397
398static void d_shrink_add(struct dentry *dentry, struct list_head *list)
399{
400 D_FLAG_VERIFY(dentry, 0);
401 list_add(&dentry->d_lru, list);
402 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
403 this_cpu_inc(nr_dentry_unused);
404}
405
406/*
407 * These can only be called under the global LRU lock, ie during the
408 * callback for freeing the LRU list. "isolate" removes it from the
409 * LRU lists entirely, while shrink_move moves it to the indicated
410 * private list.
411 */
412static void d_lru_isolate(struct dentry *dentry)
413{
414 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
415 dentry->d_flags &= ~DCACHE_LRU_LIST;
416 this_cpu_dec(nr_dentry_unused);
417 list_del_init(&dentry->d_lru);
418}
419
420static void d_lru_shrink_move(struct dentry *dentry, struct list_head *list)
421{
422 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
423 dentry->d_flags |= DCACHE_SHRINK_LIST;
424 list_move_tail(&dentry->d_lru, list);
425}
426
427/*
Dave Chinnerf6041562013-08-28 10:18:00 +1000428 * dentry_lru_(add|del)_list) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700429 */
430static void dentry_lru_add(struct dentry *dentry)
431{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400432 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
433 d_lru_add(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700434}
435
Sage Weilf0023bc2011-10-28 10:02:42 -0700436/*
437 * Remove a dentry with references from the LRU.
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000438 *
439 * If we are on the shrink list, then we can get to try_prune_one_dentry() and
440 * lose our last reference through the parent walk. In this case, we need to
441 * remove ourselves from the shrink list, not the LRU.
Sage Weilf0023bc2011-10-28 10:02:42 -0700442 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700443static void dentry_lru_del(struct dentry *dentry)
444{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400445 if (dentry->d_flags & DCACHE_LRU_LIST) {
446 if (dentry->d_flags & DCACHE_SHRINK_LIST)
447 return d_shrink_del(dentry);
448 d_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700449 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700450}
451
Miklos Szeredid52b9082007-05-08 00:23:46 -0700452/**
453 * d_kill - kill dentry and return parent
454 * @dentry: dentry to kill
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800455 * @parent: parent dentry
Miklos Szeredid52b9082007-05-08 00:23:46 -0700456 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200457 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700458 *
459 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100460 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100461 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
462 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700463 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100464static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200465 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100466 __releases(parent->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100467 __releases(dentry->d_inode->i_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700468{
Miklos Szeredid52b9082007-05-08 00:23:46 -0700469 list_del(&dentry->d_u.d_child);
Trond Myklebustc83ce982011-03-15 13:36:43 -0400470 /*
471 * Inform try_to_ascend() that we are no longer attached to the
472 * dentry tree
473 */
Miklos Szeredib161dfa62012-09-17 22:31:38 +0200474 dentry->d_flags |= DCACHE_DENTRY_KILLED;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100475 if (parent)
476 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700477 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100478 /*
479 * dentry_iput drops the locks, at which point nobody (except
480 * transient RCU lookups) can reach this dentry.
481 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700482 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900483 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700484}
485
Nick Piggin789680d2011-01-07 17:49:30 +1100486/**
487 * d_drop - drop a dentry
488 * @dentry: dentry to drop
489 *
490 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
491 * be found through a VFS lookup any more. Note that this is different from
492 * deleting the dentry - d_delete will try to mark the dentry negative if
493 * possible, giving a successful _negative_ lookup, while d_drop will
494 * just make the cache lookup fail.
495 *
496 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
497 * reason (NFS timeouts or autofs deletes).
498 *
499 * __d_drop requires dentry->d_lock.
500 */
501void __d_drop(struct dentry *dentry)
502{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700503 if (!d_unhashed(dentry)) {
Al Virob61625d2013-10-04 11:09:01 -0400504 struct hlist_bl_head *b;
505 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
506 b = &dentry->d_sb->s_anon;
507 else
508 b = d_hash(dentry->d_parent, dentry->d_name.hash);
509
510 hlist_bl_lock(b);
511 __hlist_bl_del(&dentry->d_hash);
512 dentry->d_hash.pprev = NULL;
513 hlist_bl_unlock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -0700514 dentry_rcuwalk_barrier(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +1100515 }
516}
517EXPORT_SYMBOL(__d_drop);
518
519void d_drop(struct dentry *dentry)
520{
Nick Piggin789680d2011-01-07 17:49:30 +1100521 spin_lock(&dentry->d_lock);
522 __d_drop(dentry);
523 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100524}
525EXPORT_SYMBOL(d_drop);
526
Nick Piggin77812a12011-01-07 17:49:48 +1100527/*
528 * Finish off a dentry we've decided to kill.
529 * dentry->d_lock must be held, returns with it unlocked.
530 * If ref is non-zero, then decrement the refcount too.
531 * Returns dentry requiring refcount drop, or NULL if we're done.
532 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000533static inline struct dentry *
534dentry_kill(struct dentry *dentry, int unlock_on_failure)
Nick Piggin77812a12011-01-07 17:49:48 +1100535 __releases(dentry->d_lock)
536{
Nick Piggin873feea2011-01-07 17:50:06 +1100537 struct inode *inode;
Nick Piggin77812a12011-01-07 17:49:48 +1100538 struct dentry *parent;
539
Nick Piggin873feea2011-01-07 17:50:06 +1100540 inode = dentry->d_inode;
541 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin77812a12011-01-07 17:49:48 +1100542relock:
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000543 if (unlock_on_failure) {
544 spin_unlock(&dentry->d_lock);
545 cpu_relax();
546 }
Nick Piggin77812a12011-01-07 17:49:48 +1100547 return dentry; /* try again with same dentry */
548 }
549 if (IS_ROOT(dentry))
550 parent = NULL;
551 else
552 parent = dentry->d_parent;
553 if (parent && !spin_trylock(&parent->d_lock)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100554 if (inode)
555 spin_unlock(&inode->i_lock);
Nick Piggin77812a12011-01-07 17:49:48 +1100556 goto relock;
557 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100558
Linus Torvalds0d984392013-09-08 13:46:52 -0700559 /*
560 * The dentry is now unrecoverably dead to the world.
561 */
562 lockref_mark_dead(&dentry->d_lockref);
563
Sage Weilf0023bc2011-10-28 10:02:42 -0700564 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700565 * inform the fs via d_prune that this dentry is about to be
566 * unhashed and destroyed.
567 */
Yan, Zheng590fb512013-08-13 15:42:02 +0800568 if ((dentry->d_flags & DCACHE_OP_PRUNE) && !d_unhashed(dentry))
Yan, Zheng61572bb2013-04-15 14:13:21 +0800569 dentry->d_op->d_prune(dentry);
570
571 dentry_lru_del(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100572 /* if it was on the hash then remove it */
573 __d_drop(dentry);
574 return d_kill(dentry, parent);
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*
578 * This is dput
579 *
580 * This is complicated by the fact that we do not want to put
581 * dentries that are no longer on any hash chain on the unused
582 * list: we'd much rather just get rid of them immediately.
583 *
584 * However, that implies that we have to traverse the dentry
585 * tree upwards to the parents which might _also_ now be
586 * scheduled for deletion (it may have been only waiting for
587 * its last child to go away).
588 *
589 * This tail recursion is done by hand as we don't want to depend
590 * on the compiler to always get this right (gcc generally doesn't).
591 * Real recursion would eat up our stack space.
592 */
593
594/*
595 * dput - release a dentry
596 * @dentry: dentry to release
597 *
598 * Release a dentry. This will drop the usage count and if appropriate
599 * call the dentry unlink method as well as removing it from the queues and
600 * releasing its resources. If the parent dentries were scheduled for release
601 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603void dput(struct dentry *dentry)
604{
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700605 if (unlikely(!dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return;
607
608repeat:
Waiman Long98474232013-08-28 18:24:59 -0700609 if (lockref_put_or_lock(&dentry->d_lockref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700612 /* Unreachable? Get rid of it */
613 if (unlikely(d_unhashed(dentry)))
614 goto kill_it;
615
616 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100618 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Nick Piggin265ac902010-10-10 05:36:24 -0400620
Jeff Layton39e3c952012-11-28 11:30:53 -0500621 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400622 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400623
Waiman Long98474232013-08-28 18:24:59 -0700624 dentry->d_lockref.count--;
Nick Piggin61f3dee2011-01-07 17:49:40 +1100625 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return;
627
Miklos Szeredid52b9082007-05-08 00:23:46 -0700628kill_it:
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000629 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700630 if (dentry)
631 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700633EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/**
636 * d_invalidate - invalidate a dentry
637 * @dentry: dentry to invalidate
638 *
639 * Try to invalidate the dentry if it turns out to be
640 * possible. If there are other dentries that can be
641 * reached through this one we can't delete it and we
642 * return -EBUSY. On success we return 0.
643 *
644 * no dcache lock.
645 */
646
647int d_invalidate(struct dentry * dentry)
648{
649 /*
650 * If it's already been dropped, return OK.
651 */
Nick Pigginda502952011-01-07 17:49:33 +1100652 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100654 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656 }
657 /*
658 * Check whether to do a partial shrink_dcache
659 * to get rid of unused child entries.
660 */
661 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100662 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100664 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 /*
668 * Somebody else still using it?
669 *
670 * If it's a directory, we can't drop it
671 * for fear of somebody re-populating it
672 * with children (even though dropping it
673 * would make it unreachable from the root,
674 * we might still populate it if it was a
675 * working directory or similar).
Al Viro50e69632011-11-07 16:39:57 +0000676 * We also need to leave mountpoints alone,
677 * directory or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Waiman Long98474232013-08-28 18:24:59 -0700679 if (dentry->d_lockref.count > 1 && dentry->d_inode) {
Al Viro50e69632011-11-07 16:39:57 +0000680 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EBUSY;
683 }
684 }
685
686 __d_drop(dentry);
687 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700690EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100692/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100693static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Waiman Long98474232013-08-28 18:24:59 -0700695 dentry->d_lockref.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Nick Piggindc0474b2011-01-07 17:49:43 +1100698static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100699{
Waiman Long98474232013-08-28 18:24:59 -0700700 lockref_get(&dentry->d_lockref);
Nick Piggin23044502011-01-07 17:49:31 +1100701}
702
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100703struct dentry *dget_parent(struct dentry *dentry)
704{
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700705 int gotref;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100706 struct dentry *ret;
707
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700708 /*
709 * Do optimistic parent lookup without any
710 * locking.
711 */
712 rcu_read_lock();
713 ret = ACCESS_ONCE(dentry->d_parent);
714 gotref = lockref_get_not_zero(&ret->d_lockref);
715 rcu_read_unlock();
716 if (likely(gotref)) {
717 if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
718 return ret;
719 dput(ret);
720 }
721
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100722repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100723 /*
724 * Don't need rcu_dereference because we re-check it was correct under
725 * the lock.
726 */
727 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100728 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100729 spin_lock(&ret->d_lock);
730 if (unlikely(ret != dentry->d_parent)) {
731 spin_unlock(&ret->d_lock);
732 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100733 goto repeat;
734 }
Nick Piggina734eb42011-01-07 17:49:44 +1100735 rcu_read_unlock();
Waiman Long98474232013-08-28 18:24:59 -0700736 BUG_ON(!ret->d_lockref.count);
737 ret->d_lockref.count++;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100738 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100739 return ret;
740}
741EXPORT_SYMBOL(dget_parent);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/**
744 * d_find_alias - grab a hashed alias of inode
745 * @inode: inode in question
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700746 * @want_discon: flag, used by d_splice_alias, to request
747 * that only a DISCONNECTED alias be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
749 * If inode has a hashed alias, or is a directory and has any alias,
750 * acquire the reference to alias and return it. Otherwise return NULL.
751 * Notice that if inode is a directory there can be only one alias and
752 * it can be unhashed only if it has no children, or if it is the root
753 * of a filesystem.
754 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700755 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700756 * any other hashed alias over that one unless @want_discon is set,
757 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700759static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Nick Pigginda502952011-01-07 17:49:33 +1100761 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Nick Pigginda502952011-01-07 17:49:33 +1100763again:
764 discon_alias = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800765 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100766 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700768 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100769 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 discon_alias = alias;
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700771 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100772 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100773 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return alias;
775 }
776 }
Nick Pigginda502952011-01-07 17:49:33 +1100777 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Nick Pigginda502952011-01-07 17:49:33 +1100779 if (discon_alias) {
780 alias = discon_alias;
781 spin_lock(&alias->d_lock);
782 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
783 if (IS_ROOT(alias) &&
784 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100785 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100786 spin_unlock(&alias->d_lock);
787 return alias;
788 }
789 }
790 spin_unlock(&alias->d_lock);
791 goto again;
792 }
793 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Nick Pigginda502952011-01-07 17:49:33 +1100796struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
David Howells214fda12006-03-25 03:06:36 -0800798 struct dentry *de = NULL;
799
Al Virob3d9b7a2012-06-09 13:51:19 -0400800 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100801 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700802 de = __d_find_alias(inode, 0);
Nick Piggin873feea2011-01-07 17:50:06 +1100803 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return de;
806}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700807EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/*
810 * Try to kill dentries associated with this inode.
811 * WARNING: you must own a reference to inode.
812 */
813void d_prune_aliases(struct inode *inode)
814{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700815 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100817 spin_lock(&inode->i_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800818 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -0700820 if (!dentry->d_lockref.count) {
Yan, Zheng590fb512013-08-13 15:42:02 +0800821 /*
822 * inform the fs via d_prune that this dentry
823 * is about to be unhashed and destroyed.
824 */
825 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
826 !d_unhashed(dentry))
827 dentry->d_op->d_prune(dentry);
828
Nick Piggindc0474b2011-01-07 17:49:43 +1100829 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 __d_drop(dentry);
831 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100832 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 dput(dentry);
834 goto restart;
835 }
836 spin_unlock(&dentry->d_lock);
837 }
Nick Piggin873feea2011-01-07 17:50:06 +1100838 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700840EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842/*
Nick Piggin77812a12011-01-07 17:49:48 +1100843 * Try to throw away a dentry - free the inode, dput the parent.
844 * Requires dentry->d_lock is held, and dentry->d_count == 0.
845 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700846 *
Nick Piggin77812a12011-01-07 17:49:48 +1100847 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000849static struct dentry * try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200850 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Nick Piggin77812a12011-01-07 17:49:48 +1100852 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700853
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000854 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700855 /*
Nick Piggin77812a12011-01-07 17:49:48 +1100856 * If dentry_kill returns NULL, we have nothing more to do.
857 * if it returns the same dentry, trylocks failed. In either
858 * case, just loop again.
859 *
860 * Otherwise, we need to prune ancestors too. This is necessary
861 * to prevent quadratic behavior of shrink_dcache_parent(), but
862 * is also expected to be beneficial in reducing dentry cache
863 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700864 */
Nick Piggin77812a12011-01-07 17:49:48 +1100865 if (!parent)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000866 return NULL;
Nick Piggin77812a12011-01-07 17:49:48 +1100867 if (parent == dentry)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000868 return dentry;
Nick Piggin77812a12011-01-07 17:49:48 +1100869
870 /* Prune ancestors. */
871 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700872 while (dentry) {
Waiman Long98474232013-08-28 18:24:59 -0700873 if (lockref_put_or_lock(&dentry->d_lockref))
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000874 return NULL;
875 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700876 }
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000877 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400880static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700882 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700883
Nick Pigginec336792011-01-07 17:49:47 +1100884 rcu_read_lock();
885 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100886 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
887 if (&dentry->d_lru == list)
888 break; /* empty */
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400889
890 /*
891 * Get the dentry lock, and re-verify that the dentry is
892 * this on the shrinking list. If it is, we know that
893 * DCACHE_SHRINK_LIST and DCACHE_LRU_LIST are set.
894 */
Nick Pigginec336792011-01-07 17:49:47 +1100895 spin_lock(&dentry->d_lock);
896 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
897 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100898 continue;
899 }
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000902 * The dispose list is isolated and dentries are not accounted
903 * to the LRU here, so we can simply remove it from the list
904 * here regardless of whether it is referenced or not.
905 */
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400906 d_shrink_del(dentry);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000907
908 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * We found an inuse dentry which was not removed from
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000910 * the LRU because of laziness during lookup. Do not free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Waiman Long98474232013-08-28 18:24:59 -0700912 if (dentry->d_lockref.count) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700913 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 continue;
915 }
Nick Pigginec336792011-01-07 17:49:47 +1100916 rcu_read_unlock();
Nick Piggin77812a12011-01-07 17:49:48 +1100917
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400918 /*
919 * If 'try_to_prune()' returns a dentry, it will
920 * be the same one we passed in, and d_lock will
921 * have been held the whole time, so it will not
922 * have been added to any other lists. We failed
923 * to get the inode lock.
924 *
925 * We just add it back to the shrink list.
926 */
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000927 dentry = try_prune_one_dentry(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100928
Nick Pigginec336792011-01-07 17:49:47 +1100929 rcu_read_lock();
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000930 if (dentry) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400931 d_shrink_add(dentry, list);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000932 spin_unlock(&dentry->d_lock);
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Nick Pigginec336792011-01-07 17:49:47 +1100935 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400936}
937
Dave Chinnerf6041562013-08-28 10:18:00 +1000938static enum lru_status
939dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)
940{
941 struct list_head *freeable = arg;
942 struct dentry *dentry = container_of(item, struct dentry, d_lru);
943
944
945 /*
946 * we are inverting the lru lock/dentry->d_lock here,
947 * so use a trylock. If we fail to get the lock, just skip
948 * it
949 */
950 if (!spin_trylock(&dentry->d_lock))
951 return LRU_SKIP;
952
953 /*
954 * Referenced dentries are still in use. If they have active
955 * counts, just remove them from the LRU. Otherwise give them
956 * another pass through the LRU.
957 */
958 if (dentry->d_lockref.count) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400959 d_lru_isolate(dentry);
Dave Chinnerf6041562013-08-28 10:18:00 +1000960 spin_unlock(&dentry->d_lock);
961 return LRU_REMOVED;
962 }
963
964 if (dentry->d_flags & DCACHE_REFERENCED) {
965 dentry->d_flags &= ~DCACHE_REFERENCED;
966 spin_unlock(&dentry->d_lock);
967
968 /*
969 * The list move itself will be made by the common LRU code. At
970 * this point, we've dropped the dentry->d_lock but keep the
971 * lru lock. This is safe to do, since every list movement is
972 * protected by the lru lock even if both locks are held.
973 *
974 * This is guaranteed by the fact that all LRU management
975 * functions are intermediated by the LRU API calls like
976 * list_lru_add and list_lru_del. List movement in this file
977 * only ever occur through this functions or through callbacks
978 * like this one, that are called from the LRU API.
979 *
980 * The only exceptions to this are functions like
981 * shrink_dentry_list, and code that first checks for the
982 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
983 * operating only with stack provided lists after they are
984 * properly isolated from the main list. It is thus, always a
985 * local access.
986 */
987 return LRU_ROTATE;
988 }
989
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400990 d_lru_shrink_move(dentry, freeable);
Dave Chinnerf6041562013-08-28 10:18:00 +1000991 spin_unlock(&dentry->d_lock);
992
993 return LRU_REMOVED;
994}
995
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400996/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000997 * prune_dcache_sb - shrink the dcache
998 * @sb: superblock
Dave Chinnerf6041562013-08-28 10:18:00 +1000999 * @nr_to_scan : number of entries to try to free
Dave Chinner9b17c622013-08-28 10:18:05 +10001000 * @nid: which node to scan for freeable entities
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001001 *
Dave Chinnerf6041562013-08-28 10:18:00 +10001002 * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001003 * done when we need more memory an called from the superblock shrinker
1004 * function.
1005 *
1006 * This function may fail to free any resources if all the dentries are in
1007 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001008 */
Dave Chinner9b17c622013-08-28 10:18:05 +10001009long prune_dcache_sb(struct super_block *sb, unsigned long nr_to_scan,
1010 int nid)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001011{
Dave Chinnerf6041562013-08-28 10:18:00 +10001012 LIST_HEAD(dispose);
1013 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001014
Dave Chinner9b17c622013-08-28 10:18:05 +10001015 freed = list_lru_walk_node(&sb->s_dentry_lru, nid, dentry_lru_isolate,
1016 &dispose, &nr_to_scan);
Dave Chinnerf6041562013-08-28 10:18:00 +10001017 shrink_dentry_list(&dispose);
Dave Chinner0a234c62013-08-28 10:17:57 +10001018 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Glauber Costa4e717f52013-08-28 10:18:03 +10001021static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1022 spinlock_t *lru_lock, void *arg)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001023{
Glauber Costa4e717f52013-08-28 10:18:03 +10001024 struct list_head *freeable = arg;
1025 struct dentry *dentry = container_of(item, struct dentry, d_lru);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001026
Glauber Costa4e717f52013-08-28 10:18:03 +10001027 /*
1028 * we are inverting the lru lock/dentry->d_lock here,
1029 * so use a trylock. If we fail to get the lock, just skip
1030 * it
1031 */
1032 if (!spin_trylock(&dentry->d_lock))
1033 return LRU_SKIP;
1034
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001035 d_lru_shrink_move(dentry, freeable);
Glauber Costa4e717f52013-08-28 10:18:03 +10001036 spin_unlock(&dentry->d_lock);
1037
1038 return LRU_REMOVED;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001039}
1040
Glauber Costa4e717f52013-08-28 10:18:03 +10001041
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * shrink_dcache_sb - shrink dcache for a superblock
1044 * @sb: superblock
1045 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001046 * Shrink the dcache for the specified super block. This is used to free
1047 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001049void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Glauber Costa4e717f52013-08-28 10:18:03 +10001051 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001052
Glauber Costa4e717f52013-08-28 10:18:03 +10001053 do {
1054 LIST_HEAD(dispose);
1055
1056 freed = list_lru_walk(&sb->s_dentry_lru,
1057 dentry_lru_isolate_shrink, &dispose, UINT_MAX);
1058
1059 this_cpu_sub(nr_dentry_unused, freed);
1060 shrink_dentry_list(&dispose);
1061 } while (freed > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001063EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065/*
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001066 * This tries to ascend one level of parenthood, but
1067 * we can race with renaming, so we need to re-check
1068 * the parenthood after dropping the lock and check
1069 * that the sequence number still matches.
1070 */
Al Viro48f5ec22013-09-09 15:22:25 -04001071static struct dentry *try_to_ascend(struct dentry *old, unsigned seq)
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001072{
1073 struct dentry *new = old->d_parent;
1074
1075 rcu_read_lock();
1076 spin_unlock(&old->d_lock);
1077 spin_lock(&new->d_lock);
1078
1079 /*
1080 * might go back up the wrong parent if we have had a rename
1081 * or deletion
1082 */
1083 if (new != old->d_parent ||
Miklos Szeredib161dfa62012-09-17 22:31:38 +02001084 (old->d_flags & DCACHE_DENTRY_KILLED) ||
Al Viro48f5ec22013-09-09 15:22:25 -04001085 need_seqretry(&rename_lock, seq)) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001086 spin_unlock(&new->d_lock);
1087 new = NULL;
1088 }
1089 rcu_read_unlock();
1090 return new;
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/**
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001094 * enum d_walk_ret - action to talke during tree walk
1095 * @D_WALK_CONTINUE: contrinue walk
1096 * @D_WALK_QUIT: quit walk
1097 * @D_WALK_NORETRY: quit when retry is needed
1098 * @D_WALK_SKIP: skip this dentry and its children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001100enum d_walk_ret {
1101 D_WALK_CONTINUE,
1102 D_WALK_QUIT,
1103 D_WALK_NORETRY,
1104 D_WALK_SKIP,
1105};
1106
1107/**
1108 * d_walk - walk the dentry tree
1109 * @parent: start of walk
1110 * @data: data passed to @enter() and @finish()
1111 * @enter: callback when first entering the dentry
1112 * @finish: callback when successfully finished the walk
1113 *
1114 * The @enter() and @finish() callbacks are called with d_lock held.
1115 */
1116static void d_walk(struct dentry *parent, void *data,
1117 enum d_walk_ret (*enter)(void *, struct dentry *),
1118 void (*finish)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Nick Piggin949854d2011-01-07 17:49:37 +11001120 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct list_head *next;
Al Viro48f5ec22013-09-09 15:22:25 -04001122 unsigned seq = 0;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001123 enum d_walk_ret ret;
1124 bool retry = true;
Nick Piggin949854d2011-01-07 17:49:37 +11001125
Nick Piggin58db63d2011-01-07 17:49:39 +11001126again:
Al Viro48f5ec22013-09-09 15:22:25 -04001127 read_seqbegin_or_lock(&rename_lock, &seq);
Nick Piggin58db63d2011-01-07 17:49:39 +11001128 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001129 spin_lock(&this_parent->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001130
1131 ret = enter(data, this_parent);
1132 switch (ret) {
1133 case D_WALK_CONTINUE:
1134 break;
1135 case D_WALK_QUIT:
1136 case D_WALK_SKIP:
1137 goto out_unlock;
1138 case D_WALK_NORETRY:
1139 retry = false;
1140 break;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142repeat:
1143 next = this_parent->d_subdirs.next;
1144resume:
1145 while (next != &this_parent->d_subdirs) {
1146 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001147 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001149
1150 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001151
1152 ret = enter(data, dentry);
1153 switch (ret) {
1154 case D_WALK_CONTINUE:
1155 break;
1156 case D_WALK_QUIT:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001157 spin_unlock(&dentry->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001158 goto out_unlock;
1159 case D_WALK_NORETRY:
1160 retry = false;
1161 break;
1162 case D_WALK_SKIP:
1163 spin_unlock(&dentry->d_lock);
1164 continue;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001165 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001168 spin_unlock(&this_parent->d_lock);
1169 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001171 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto repeat;
1173 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001174 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 /*
1177 * All done at this level ... ascend and resume the search.
1178 */
1179 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001180 struct dentry *child = this_parent;
Al Viro48f5ec22013-09-09 15:22:25 -04001181 this_parent = try_to_ascend(this_parent, seq);
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001182 if (!this_parent)
Nick Piggin949854d2011-01-07 17:49:37 +11001183 goto rename_retry;
Nick Piggin949854d2011-01-07 17:49:37 +11001184 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 goto resume;
1186 }
Al Viro48f5ec22013-09-09 15:22:25 -04001187 if (need_seqretry(&rename_lock, seq)) {
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001188 spin_unlock(&this_parent->d_lock);
1189 goto rename_retry;
1190 }
1191 if (finish)
1192 finish(data);
1193
1194out_unlock:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001195 spin_unlock(&this_parent->d_lock);
Al Viro48f5ec22013-09-09 15:22:25 -04001196 done_seqretry(&rename_lock, seq);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001197 return;
Nick Piggin58db63d2011-01-07 17:49:39 +11001198
1199rename_retry:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001200 if (!retry)
1201 return;
Al Viro48f5ec22013-09-09 15:22:25 -04001202 seq = 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001203 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001205
1206/*
1207 * Search for at least 1 mount point in the dentry's subdirs.
1208 * We descend to the next level whenever the d_subdirs
1209 * list is non-empty and continue searching.
1210 */
1211
1212/**
1213 * have_submounts - check for mounts over a dentry
1214 * @parent: dentry to check.
1215 *
1216 * Return true if the parent or its subdirectories contain
1217 * a mount point
1218 */
1219
1220static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
1221{
1222 int *ret = data;
1223 if (d_mountpoint(dentry)) {
1224 *ret = 1;
1225 return D_WALK_QUIT;
1226 }
1227 return D_WALK_CONTINUE;
1228}
1229
1230int have_submounts(struct dentry *parent)
1231{
1232 int ret = 0;
1233
1234 d_walk(parent, &ret, check_mount, NULL);
1235
1236 return ret;
1237}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001238EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240/*
Miklos Szeredieed81002013-09-05 14:39:11 +02001241 * Called by mount code to set a mountpoint and check if the mountpoint is
1242 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1243 * subtree can become unreachable).
1244 *
1245 * Only one of check_submounts_and_drop() and d_set_mounted() must succeed. For
1246 * this reason take rename_lock and d_lock on dentry and ancestors.
1247 */
1248int d_set_mounted(struct dentry *dentry)
1249{
1250 struct dentry *p;
1251 int ret = -ENOENT;
1252 write_seqlock(&rename_lock);
1253 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1254 /* Need exclusion wrt. check_submounts_and_drop() */
1255 spin_lock(&p->d_lock);
1256 if (unlikely(d_unhashed(p))) {
1257 spin_unlock(&p->d_lock);
1258 goto out;
1259 }
1260 spin_unlock(&p->d_lock);
1261 }
1262 spin_lock(&dentry->d_lock);
1263 if (!d_unlinked(dentry)) {
1264 dentry->d_flags |= DCACHE_MOUNTED;
1265 ret = 0;
1266 }
1267 spin_unlock(&dentry->d_lock);
1268out:
1269 write_sequnlock(&rename_lock);
1270 return ret;
1271}
1272
1273/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001274 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * and move any unused dentries to the end of the unused
1276 * list for prune_dcache(). We descend to the next level
1277 * whenever the d_subdirs list is non-empty and continue
1278 * searching.
1279 *
1280 * It returns zero iff there are no unused children,
1281 * otherwise it returns the number of children moved to
1282 * the end of the unused list. This may not be the total
1283 * number of unused children, because select_parent can
1284 * drop the lock and return early due to latency
1285 * constraints.
1286 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001287
1288struct select_data {
1289 struct dentry *start;
1290 struct list_head dispose;
1291 int found;
1292};
1293
1294static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001296 struct select_data *data = _data;
1297 enum d_walk_ret ret = D_WALK_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001299 if (data->start == dentry)
1300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001302 /*
1303 * move only zero ref count dentries to the dispose list.
1304 *
1305 * Those which are presently on the shrink list, being processed
1306 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1307 * loop in shrink_dcache_parent() might not make any progress
1308 * and loop forever.
1309 */
1310 if (dentry->d_lockref.count) {
1311 dentry_lru_del(dentry);
1312 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001313 /*
1314 * We can't use d_lru_shrink_move() because we
1315 * need to get the global LRU lock and do the
Linus Torvalds05a82522013-09-15 07:11:01 -04001316 * LRU accounting.
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001317 */
1318 d_lru_del(dentry);
1319 d_shrink_add(dentry, &data->dispose);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001320 data->found++;
1321 ret = D_WALK_NORETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 /*
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001324 * We can return to the caller if we have found some (this
1325 * ensures forward progress). We'll be coming back to find
1326 * the rest.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001328 if (data->found && need_resched())
1329 ret = D_WALK_QUIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330out:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334/**
1335 * shrink_dcache_parent - prune dcache
1336 * @parent: parent of entries to prune
1337 *
1338 * Prune the dcache to remove unused children of the parent dentry.
1339 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001340void shrink_dcache_parent(struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001342 for (;;) {
1343 struct select_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001345 INIT_LIST_HEAD(&data.dispose);
1346 data.start = parent;
1347 data.found = 0;
1348
1349 d_walk(parent, &data, select_collect, NULL);
1350 if (!data.found)
1351 break;
1352
1353 shrink_dentry_list(&data.dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001354 cond_resched();
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001357EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Al Viro42c32602013-11-08 12:31:16 -05001359static enum d_walk_ret umount_collect(void *_data, struct dentry *dentry)
1360{
1361 struct select_data *data = _data;
1362 enum d_walk_ret ret = D_WALK_CONTINUE;
1363
1364 if (dentry->d_lockref.count) {
1365 dentry_lru_del(dentry);
1366 if (likely(!list_empty(&dentry->d_subdirs)))
1367 goto out;
1368 if (dentry == data->start && dentry->d_lockref.count == 1)
1369 goto out;
1370 printk(KERN_ERR
1371 "BUG: Dentry %p{i=%lx,n=%s}"
1372 " still in use (%d)"
1373 " [unmount of %s %s]\n",
1374 dentry,
1375 dentry->d_inode ?
1376 dentry->d_inode->i_ino : 0UL,
1377 dentry->d_name.name,
1378 dentry->d_lockref.count,
1379 dentry->d_sb->s_type->name,
1380 dentry->d_sb->s_id);
1381 BUG();
1382 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1383 /*
1384 * We can't use d_lru_shrink_move() because we
1385 * need to get the global LRU lock and do the
1386 * LRU accounting.
1387 */
1388 if (dentry->d_flags & DCACHE_LRU_LIST)
1389 d_lru_del(dentry);
1390 d_shrink_add(dentry, &data->dispose);
1391 data->found++;
1392 ret = D_WALK_NORETRY;
1393 }
1394out:
1395 if (data->found && need_resched())
1396 ret = D_WALK_QUIT;
1397 return ret;
1398}
1399
1400/*
1401 * destroy the dentries attached to a superblock on unmounting
1402 */
1403void shrink_dcache_for_umount(struct super_block *sb)
1404{
1405 struct dentry *dentry;
1406
1407 if (down_read_trylock(&sb->s_umount))
1408 BUG();
1409
1410 dentry = sb->s_root;
1411 sb->s_root = NULL;
1412 for (;;) {
1413 struct select_data data;
1414
1415 INIT_LIST_HEAD(&data.dispose);
1416 data.start = dentry;
1417 data.found = 0;
1418
1419 d_walk(dentry, &data, umount_collect, NULL);
1420 if (!data.found)
1421 break;
1422
1423 shrink_dentry_list(&data.dispose);
1424 cond_resched();
1425 }
1426 d_drop(dentry);
1427 dput(dentry);
1428
1429 while (!hlist_bl_empty(&sb->s_anon)) {
1430 struct select_data data;
1431 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
1432
1433 INIT_LIST_HEAD(&data.dispose);
1434 data.start = NULL;
1435 data.found = 0;
1436
1437 d_walk(dentry, &data, umount_collect, NULL);
1438 if (data.found)
1439 shrink_dentry_list(&data.dispose);
1440 cond_resched();
1441 }
1442}
1443
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001444static enum d_walk_ret check_and_collect(void *_data, struct dentry *dentry)
1445{
1446 struct select_data *data = _data;
1447
1448 if (d_mountpoint(dentry)) {
1449 data->found = -EBUSY;
1450 return D_WALK_QUIT;
1451 }
1452
1453 return select_collect(_data, dentry);
1454}
1455
1456static void check_and_drop(void *_data)
1457{
1458 struct select_data *data = _data;
1459
1460 if (d_mountpoint(data->start))
1461 data->found = -EBUSY;
1462 if (!data->found)
1463 __d_drop(data->start);
1464}
1465
1466/**
1467 * check_submounts_and_drop - prune dcache, check for submounts and drop
1468 *
1469 * All done as a single atomic operation relative to has_unlinked_ancestor().
1470 * Returns 0 if successfully unhashed @parent. If there were submounts then
1471 * return -EBUSY.
1472 *
1473 * @dentry: dentry to prune and drop
1474 */
1475int check_submounts_and_drop(struct dentry *dentry)
1476{
1477 int ret = 0;
1478
1479 /* Negative dentries can be dropped without further checks */
1480 if (!dentry->d_inode) {
1481 d_drop(dentry);
1482 goto out;
1483 }
1484
1485 for (;;) {
1486 struct select_data data;
1487
1488 INIT_LIST_HEAD(&data.dispose);
1489 data.start = dentry;
1490 data.found = 0;
1491
1492 d_walk(dentry, &data, check_and_collect, check_and_drop);
1493 ret = data.found;
1494
1495 if (!list_empty(&data.dispose))
1496 shrink_dentry_list(&data.dispose);
1497
1498 if (ret <= 0)
1499 break;
1500
1501 cond_resched();
1502 }
1503
1504out:
1505 return ret;
1506}
1507EXPORT_SYMBOL(check_submounts_and_drop);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/**
Al Viroa4464db2011-07-07 15:03:58 -04001510 * __d_alloc - allocate a dcache entry
1511 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * @name: qstr of the name
1513 *
1514 * Allocates a dentry. It returns %NULL if there is insufficient memory
1515 * available. On a success the dentry is returned. The name passed in is
1516 * copied and the copy passed in may be reused after this call.
1517 */
1518
Al Viroa4464db2011-07-07 15:03:58 -04001519struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct dentry *dentry;
1522 char *dname;
1523
Mel Gormane12ba742007-10-16 01:25:52 -07001524 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!dentry)
1526 return NULL;
1527
Linus Torvalds6326c712012-05-21 16:14:04 -07001528 /*
1529 * We guarantee that the inline name is always NUL-terminated.
1530 * This way the memcpy() done by the name switching in rename
1531 * will still always have a NUL at the end, even if we might
1532 * be overwriting an internal NUL character
1533 */
1534 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (name->len > DNAME_INLINE_LEN-1) {
1536 dname = kmalloc(name->len + 1, GFP_KERNEL);
1537 if (!dname) {
1538 kmem_cache_free(dentry_cache, dentry);
1539 return NULL;
1540 }
1541 } else {
1542 dname = dentry->d_iname;
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 dentry->d_name.len = name->len;
1546 dentry->d_name.hash = name->hash;
1547 memcpy(dname, name->name, name->len);
1548 dname[name->len] = 0;
1549
Linus Torvalds6326c712012-05-21 16:14:04 -07001550 /* Make sure we always see the terminating NUL character */
1551 smp_wmb();
1552 dentry->d_name.name = dname;
1553
Waiman Long98474232013-08-28 18:24:59 -07001554 dentry->d_lockref.count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001555 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001557 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001559 dentry->d_parent = dentry;
1560 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 dentry->d_op = NULL;
1562 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001563 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 INIT_LIST_HEAD(&dentry->d_lru);
1565 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Virob3d9b7a2012-06-09 13:51:19 -04001566 INIT_HLIST_NODE(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001567 INIT_LIST_HEAD(&dentry->d_u.d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001568 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Nick Piggin3e880fb2011-01-07 17:49:19 +11001570 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return dentry;
1573}
Al Viroa4464db2011-07-07 15:03:58 -04001574
1575/**
1576 * d_alloc - allocate a dcache entry
1577 * @parent: parent of entry to allocate
1578 * @name: qstr of the name
1579 *
1580 * Allocates a dentry. It returns %NULL if there is insufficient memory
1581 * available. On a success the dentry is returned. The name passed in is
1582 * copied and the copy passed in may be reused after this call.
1583 */
1584struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1585{
1586 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1587 if (!dentry)
1588 return NULL;
1589
1590 spin_lock(&parent->d_lock);
1591 /*
1592 * don't need child lock because it is not subject
1593 * to concurrency here
1594 */
1595 __dget_dlock(parent);
1596 dentry->d_parent = parent;
1597 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1598 spin_unlock(&parent->d_lock);
1599
1600 return dentry;
1601}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001602EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Nick Piggin4b936882011-01-07 17:50:07 +11001604struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1605{
Al Viroa4464db2011-07-07 15:03:58 -04001606 struct dentry *dentry = __d_alloc(sb, name);
1607 if (dentry)
Nick Piggin4b936882011-01-07 17:50:07 +11001608 dentry->d_flags |= DCACHE_DISCONNECTED;
Nick Piggin4b936882011-01-07 17:50:07 +11001609 return dentry;
1610}
1611EXPORT_SYMBOL(d_alloc_pseudo);
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1614{
1615 struct qstr q;
1616
1617 q.name = name;
1618 q.len = strlen(name);
1619 q.hash = full_name_hash(q.name, q.len);
1620 return d_alloc(parent, &q);
1621}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001622EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Nick Pigginfb045ad2011-01-07 17:49:55 +11001624void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1625{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001626 WARN_ON_ONCE(dentry->d_op);
1627 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001628 DCACHE_OP_COMPARE |
1629 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001630 DCACHE_OP_WEAK_REVALIDATE |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001631 DCACHE_OP_DELETE ));
1632 dentry->d_op = op;
1633 if (!op)
1634 return;
1635 if (op->d_hash)
1636 dentry->d_flags |= DCACHE_OP_HASH;
1637 if (op->d_compare)
1638 dentry->d_flags |= DCACHE_OP_COMPARE;
1639 if (op->d_revalidate)
1640 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001641 if (op->d_weak_revalidate)
1642 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001643 if (op->d_delete)
1644 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001645 if (op->d_prune)
1646 dentry->d_flags |= DCACHE_OP_PRUNE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001647
1648}
1649EXPORT_SYMBOL(d_set_d_op);
1650
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001651static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1652{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001653 spin_lock(&dentry->d_lock);
David Howells9875cf82011-01-14 18:45:21 +00001654 if (inode) {
1655 if (unlikely(IS_AUTOMOUNT(inode)))
1656 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
Al Virob3d9b7a2012-06-09 13:51:19 -04001657 hlist_add_head(&dentry->d_alias, &inode->i_dentry);
David Howells9875cf82011-01-14 18:45:21 +00001658 }
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001659 dentry->d_inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001660 dentry_rcuwalk_barrier(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001661 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001662 fsnotify_d_instantiate(dentry, inode);
1663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665/**
1666 * d_instantiate - fill in inode information for a dentry
1667 * @entry: dentry to complete
1668 * @inode: inode to attach to this dentry
1669 *
1670 * Fill in inode information in the entry.
1671 *
1672 * This turns negative dentries into productive full members
1673 * of society.
1674 *
1675 * NOTE! This assumes that the inode count has been incremented
1676 * (or otherwise set) by the caller to indicate that it is now
1677 * in use by the dcache.
1678 */
1679
1680void d_instantiate(struct dentry *entry, struct inode * inode)
1681{
Al Virob3d9b7a2012-06-09 13:51:19 -04001682 BUG_ON(!hlist_unhashed(&entry->d_alias));
Nick Piggin873feea2011-01-07 17:50:06 +11001683 if (inode)
1684 spin_lock(&inode->i_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001685 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001686 if (inode)
1687 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 security_d_instantiate(entry, inode);
1689}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001690EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692/**
1693 * d_instantiate_unique - instantiate a non-aliased dentry
1694 * @entry: dentry to instantiate
1695 * @inode: inode to attach to this dentry
1696 *
1697 * Fill in inode information in the entry. On success, it returns NULL.
1698 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001699 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *
1701 * Note that in order to avoid conflicts with rename() etc, the caller
1702 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001703 *
1704 * This also 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
David Howells770bfad2006-08-22 20:06:07 -04001708static struct dentry *__d_instantiate_unique(struct dentry *entry,
1709 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct dentry *alias;
1712 int len = entry->d_name.len;
1713 const char *name = entry->d_name.name;
1714 unsigned int hash = entry->d_name.hash;
1715
David Howells770bfad2006-08-22 20:06:07 -04001716 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001717 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001718 return NULL;
1719 }
1720
Sasha Levinb67bfe02013-02-27 17:06:00 -08001721 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
Nick Piggin9abca362011-01-07 17:49:36 +11001722 /*
1723 * Don't need alias->d_lock here, because aliases with
1724 * d_parent == entry->d_parent are not subject to name or
1725 * parent changes, because the parent inode i_mutex is held.
1726 */
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001727 if (alias->d_name.hash != hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 continue;
1729 if (alias->d_parent != entry->d_parent)
1730 continue;
Linus Torvaldsee983e82012-05-10 12:37:10 -07001731 if (alias->d_name.len != len)
1732 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001733 if (dentry_cmp(alias, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001735 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return alias;
1737 }
David Howells770bfad2006-08-22 20:06:07 -04001738
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001739 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return NULL;
1741}
David Howells770bfad2006-08-22 20:06:07 -04001742
1743struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1744{
1745 struct dentry *result;
1746
Al Virob3d9b7a2012-06-09 13:51:19 -04001747 BUG_ON(!hlist_unhashed(&entry->d_alias));
David Howells770bfad2006-08-22 20:06:07 -04001748
Nick Piggin873feea2011-01-07 17:50:06 +11001749 if (inode)
1750 spin_lock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001751 result = __d_instantiate_unique(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001752 if (inode)
1753 spin_unlock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001754
1755 if (!result) {
1756 security_d_instantiate(entry, inode);
1757 return NULL;
1758 }
1759
1760 BUG_ON(!d_unhashed(result));
1761 iput(inode);
1762 return result;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765EXPORT_SYMBOL(d_instantiate_unique);
1766
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001767/**
1768 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1769 * @entry: dentry to complete
1770 * @inode: inode to attach to this dentry
1771 *
1772 * Fill in inode information in the entry. If a directory alias is found, then
1773 * return an error (and drop inode). Together with d_materialise_unique() this
1774 * guarantees that a directory inode may never have more than one alias.
1775 */
1776int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1777{
1778 BUG_ON(!hlist_unhashed(&entry->d_alias));
1779
1780 spin_lock(&inode->i_lock);
1781 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1782 spin_unlock(&inode->i_lock);
1783 iput(inode);
1784 return -EBUSY;
1785 }
1786 __d_instantiate(entry, inode);
1787 spin_unlock(&inode->i_lock);
1788 security_d_instantiate(entry, inode);
1789
1790 return 0;
1791}
1792EXPORT_SYMBOL(d_instantiate_no_diralias);
1793
Al Viroadc0e912012-01-08 16:49:21 -05001794struct dentry *d_make_root(struct inode *root_inode)
1795{
1796 struct dentry *res = NULL;
1797
1798 if (root_inode) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07001799 static const struct qstr name = QSTR_INIT("/", 1);
Al Viroadc0e912012-01-08 16:49:21 -05001800
1801 res = __d_alloc(root_inode->i_sb, &name);
1802 if (res)
1803 d_instantiate(res, root_inode);
1804 else
1805 iput(root_inode);
1806 }
1807 return res;
1808}
1809EXPORT_SYMBOL(d_make_root);
1810
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001811static struct dentry * __d_find_any_alias(struct inode *inode)
1812{
1813 struct dentry *alias;
1814
Al Virob3d9b7a2012-06-09 13:51:19 -04001815 if (hlist_empty(&inode->i_dentry))
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001816 return NULL;
Al Virob3d9b7a2012-06-09 13:51:19 -04001817 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001818 __dget(alias);
1819 return alias;
1820}
1821
Sage Weil46f72b32012-01-10 09:04:37 -08001822/**
1823 * d_find_any_alias - find any alias for a given inode
1824 * @inode: inode to find an alias for
1825 *
1826 * If any aliases exist for the given inode, take and return a
1827 * reference for one of them. If no aliases exist, return %NULL.
1828 */
1829struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001830{
1831 struct dentry *de;
1832
1833 spin_lock(&inode->i_lock);
1834 de = __d_find_any_alias(inode);
1835 spin_unlock(&inode->i_lock);
1836 return de;
1837}
Sage Weil46f72b32012-01-10 09:04:37 -08001838EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001841 * d_obtain_alias - find or allocate a dentry for a given inode
1842 * @inode: inode to allocate the dentry for
1843 *
1844 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1845 * similar open by handle operations. The returned dentry may be anonymous,
1846 * or may have a full name (if the inode was already in the cache).
1847 *
1848 * When called on a directory inode, we must ensure that the inode only ever
1849 * has one dentry. If a dentry is found, that is returned instead of
1850 * allocating a new one.
1851 *
1852 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001853 * to the dentry. In case of an error the reference on the inode is released.
1854 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1855 * be passed in and will be the error will be propagate to the return value,
1856 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001857 */
1858struct dentry *d_obtain_alias(struct inode *inode)
1859{
NeilBrownb911a6b2012-11-08 16:09:37 -08001860 static const struct qstr anonstring = QSTR_INIT("/", 1);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001861 struct dentry *tmp;
1862 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001863
1864 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001865 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001866 if (IS_ERR(inode))
1867 return ERR_CAST(inode);
1868
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001869 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001870 if (res)
1871 goto out_iput;
1872
Al Viroa4464db2011-07-07 15:03:58 -04001873 tmp = __d_alloc(inode->i_sb, &anonstring);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001874 if (!tmp) {
1875 res = ERR_PTR(-ENOMEM);
1876 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001877 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001878
Nick Piggin873feea2011-01-07 17:50:06 +11001879 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001880 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001881 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001882 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001883 dput(tmp);
1884 goto out_iput;
1885 }
1886
1887 /* attach a disconnected dentry */
1888 spin_lock(&tmp->d_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001889 tmp->d_inode = inode;
1890 tmp->d_flags |= DCACHE_DISCONNECTED;
Al Virob3d9b7a2012-06-09 13:51:19 -04001891 hlist_add_head(&tmp->d_alias, &inode->i_dentry);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001892 hlist_bl_lock(&tmp->d_sb->s_anon);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001893 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001894 hlist_bl_unlock(&tmp->d_sb->s_anon);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001895 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001896 spin_unlock(&inode->i_lock);
Josef Bacik24ff6662010-11-18 20:52:55 -05001897 security_d_instantiate(tmp, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001898
Christoph Hellwig9308a612008-08-11 15:49:12 +02001899 return tmp;
1900
1901 out_iput:
Josef Bacik24ff6662010-11-18 20:52:55 -05001902 if (res && !IS_ERR(res))
1903 security_d_instantiate(res, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001904 iput(inode);
1905 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001906}
Benny Halevyadc48722009-02-27 14:02:59 -08001907EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909/**
1910 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1911 * @inode: the inode which may have a disconnected dentry
1912 * @dentry: a negative dentry which we want to point to the inode.
1913 *
1914 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1915 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1916 * and return it, else simply d_add the inode to the dentry and return NULL.
1917 *
1918 * This is needed in the lookup routine of any filesystem that is exportable
1919 * (via knfsd) so that we can build dcache paths to directories effectively.
1920 *
1921 * If a dentry was found and moved, then it is returned. Otherwise NULL
1922 * is returned. This matches the expected return value of ->lookup.
1923 *
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001924 * Cluster filesystems may call this function with a negative, hashed dentry.
1925 * In that case, we know that the inode will be a regular file, and also this
1926 * will only occur during atomic_open. So we need to check for the dentry
1927 * being already hashed only in the final case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
1929struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1930{
1931 struct dentry *new = NULL;
1932
Al Viroa9049372011-07-08 21:20:11 -04001933 if (IS_ERR(inode))
1934 return ERR_CAST(inode);
1935
NeilBrown21c0d8f2006-10-04 02:16:16 -07001936 if (inode && S_ISDIR(inode->i_mode)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001937 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001938 new = __d_find_alias(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (new) {
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001940 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Piggin873feea2011-01-07 17:50:06 +11001941 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 d_move(new, dentry);
1944 iput(inode);
1945 } else {
Nick Piggin873feea2011-01-07 17:50:06 +11001946 /* already taking inode->i_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001947 __d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001948 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 security_d_instantiate(dentry, inode);
1950 d_rehash(dentry);
1951 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001952 } else {
1953 d_instantiate(dentry, inode);
1954 if (d_unhashed(dentry))
1955 d_rehash(dentry);
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return new;
1958}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001959EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Barry Naujok94035402008-05-21 16:50:46 +10001961/**
1962 * d_add_ci - lookup or allocate new dentry with case-exact name
1963 * @inode: the inode case-insensitive lookup has found
1964 * @dentry: the negative dentry that was passed to the parent's lookup func
1965 * @name: the case-exact name to be associated with the returned dentry
1966 *
1967 * This is to avoid filling the dcache with case-insensitive names to the
1968 * same inode, only the actual correct case is stored in the dcache for
1969 * case-insensitive filesystems.
1970 *
1971 * For a case-insensitive lookup match and if the the case-exact dentry
1972 * already exists in in the dcache, use it and return it.
1973 *
1974 * If no entry exists with the exact case name, allocate new dentry with
1975 * the exact case, and return the spliced entry.
1976 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001977struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001978 struct qstr *name)
1979{
Barry Naujok94035402008-05-21 16:50:46 +10001980 struct dentry *found;
1981 struct dentry *new;
1982
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001983 /*
1984 * First check if a dentry matching the name already exists,
1985 * if not go ahead and create it now.
1986 */
Barry Naujok94035402008-05-21 16:50:46 +10001987 found = d_hash_and_lookup(dentry->d_parent, name);
Al Viro4f522a22013-02-11 23:20:37 -05001988 if (unlikely(IS_ERR(found)))
1989 goto err_out;
Barry Naujok94035402008-05-21 16:50:46 +10001990 if (!found) {
1991 new = d_alloc(dentry->d_parent, name);
1992 if (!new) {
Al Viro4f522a22013-02-11 23:20:37 -05001993 found = ERR_PTR(-ENOMEM);
Barry Naujok94035402008-05-21 16:50:46 +10001994 goto err_out;
1995 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001996
Barry Naujok94035402008-05-21 16:50:46 +10001997 found = d_splice_alias(inode, new);
1998 if (found) {
1999 dput(new);
2000 return found;
2001 }
2002 return new;
2003 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002004
2005 /*
2006 * If a matching dentry exists, and it's not negative use it.
2007 *
2008 * Decrement the reference count to balance the iget() done
2009 * earlier on.
2010 */
Barry Naujok94035402008-05-21 16:50:46 +10002011 if (found->d_inode) {
2012 if (unlikely(found->d_inode != inode)) {
2013 /* This can't happen because bad inodes are unhashed. */
2014 BUG_ON(!is_bad_inode(inode));
2015 BUG_ON(!is_bad_inode(found->d_inode));
2016 }
Barry Naujok94035402008-05-21 16:50:46 +10002017 iput(inode);
2018 return found;
2019 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002020
Barry Naujok94035402008-05-21 16:50:46 +10002021 /*
2022 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002023 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10002024 */
Al Viro4513d892011-07-17 10:52:14 -04002025 new = d_splice_alias(inode, found);
2026 if (new) {
2027 dput(found);
2028 found = new;
Barry Naujok94035402008-05-21 16:50:46 +10002029 }
Al Viro4513d892011-07-17 10:52:14 -04002030 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002031
2032err_out:
2033 iput(inode);
Al Viro4f522a22013-02-11 23:20:37 -05002034 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002035}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002036EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002038/*
2039 * Do the slow-case of the dentry name compare.
2040 *
2041 * Unlike the dentry_cmp() function, we need to atomically
Linus Torvaldsda53be12013-05-21 15:22:44 -07002042 * load the name and length information, so that the
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002043 * filesystem can rely on them, and can use the 'name' and
2044 * 'len' information without worrying about walking off the
2045 * end of memory etc.
2046 *
2047 * Thus the read_seqcount_retry() and the "duplicate" info
2048 * in arguments (the low-level filesystem should not look
2049 * at the dentry inode or name contents directly, since
2050 * rename can change them while we're in RCU mode).
2051 */
2052enum slow_d_compare {
2053 D_COMP_OK,
2054 D_COMP_NOMATCH,
2055 D_COMP_SEQRETRY,
2056};
2057
2058static noinline enum slow_d_compare slow_dentry_cmp(
2059 const struct dentry *parent,
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002060 struct dentry *dentry,
2061 unsigned int seq,
2062 const struct qstr *name)
2063{
2064 int tlen = dentry->d_name.len;
2065 const char *tname = dentry->d_name.name;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002066
2067 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2068 cpu_relax();
2069 return D_COMP_SEQRETRY;
2070 }
Linus Torvaldsda53be12013-05-21 15:22:44 -07002071 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002072 return D_COMP_NOMATCH;
2073 return D_COMP_OK;
2074}
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/**
Nick Piggin31e6b012011-01-07 17:49:52 +11002077 * __d_lookup_rcu - search for a dentry (racy, store-free)
2078 * @parent: parent dentry
2079 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07002080 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11002081 * Returns: dentry, or NULL
2082 *
2083 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2084 * resolution (store-free path walking) design described in
2085 * Documentation/filesystems/path-lookup.txt.
2086 *
2087 * This is not to be used outside core vfs.
2088 *
2089 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2090 * held, and rcu_read_lock held. The returned dentry must not be stored into
2091 * without taking d_lock and checking d_seq sequence count against @seq
2092 * returned here.
2093 *
Linus Torvalds15570082013-09-02 11:38:06 -07002094 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
Nick Piggin31e6b012011-01-07 17:49:52 +11002095 * function.
2096 *
2097 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2098 * the returned dentry, so long as its parent's seqlock is checked after the
2099 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2100 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002101 *
2102 * NOTE! The caller *has* to check the resulting dentry against the sequence
2103 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11002104 */
Linus Torvalds8966be92012-03-02 14:23:30 -08002105struct dentry *__d_lookup_rcu(const struct dentry *parent,
2106 const struct qstr *name,
Linus Torvaldsda53be12013-05-21 15:22:44 -07002107 unsigned *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11002108{
Linus Torvalds26fe5752012-05-10 13:14:12 -07002109 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11002110 const unsigned char *str = name->name;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002111 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002112 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002113 struct dentry *dentry;
2114
2115 /*
2116 * Note: There is significant duplication with __d_lookup_rcu which is
2117 * required to prevent single threaded performance regressions
2118 * especially on architectures where smp_rmb (in seqcounts) are costly.
2119 * Keep the two functions in sync.
2120 */
2121
2122 /*
2123 * The hash list is protected using RCU.
2124 *
2125 * Carefully use d_seq when comparing a candidate dentry, to avoid
2126 * races with d_move().
2127 *
2128 * It is possible that concurrent renames can mess up our list
2129 * walk here and result in missing our dentry, resulting in the
2130 * false-negative result. d_lookup() protects against concurrent
2131 * renames using rename_lock seqlock.
2132 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002133 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11002134 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002135 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08002136 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11002137
Nick Piggin31e6b012011-01-07 17:49:52 +11002138seqretry:
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002139 /*
2140 * The dentry sequence count protects us from concurrent
Linus Torvaldsda53be12013-05-21 15:22:44 -07002141 * renames, and thus protects parent and name fields.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002142 *
2143 * The caller must perform a seqcount check in order
Linus Torvaldsda53be12013-05-21 15:22:44 -07002144 * to do anything useful with the returned dentry.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002145 *
2146 * NOTE! We do a "raw" seqcount_begin here. That means that
2147 * we don't wait for the sequence count to stabilize if it
2148 * is in the middle of a sequence change. If we do the slow
2149 * dentry compare, we will do seqretries until it is stable,
2150 * and if we end up with a successful lookup, we actually
2151 * want to exit RCU lookup anyway.
2152 */
2153 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11002154 if (dentry->d_parent != parent)
2155 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07002156 if (d_unhashed(dentry))
2157 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002158
2159 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07002160 if (dentry->d_name.hash != hashlen_hash(hashlen))
2161 continue;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002162 *seqp = seq;
2163 switch (slow_dentry_cmp(parent, dentry, seq, name)) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002164 case D_COMP_OK:
2165 return dentry;
2166 case D_COMP_NOMATCH:
2167 continue;
2168 default:
2169 goto seqretry;
2170 }
2171 }
2172
Linus Torvalds26fe5752012-05-10 13:14:12 -07002173 if (dentry->d_name.hash_len != hashlen)
Linus Torvaldsee983e82012-05-10 12:37:10 -07002174 continue;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002175 *seqp = seq;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002176 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002177 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002178 }
2179 return NULL;
2180}
2181
2182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * d_lookup - search for a dentry
2184 * @parent: parent dentry
2185 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10002186 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 *
Nick Pigginb04f7842010-08-18 04:37:34 +10002188 * d_lookup searches the children of the parent dentry for the name in
2189 * question. If the dentry is found its reference count is incremented and the
2190 * dentry is returned. The caller must use dput to free the entry when it has
2191 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 */
Al Viroda2d8452013-01-24 18:29:34 -05002193struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Nick Piggin31e6b012011-01-07 17:49:52 +11002195 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002196 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 do {
2199 seq = read_seqbegin(&rename_lock);
2200 dentry = __d_lookup(parent, name);
2201 if (dentry)
2202 break;
2203 } while (read_seqretry(&rename_lock, seq));
2204 return dentry;
2205}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002206EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Nick Piggin31e6b012011-01-07 17:49:52 +11002208/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002209 * __d_lookup - search for a dentry (racy)
2210 * @parent: parent dentry
2211 * @name: qstr of name we wish to find
2212 * Returns: dentry, or NULL
2213 *
2214 * __d_lookup is like d_lookup, however it may (rarely) return a
2215 * false-negative result due to unrelated rename activity.
2216 *
2217 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2218 * however it must be used carefully, eg. with a following d_lookup in
2219 * the case of failure.
2220 *
2221 * __d_lookup callers must be commented.
2222 */
Al Viroa713ca22013-01-24 18:27:00 -05002223struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 unsigned int len = name->len;
2226 unsigned int hash = name->hash;
2227 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002228 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002229 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002230 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002231 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Nick Pigginb04f7842010-08-18 04:37:34 +10002233 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002234 * Note: There is significant duplication with __d_lookup_rcu which is
2235 * required to prevent single threaded performance regressions
2236 * especially on architectures where smp_rmb (in seqcounts) are costly.
2237 * Keep the two functions in sync.
2238 */
2239
2240 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002241 * The hash list is protected using RCU.
2242 *
2243 * Take d_lock when comparing a candidate dentry, to avoid races
2244 * with d_move().
2245 *
2246 * It is possible that concurrent renames can mess up our list
2247 * walk here and result in missing our dentry, resulting in the
2248 * false-negative result. d_lookup() protects against concurrent
2249 * renames using rename_lock seqlock.
2250 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002251 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rcu_read_lock();
2254
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002255 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (dentry->d_name.hash != hash)
2258 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (dentry->d_parent != parent)
2262 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002263 if (d_unhashed(dentry))
2264 goto next;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * It is safe to compare names since d_move() cannot
2268 * change the qstr (protected by d_lock).
2269 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11002270 if (parent->d_flags & DCACHE_OP_COMPARE) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002271 int tlen = dentry->d_name.len;
2272 const char *tname = dentry->d_name.name;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002273 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto next;
2275 } else {
Linus Torvaldsee983e82012-05-10 12:37:10 -07002276 if (dentry->d_name.len != len)
2277 goto next;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002278 if (dentry_cmp(dentry, str, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 goto next;
2280 }
2281
Waiman Long98474232013-08-28 18:24:59 -07002282 dentry->d_lockref.count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002283 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 spin_unlock(&dentry->d_lock);
2285 break;
2286next:
2287 spin_unlock(&dentry->d_lock);
2288 }
2289 rcu_read_unlock();
2290
2291 return found;
2292}
2293
2294/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002295 * d_hash_and_lookup - hash the qstr then search for a dentry
2296 * @dir: Directory to search in
2297 * @name: qstr of name we wish to find
2298 *
Al Viro4f522a22013-02-11 23:20:37 -05002299 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002300 */
2301struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2302{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002303 /*
2304 * Check for a fs-specific hash function. Note that we must
2305 * calculate the standard hash first, as the d_op->d_hash()
2306 * routine may choose to leave the hash value unchanged.
2307 */
2308 name->hash = full_name_hash(name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002309 if (dir->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002310 int err = dir->d_op->d_hash(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05002311 if (unlikely(err < 0))
2312 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002313 }
Al Viro4f522a22013-02-11 23:20:37 -05002314 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002315}
Al Viro4f522a22013-02-11 23:20:37 -05002316EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002317
2318/**
Nick Piggin786a5e12011-01-07 17:49:16 +11002319 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 * @dentry: The dentry alleged to be valid child of @dparent
Randy Dunlapff5fdb62011-01-22 20:16:06 -08002321 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 *
2323 * An insecure source has sent us a dentry, here we verify it and dget() it.
2324 * This is used by ncpfs in its readdir implementation.
2325 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11002326 *
2327 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 */
Nick Piggind3a23e12011-01-05 20:01:21 +11002329int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Nick Piggin786a5e12011-01-07 17:49:16 +11002331 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11002332
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002333 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002334 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
2335 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002336 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggindc0474b2011-01-07 17:49:43 +11002337 __dget_dlock(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002338 spin_unlock(&dentry->d_lock);
2339 spin_unlock(&dparent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 1;
2341 }
2342 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002343 spin_unlock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return 0;
2346}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002347EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349/*
2350 * When a file is deleted, we have two options:
2351 * - turn this dentry into a negative dentry
2352 * - unhash this dentry and free it.
2353 *
2354 * Usually, we want to just turn this into
2355 * a negative dentry, but if anybody else is
2356 * currently using the dentry or the inode
2357 * we can't do that and we fall back on removing
2358 * it from the hash queues and waiting for
2359 * it to be deleted later when it has no users
2360 */
2361
2362/**
2363 * d_delete - delete a dentry
2364 * @dentry: The dentry to delete
2365 *
2366 * Turn the dentry into a negative dentry if possible, otherwise
2367 * remove it from the hash queues so it can be deleted later
2368 */
2369
2370void d_delete(struct dentry * dentry)
2371{
Nick Piggin873feea2011-01-07 17:50:06 +11002372 struct inode *inode;
John McCutchan7a91bf72005-08-08 13:52:16 -04002373 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Are we the only user?
2376 */
Nick Piggin357f8e62011-01-07 17:49:42 +11002377again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 spin_lock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002379 inode = dentry->d_inode;
2380 isdir = S_ISDIR(inode->i_mode);
Waiman Long98474232013-08-28 18:24:59 -07002381 if (dentry->d_lockref.count == 1) {
Alan Cox1fe0c022012-09-19 15:49:51 +01002382 if (!spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002383 spin_unlock(&dentry->d_lock);
2384 cpu_relax();
2385 goto again;
2386 }
Al Viro13e3c5e2010-05-21 16:11:04 -04002387 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002388 dentry_unlink_inode(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04002389 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return;
2391 }
2392
2393 if (!d_unhashed(dentry))
2394 __d_drop(dentry);
2395
2396 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04002397
2398 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002400EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002402static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002404 BUG_ON(!d_unhashed(entry));
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002405 hlist_bl_lock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -07002406 entry->d_flags |= DCACHE_RCUACCESS;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002407 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002408 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
David Howells770bfad2006-08-22 20:06:07 -04002411static void _d_rehash(struct dentry * entry)
2412{
2413 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/**
2417 * d_rehash - add an entry back to the hash
2418 * @entry: dentry to add to the hash
2419 *
2420 * Adds a dentry to the hash according to its name.
2421 */
2422
2423void d_rehash(struct dentry * entry)
2424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04002426 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002429EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002431/**
2432 * dentry_update_name_case - update case insensitive dentry with a new name
2433 * @dentry: dentry to be updated
2434 * @name: new name
2435 *
2436 * Update a case insensitive dentry with new case of name.
2437 *
2438 * dentry must have been returned by d_lookup with name @name. Old and new
2439 * name lengths must match (ie. no d_compare which allows mismatched name
2440 * lengths).
2441 *
2442 * Parent inode i_mutex must be held over d_lookup and into this call (to
2443 * keep renames and concurrent inserts, and readdir(2) away).
2444 */
2445void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2446{
Linus Torvalds7ebfa572011-04-15 07:34:26 -07002447 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002448 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2449
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002450 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002451 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002452 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002453 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002454 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002455}
2456EXPORT_SYMBOL(dentry_update_name_case);
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458static void switch_names(struct dentry *dentry, struct dentry *target)
2459{
2460 if (dname_external(target)) {
2461 if (dname_external(dentry)) {
2462 /*
2463 * Both external: swap the pointers
2464 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002465 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 } else {
2467 /*
2468 * dentry:internal, target:external. Steal target's
2469 * storage and make target internal.
2470 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002471 memcpy(target->d_iname, dentry->d_name.name,
2472 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 dentry->d_name.name = target->d_name.name;
2474 target->d_name.name = target->d_iname;
2475 }
2476 } else {
2477 if (dname_external(dentry)) {
2478 /*
2479 * dentry:external, target:internal. Give dentry's
2480 * storage to target and make dentry internal
2481 */
2482 memcpy(dentry->d_iname, target->d_name.name,
2483 target->d_name.len + 1);
2484 target->d_name.name = dentry->d_name.name;
2485 dentry->d_name.name = dentry->d_iname;
2486 } else {
2487 /*
2488 * Both are internal. Just copy target to dentry
2489 */
2490 memcpy(dentry->d_iname, target->d_name.name,
2491 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05002492 dentry->d_name.len = target->d_name.len;
2493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002496 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002499static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2500{
2501 /*
2502 * XXXX: do we really need to take target->d_lock?
2503 */
2504 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2505 spin_lock(&target->d_parent->d_lock);
2506 else {
2507 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2508 spin_lock(&dentry->d_parent->d_lock);
2509 spin_lock_nested(&target->d_parent->d_lock,
2510 DENTRY_D_LOCK_NESTED);
2511 } else {
2512 spin_lock(&target->d_parent->d_lock);
2513 spin_lock_nested(&dentry->d_parent->d_lock,
2514 DENTRY_D_LOCK_NESTED);
2515 }
2516 }
2517 if (target < dentry) {
2518 spin_lock_nested(&target->d_lock, 2);
2519 spin_lock_nested(&dentry->d_lock, 3);
2520 } else {
2521 spin_lock_nested(&dentry->d_lock, 2);
2522 spin_lock_nested(&target->d_lock, 3);
2523 }
2524}
2525
2526static void dentry_unlock_parents_for_move(struct dentry *dentry,
2527 struct dentry *target)
2528{
2529 if (target->d_parent != dentry->d_parent)
2530 spin_unlock(&dentry->d_parent->d_lock);
2531 if (target->d_parent != target)
2532 spin_unlock(&target->d_parent->d_lock);
2533}
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002536 * When switching names, the actual string doesn't strictly have to
2537 * be preserved in the target - because we're dropping the target
2538 * anyway. As such, we can just do a simple memcpy() to copy over
2539 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002541 * Note that we have to be a lot more careful about getting the hash
2542 * switched - we have to switch the hash value properly even if it
2543 * then no longer matches the actual (corrupted) string of the target.
2544 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002546/*
Al Viro18367502011-07-12 21:42:24 -04002547 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 * @dentry: entry to move
2549 * @target: new dentry
2550 *
2551 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002552 * dcache entries should not be moved in this way. Caller must hold
2553 * rename_lock, the i_mutex of the source and target directories,
2554 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Al Viro18367502011-07-12 21:42:24 -04002556static void __d_move(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (!dentry->d_inode)
2559 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2560
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002561 BUG_ON(d_ancestor(dentry, target));
2562 BUG_ON(d_ancestor(target, dentry));
2563
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002564 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Nick Piggin31e6b012011-01-07 17:49:52 +11002566 write_seqcount_begin(&dentry->d_seq);
2567 write_seqcount_begin(&target->d_seq);
2568
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002569 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2570
2571 /*
2572 * Move the dentry to the target hash queue. Don't bother checking
2573 * for the same hash queue because of how unlikely it is.
2574 */
2575 __d_drop(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11002576 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /* Unhash the target: dput() will then get rid of it */
2579 __d_drop(target);
2580
Eric Dumazet5160ee62006-01-08 01:03:32 -08002581 list_del(&dentry->d_u.d_child);
2582 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /* Switch the names.. */
2585 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002586 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 /* ... and switch the parents */
2589 if (IS_ROOT(dentry)) {
2590 dentry->d_parent = target->d_parent;
2591 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002592 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002594 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002597 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
Eric Dumazet5160ee62006-01-08 01:03:32 -08002600 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002601
Nick Piggin31e6b012011-01-07 17:49:52 +11002602 write_seqcount_end(&target->d_seq);
2603 write_seqcount_end(&dentry->d_seq);
2604
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002605 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002607 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 spin_unlock(&dentry->d_lock);
Al Viro18367502011-07-12 21:42:24 -04002609}
2610
2611/*
2612 * d_move - move a dentry
2613 * @dentry: entry to move
2614 * @target: new dentry
2615 *
2616 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002617 * dcache entries should not be moved in this way. See the locking
2618 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002619 */
2620void d_move(struct dentry *dentry, struct dentry *target)
2621{
2622 write_seqlock(&rename_lock);
2623 __d_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002625}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002626EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002628/**
2629 * d_ancestor - search for an ancestor
2630 * @p1: ancestor dentry
2631 * @p2: child dentry
2632 *
2633 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2634 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002635 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002636struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002637{
2638 struct dentry *p;
2639
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002640 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002641 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002642 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002643 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002644 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002645}
2646
2647/*
2648 * This helper attempts to cope with remotely renamed directories
2649 *
2650 * It assumes that the caller is already holding
Al Viro18367502011-07-12 21:42:24 -04002651 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002652 *
2653 * Note: If ever the locking in lock_rename() changes, then please
2654 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002655 */
Nick Piggin873feea2011-01-07 17:50:06 +11002656static struct dentry *__d_unalias(struct inode *inode,
2657 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002658{
2659 struct mutex *m1 = NULL, *m2 = NULL;
Al Viroee3efa92012-06-08 15:59:33 -04002660 struct dentry *ret = ERR_PTR(-EBUSY);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002661
2662 /* If alias and dentry share a parent, then no extra locks required */
2663 if (alias->d_parent == dentry->d_parent)
2664 goto out_unalias;
2665
Trond Myklebust9eaef272006-10-21 10:24:20 -07002666 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002667 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2668 goto out_err;
2669 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2670 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2671 goto out_err;
2672 m2 = &alias->d_parent->d_inode->i_mutex;
2673out_unalias:
Al Viroee3efa92012-06-08 15:59:33 -04002674 if (likely(!d_mountpoint(alias))) {
2675 __d_move(alias, dentry);
2676 ret = alias;
2677 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002678out_err:
Nick Piggin873feea2011-01-07 17:50:06 +11002679 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002680 if (m2)
2681 mutex_unlock(m2);
2682 if (m1)
2683 mutex_unlock(m1);
2684 return ret;
2685}
2686
2687/*
David Howells770bfad2006-08-22 20:06:07 -04002688 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2689 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002690 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002691 */
2692static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2693{
Al Viro740da422013-01-30 10:13:38 -05002694 struct dentry *dparent;
David Howells770bfad2006-08-22 20:06:07 -04002695
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002696 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002697
Nick Piggin31e6b012011-01-07 17:49:52 +11002698 write_seqcount_begin(&dentry->d_seq);
2699 write_seqcount_begin(&anon->d_seq);
2700
David Howells770bfad2006-08-22 20:06:07 -04002701 dparent = dentry->d_parent;
David Howells770bfad2006-08-22 20:06:07 -04002702
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002703 switch_names(dentry, anon);
2704 swap(dentry->d_name.hash, anon->d_name.hash);
2705
Al Viro740da422013-01-30 10:13:38 -05002706 dentry->d_parent = dentry;
2707 list_del_init(&dentry->d_u.d_child);
2708 anon->d_parent = dparent;
Wei Yongjun9ed53b12013-03-12 00:10:50 +08002709 list_move(&anon->d_u.d_child, &dparent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002710
Nick Piggin31e6b012011-01-07 17:49:52 +11002711 write_seqcount_end(&dentry->d_seq);
2712 write_seqcount_end(&anon->d_seq);
2713
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002714 dentry_unlock_parents_for_move(anon, dentry);
2715 spin_unlock(&dentry->d_lock);
2716
2717 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002718 anon->d_flags &= ~DCACHE_DISCONNECTED;
2719}
2720
2721/**
2722 * d_materialise_unique - introduce an inode into the tree
2723 * @dentry: candidate dentry
2724 * @inode: inode to bind to the dentry, to which aliases may be attached
2725 *
2726 * Introduces an dentry into the tree, substituting an extant disconnected
Jeff Laytonc46c8872011-07-26 13:33:16 -04002727 * root directory alias in its place if there is one. Caller must hold the
2728 * i_mutex of the parent directory.
David Howells770bfad2006-08-22 20:06:07 -04002729 */
2730struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2731{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002732 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002733
2734 BUG_ON(!d_unhashed(dentry));
2735
David Howells770bfad2006-08-22 20:06:07 -04002736 if (!inode) {
2737 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002738 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002739 d_rehash(actual);
2740 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002741 }
2742
Nick Piggin873feea2011-01-07 17:50:06 +11002743 spin_lock(&inode->i_lock);
Nick Piggin357f8e62011-01-07 17:49:42 +11002744
Trond Myklebust9eaef272006-10-21 10:24:20 -07002745 if (S_ISDIR(inode->i_mode)) {
2746 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002747
Trond Myklebust9eaef272006-10-21 10:24:20 -07002748 /* Does an aliased dentry already exist? */
Linus Torvalds32ba9c32012-06-08 10:34:03 -07002749 alias = __d_find_alias(inode, 0);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002750 if (alias) {
2751 actual = alias;
Al Viro18367502011-07-12 21:42:24 -04002752 write_seqlock(&rename_lock);
2753
2754 if (d_ancestor(alias, dentry)) {
2755 /* Check for loops */
2756 actual = ERR_PTR(-ELOOP);
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002757 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04002758 } else if (IS_ROOT(alias)) {
2759 /* Is this an anonymous mountpoint that we
2760 * could splice into our tree? */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002761 __d_materialise_dentry(dentry, alias);
Al Viro18367502011-07-12 21:42:24 -04002762 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002763 __d_drop(alias);
2764 goto found;
Al Viro18367502011-07-12 21:42:24 -04002765 } else {
2766 /* Nope, but we must(!) avoid directory
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002767 * aliasing. This drops inode->i_lock */
Al Viro18367502011-07-12 21:42:24 -04002768 actual = __d_unalias(inode, dentry, alias);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002769 }
Al Viro18367502011-07-12 21:42:24 -04002770 write_sequnlock(&rename_lock);
David Howellsdd179942011-08-16 15:31:30 +01002771 if (IS_ERR(actual)) {
2772 if (PTR_ERR(actual) == -ELOOP)
2773 pr_warn_ratelimited(
2774 "VFS: Lookup of '%s' in %s %s"
2775 " would have caused loop\n",
2776 dentry->d_name.name,
2777 inode->i_sb->s_type->name,
2778 inode->i_sb->s_id);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002779 dput(alias);
David Howellsdd179942011-08-16 15:31:30 +01002780 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002781 goto out_nolock;
2782 }
David Howells770bfad2006-08-22 20:06:07 -04002783 }
2784
2785 /* Add a unique reference */
2786 actual = __d_instantiate_unique(dentry, inode);
2787 if (!actual)
2788 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002789 else
2790 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002791
David Howells770bfad2006-08-22 20:06:07 -04002792 spin_lock(&actual->d_lock);
2793found:
2794 _d_rehash(actual);
2795 spin_unlock(&actual->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002796 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002797out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002798 if (actual == dentry) {
2799 security_d_instantiate(dentry, inode);
2800 return NULL;
2801 }
2802
2803 iput(inode);
2804 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002805}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002806EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002807
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002808static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002809{
2810 *buflen -= namelen;
2811 if (*buflen < 0)
2812 return -ENAMETOOLONG;
2813 *buffer -= namelen;
2814 memcpy(*buffer, str, namelen);
2815 return 0;
2816}
2817
Waiman Long232d2d62013-09-09 12:18:13 -04002818/**
2819 * prepend_name - prepend a pathname in front of current buffer pointer
Waiman Long18129972013-09-12 10:55:35 -04002820 * @buffer: buffer pointer
2821 * @buflen: allocated length of the buffer
2822 * @name: name string and length qstr structure
Waiman Long232d2d62013-09-09 12:18:13 -04002823 *
2824 * With RCU path tracing, it may race with d_move(). Use ACCESS_ONCE() to
2825 * make sure that either the old or the new name pointer and length are
2826 * fetched. However, there may be mismatch between length and pointer.
2827 * The length cannot be trusted, we need to copy it byte-by-byte until
2828 * the length is reached or a null byte is found. It also prepends "/" at
2829 * the beginning of the name. The sequence number check at the caller will
2830 * retry it again when a d_move() does happen. So any garbage in the buffer
2831 * due to mismatched pointer and length will be discarded.
2832 */
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002833static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2834{
Waiman Long232d2d62013-09-09 12:18:13 -04002835 const char *dname = ACCESS_ONCE(name->name);
2836 u32 dlen = ACCESS_ONCE(name->len);
2837 char *p;
2838
2839 if (*buflen < dlen + 1)
2840 return -ENAMETOOLONG;
2841 *buflen -= dlen + 1;
2842 p = *buffer -= dlen + 1;
2843 *p++ = '/';
2844 while (dlen--) {
2845 char c = *dname++;
2846 if (!c)
2847 break;
2848 *p++ = c;
2849 }
2850 return 0;
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853/**
Randy Dunlap208898c2010-11-18 15:02:49 -08002854 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002855 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002856 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002857 * @buffer: pointer to the end of the buffer
2858 * @buflen: pointer to buffer length
2859 *
Waiman Long18129972013-09-12 10:55:35 -04002860 * The function will first try to write out the pathname without taking any
2861 * lock other than the RCU read lock to make sure that dentries won't go away.
2862 * It only checks the sequence number of the global rename_lock as any change
2863 * in the dentry's d_seq will be preceded by changes in the rename_lock
2864 * sequence number. If the sequence number had been changed, it will restart
2865 * the whole pathname back-tracing sequence again by taking the rename_lock.
2866 * In this case, there is no need to take the RCU read lock as the recursive
2867 * parent pointer references will keep the dentry chain alive as long as no
2868 * rename operation is performed.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002869 */
Al Viro02125a82011-12-05 08:43:34 -05002870static int prepend_path(const struct path *path,
2871 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002872 char **buffer, int *buflen)
2873{
2874 struct dentry *dentry = path->dentry;
2875 struct vfsmount *vfsmnt = path->mnt;
Al Viro0714a532011-11-24 22:19:58 -05002876 struct mount *mnt = real_mount(vfsmnt);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002877 int error = 0;
Al Viro48a066e2013-09-29 22:06:07 -04002878 unsigned seq, m_seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002879 char *bptr;
2880 int blen;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002881
Al Viro48f5ec22013-09-09 15:22:25 -04002882 rcu_read_lock();
Al Viro48a066e2013-09-29 22:06:07 -04002883restart_mnt:
2884 read_seqbegin_or_lock(&mount_lock, &m_seq);
2885 seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002886restart:
2887 bptr = *buffer;
2888 blen = *buflen;
Al Viro48a066e2013-09-29 22:06:07 -04002889 error = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04002890 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002891 while (dentry != root->dentry || vfsmnt != root->mnt) {
2892 struct dentry * parent;
2893
2894 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Al Viro48a066e2013-09-29 22:06:07 -04002895 struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002896 /* Global root? */
Al Viro48a066e2013-09-29 22:06:07 -04002897 if (mnt != parent) {
2898 dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
2899 mnt = parent;
Waiman Long232d2d62013-09-09 12:18:13 -04002900 vfsmnt = &mnt->mnt;
2901 continue;
2902 }
2903 /*
2904 * Filesystems needing to implement special "root names"
2905 * should do so with ->d_dname()
2906 */
2907 if (IS_ROOT(dentry) &&
2908 (dentry->d_name.len != 1 ||
2909 dentry->d_name.name[0] != '/')) {
2910 WARN(1, "Root dentry has weird name <%.*s>\n",
2911 (int) dentry->d_name.len,
2912 dentry->d_name.name);
2913 }
2914 if (!error)
2915 error = is_mounted(vfsmnt) ? 1 : 2;
2916 break;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002917 }
2918 parent = dentry->d_parent;
2919 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04002920 error = prepend_name(&bptr, &blen, &dentry->d_name);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002921 if (error)
2922 break;
2923
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002924 dentry = parent;
2925 }
Al Viro48f5ec22013-09-09 15:22:25 -04002926 if (!(seq & 1))
2927 rcu_read_unlock();
2928 if (need_seqretry(&rename_lock, seq)) {
2929 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04002930 goto restart;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002931 }
Al Viro48f5ec22013-09-09 15:22:25 -04002932 done_seqretry(&rename_lock, seq);
Al Viro48a066e2013-09-29 22:06:07 -04002933 if (need_seqretry(&mount_lock, m_seq)) {
2934 m_seq = 1;
2935 goto restart_mnt;
2936 }
2937 done_seqretry(&mount_lock, m_seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002938
Waiman Long232d2d62013-09-09 12:18:13 -04002939 if (error >= 0 && bptr == *buffer) {
2940 if (--blen < 0)
2941 error = -ENAMETOOLONG;
2942 else
2943 *--bptr = '/';
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002944 }
Waiman Long232d2d62013-09-09 12:18:13 -04002945 *buffer = bptr;
2946 *buflen = blen;
Al Viro7ea600b2013-03-26 18:25:57 -04002947 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002948}
2949
2950/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002951 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002952 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002953 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07002954 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 * @buflen: buffer length
2956 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002957 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002959 * Returns a pointer into the buffer or an error code if the
2960 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002961 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002962 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002963 *
Al Viro02125a82011-12-05 08:43:34 -05002964 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
Al Viro02125a82011-12-05 08:43:34 -05002966char *__d_path(const struct path *path,
2967 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002968 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002970 char *res = buf + buflen;
2971 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002973 prepend(&res, &buflen, "\0", 1);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002974 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002975
Al Viro02125a82011-12-05 08:43:34 -05002976 if (error < 0)
2977 return ERR_PTR(error);
2978 if (error > 0)
2979 return NULL;
2980 return res;
2981}
2982
2983char *d_absolute_path(const struct path *path,
2984 char *buf, int buflen)
2985{
2986 struct path root = {};
2987 char *res = buf + buflen;
2988 int error;
2989
2990 prepend(&res, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05002991 error = prepend_path(path, &root, &res, &buflen);
Al Viro02125a82011-12-05 08:43:34 -05002992
2993 if (error > 1)
2994 error = -EINVAL;
2995 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002996 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002997 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003000/*
3001 * same as __d_path but appends "(deleted)" for unlinked files.
3002 */
Al Viro02125a82011-12-05 08:43:34 -05003003static int path_with_deleted(const struct path *path,
3004 const struct path *root,
3005 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003006{
3007 prepend(buf, buflen, "\0", 1);
3008 if (d_unlinked(path->dentry)) {
3009 int error = prepend(buf, buflen, " (deleted)", 10);
3010 if (error)
3011 return error;
3012 }
3013
3014 return prepend_path(path, root, buf, buflen);
3015}
3016
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003017static int prepend_unreachable(char **buffer, int *buflen)
3018{
3019 return prepend(buffer, buflen, "(unreachable)", 13);
3020}
3021
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003022static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3023{
3024 unsigned seq;
3025
3026 do {
3027 seq = read_seqcount_begin(&fs->seq);
3028 *root = fs->root;
3029 } while (read_seqcount_retry(&fs->seq, seq));
3030}
3031
Jan Bluncka03a8a702008-02-14 19:38:32 -08003032/**
3033 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08003034 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08003035 * @buf: buffer to return value in
3036 * @buflen: buffer length
3037 *
3038 * Convert a dentry into an ASCII path name. If the entry has been deleted
3039 * the string " (deleted)" is appended. Note that this is ambiguous.
3040 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003041 * Returns a pointer into the buffer or an error code if the path was
3042 * too long. Note: Callers should use the returned pointer, not the passed
3043 * in buffer, to use the name! The implementation often starts at an offset
3044 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003045 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003046 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003047 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07003048char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003050 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003051 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003052 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003054 /*
3055 * We have various synthetic filesystems that never get mounted. On
3056 * these filesystems dentries are never used for lookup purposes, and
3057 * thus don't need to be hashed. They also don't need a name until a
3058 * user wants to identify the object in /proc/pid/fd/. The little hack
3059 * below allows us to generate a name for these objects on demand:
3060 */
Jan Blunckcf28b482008-02-14 19:38:44 -08003061 if (path->dentry->d_op && path->dentry->d_op->d_dname)
3062 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003063
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003064 rcu_read_lock();
3065 get_fs_root_rcu(current->fs, &root);
Al Viro02125a82011-12-05 08:43:34 -05003066 error = path_with_deleted(path, &root, &res, &buflen);
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003067 rcu_read_unlock();
3068
Al Viro02125a82011-12-05 08:43:34 -05003069 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003070 res = ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return res;
3072}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003073EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003076 * Helper function for dentry_operations.d_dname() members
3077 */
3078char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3079 const char *fmt, ...)
3080{
3081 va_list args;
3082 char temp[64];
3083 int sz;
3084
3085 va_start(args, fmt);
3086 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3087 va_end(args);
3088
3089 if (sz > sizeof(temp) || sz > buflen)
3090 return ERR_PTR(-ENAMETOOLONG);
3091
3092 buffer += buflen - sz;
3093 return memcpy(buffer, temp, sz);
3094}
3095
Al Viro118b2302013-08-24 12:08:17 -04003096char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3097{
3098 char *end = buffer + buflen;
3099 /* these dentries are never renamed, so d_lock is not needed */
3100 if (prepend(&end, &buflen, " (deleted)", 11) ||
Waiman Long232d2d62013-09-09 12:18:13 -04003101 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
Al Viro118b2302013-08-24 12:08:17 -04003102 prepend(&end, &buflen, "/", 1))
3103 end = ERR_PTR(-ENAMETOOLONG);
Waiman Long232d2d62013-09-09 12:18:13 -04003104 return end;
Al Viro118b2302013-08-24 12:08:17 -04003105}
3106
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003107/*
Ram Pai6092d042008-03-27 13:06:20 +01003108 * Write full pathname from the root of the filesystem into the buffer.
3109 */
Nick Pigginec2447c2011-01-07 17:49:29 +11003110static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01003111{
Waiman Long232d2d62013-09-09 12:18:13 -04003112 char *end, *retval;
3113 int len, seq = 0;
3114 int error = 0;
Ram Pai6092d042008-03-27 13:06:20 +01003115
Al Viro48f5ec22013-09-09 15:22:25 -04003116 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003117restart:
3118 end = buf + buflen;
3119 len = buflen;
3120 prepend(&end, &len, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01003121 if (buflen < 1)
3122 goto Elong;
3123 /* Get '/' right */
3124 retval = end-1;
3125 *retval = '/';
Waiman Long232d2d62013-09-09 12:18:13 -04003126 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003127 while (!IS_ROOT(dentry)) {
3128 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11003129 int error;
Ram Pai6092d042008-03-27 13:06:20 +01003130
Ram Pai6092d042008-03-27 13:06:20 +01003131 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003132 error = prepend_name(&end, &len, &dentry->d_name);
3133 if (error)
3134 break;
Ram Pai6092d042008-03-27 13:06:20 +01003135
3136 retval = end;
3137 dentry = parent;
3138 }
Al Viro48f5ec22013-09-09 15:22:25 -04003139 if (!(seq & 1))
3140 rcu_read_unlock();
3141 if (need_seqretry(&rename_lock, seq)) {
3142 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003143 goto restart;
Al Viro48f5ec22013-09-09 15:22:25 -04003144 }
3145 done_seqretry(&rename_lock, seq);
Waiman Long232d2d62013-09-09 12:18:13 -04003146 if (error)
3147 goto Elong;
Al Viroc1031352010-06-06 22:31:14 -04003148 return retval;
3149Elong:
3150 return ERR_PTR(-ENAMETOOLONG);
3151}
Nick Pigginec2447c2011-01-07 17:49:29 +11003152
3153char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3154{
Waiman Long232d2d62013-09-09 12:18:13 -04003155 return __dentry_path(dentry, buf, buflen);
Nick Pigginec2447c2011-01-07 17:49:29 +11003156}
3157EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04003158
3159char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3160{
3161 char *p = NULL;
3162 char *retval;
3163
Al Viroc1031352010-06-06 22:31:14 -04003164 if (d_unlinked(dentry)) {
3165 p = buf + buflen;
3166 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3167 goto Elong;
3168 buflen++;
3169 }
3170 retval = __dentry_path(dentry, buf, buflen);
Al Viroc1031352010-06-06 22:31:14 -04003171 if (!IS_ERR(retval) && p)
3172 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01003173 return retval;
3174Elong:
Ram Pai6092d042008-03-27 13:06:20 +01003175 return ERR_PTR(-ENAMETOOLONG);
3176}
3177
Linus Torvalds8b19e342013-09-12 10:35:47 -07003178static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3179 struct path *pwd)
Linus Torvalds57624822013-09-12 10:12:47 -07003180{
Linus Torvalds8b19e342013-09-12 10:35:47 -07003181 unsigned seq;
3182
3183 do {
3184 seq = read_seqcount_begin(&fs->seq);
3185 *root = fs->root;
3186 *pwd = fs->pwd;
3187 } while (read_seqcount_retry(&fs->seq, seq));
Linus Torvalds57624822013-09-12 10:12:47 -07003188}
3189
Ram Pai6092d042008-03-27 13:06:20 +01003190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 * NOTE! The user-level library version returns a
3192 * character pointer. The kernel system call just
3193 * returns the length of the buffer filled (which
3194 * includes the ending '\0' character), or a negative
3195 * error value. So libc would do something like
3196 *
3197 * char *getcwd(char * buf, size_t size)
3198 * {
3199 * int retval;
3200 *
3201 * retval = sys_getcwd(buf, size);
3202 * if (retval >= 0)
3203 * return buf;
3204 * errno = -retval;
3205 * return NULL;
3206 * }
3207 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01003208SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Linus Torvalds552ce542007-02-13 12:08:18 -08003210 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003211 struct path pwd, root;
Linus Torvalds3272c542013-09-12 12:40:15 -07003212 char *page = __getname();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 if (!page)
3215 return -ENOMEM;
3216
Linus Torvalds8b19e342013-09-12 10:35:47 -07003217 rcu_read_lock();
3218 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Linus Torvalds552ce542007-02-13 12:08:18 -08003220 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003221 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08003222 unsigned long len;
Linus Torvalds3272c542013-09-12 12:40:15 -07003223 char *cwd = page + PATH_MAX;
3224 int buflen = PATH_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003226 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003227 error = prepend_path(&pwd, &root, &cwd, &buflen);
Linus Torvaldsff812d72013-09-12 11:57:01 -07003228 rcu_read_unlock();
Linus Torvalds552ce542007-02-13 12:08:18 -08003229
Al Viro02125a82011-12-05 08:43:34 -05003230 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08003231 goto out;
3232
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003233 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05003234 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003235 error = prepend_unreachable(&cwd, &buflen);
3236 if (error)
3237 goto out;
3238 }
3239
Linus Torvalds552ce542007-02-13 12:08:18 -08003240 error = -ERANGE;
Linus Torvalds3272c542013-09-12 12:40:15 -07003241 len = PATH_MAX + page - cwd;
Linus Torvalds552ce542007-02-13 12:08:18 -08003242 if (len <= size) {
3243 error = len;
3244 if (copy_to_user(buf, cwd, len))
3245 error = -EFAULT;
3246 }
Nick Piggin949854d2011-01-07 17:49:37 +11003247 } else {
Linus Torvaldsff812d72013-09-12 11:57:01 -07003248 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +11003249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251out:
Linus Torvalds3272c542013-09-12 12:40:15 -07003252 __putname(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return error;
3254}
3255
3256/*
3257 * Test whether new_dentry is a subdirectory of old_dentry.
3258 *
3259 * Trivially implemented using the dcache structure
3260 */
3261
3262/**
3263 * is_subdir - is new dentry a subdirectory of old_dentry
3264 * @new_dentry: new dentry
3265 * @old_dentry: old dentry
3266 *
3267 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
3268 * Returns 0 otherwise.
3269 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3270 */
3271
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003272int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11003275 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003277 if (new_dentry == old_dentry)
3278 return 1;
3279
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003280 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003283 /*
3284 * Need rcu_readlock to protect against the d_parent trashing
3285 * due to d_move
3286 */
3287 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003288 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003290 else
3291 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11003292 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 return result;
3296}
3297
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003298static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003300 struct dentry *root = data;
3301 if (dentry != root) {
3302 if (d_unhashed(dentry) || !dentry->d_inode)
3303 return D_WALK_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Miklos Szeredi01ddc4e2013-09-05 11:44:34 +02003305 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3306 dentry->d_flags |= DCACHE_GENOCIDE;
3307 dentry->d_lockref.count--;
3308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003310 return D_WALK_CONTINUE;
3311}
Nick Piggin58db63d2011-01-07 17:49:39 +11003312
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003313void d_genocide(struct dentry *parent)
3314{
3315 d_walk(parent, parent, d_genocide_kill, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Al Viro60545d02013-06-07 01:20:27 -04003318void d_tmpfile(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Al Viro60545d02013-06-07 01:20:27 -04003320 inode_dec_link_count(inode);
3321 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3322 !hlist_unhashed(&dentry->d_alias) ||
3323 !d_unlinked(dentry));
3324 spin_lock(&dentry->d_parent->d_lock);
3325 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3326 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3327 (unsigned long long)inode->i_ino);
3328 spin_unlock(&dentry->d_lock);
3329 spin_unlock(&dentry->d_parent->d_lock);
3330 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
Al Viro60545d02013-06-07 01:20:27 -04003332EXPORT_SYMBOL(d_tmpfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334static __initdata unsigned long dhash_entries;
3335static int __init set_dhash_entries(char *str)
3336{
3337 if (!str)
3338 return 0;
3339 dhash_entries = simple_strtoul(str, &str, 0);
3340 return 1;
3341}
3342__setup("dhash_entries=", set_dhash_entries);
3343
3344static void __init dcache_init_early(void)
3345{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003346 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 /* If hashes are distributed across NUMA nodes, defer
3349 * hash allocation until vmalloc space is available.
3350 */
3351 if (hashdist)
3352 return;
3353
3354 dentry_hashtable =
3355 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003356 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 dhash_entries,
3358 13,
3359 HASH_EARLY,
3360 &d_hash_shift,
3361 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003362 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 0);
3364
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003365 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003366 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Denis Cheng74bf17c2007-10-16 23:26:30 -07003369static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003371 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 /*
3374 * A constructor could be added for stable state like the lists,
3375 * but it is probably not worth it because of the cache nature
3376 * of the dcache.
3377 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003378 dentry_cache = KMEM_CACHE(dentry,
3379 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
3381 /* Hash may have been set up in dcache_init_early */
3382 if (!hashdist)
3383 return;
3384
3385 dentry_hashtable =
3386 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003387 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 dhash_entries,
3389 13,
3390 0,
3391 &d_hash_shift,
3392 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003393 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 0);
3395
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003396 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003397 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
3400/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003401struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003402EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404EXPORT_SYMBOL(d_genocide);
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406void __init vfs_caches_init_early(void)
3407{
3408 dcache_init_early();
3409 inode_init_early();
3410}
3411
3412void __init vfs_caches_init(unsigned long mempages)
3413{
3414 unsigned long reserve;
3415
3416 /* Base hash sizes on available memory, with a reserve equal to
3417 150% of current kernel size */
3418
3419 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3420 mempages -= reserve;
3421
3422 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003423 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Denis Cheng74bf17c2007-10-16 23:26:30 -07003425 dcache_init();
3426 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07003428 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 bdev_cache_init();
3430 chrdev_init();
3431}