blob: cee6a60ddf939eb2ac9ec513b8a566dd97eed24c [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>
Thomas Gleixner3c536052012-03-07 21:00:34 +010040#include <linux/delay.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:
Al Viro6637ecd2014-10-26 19:19:16 -040047 * - i_dentry, d_u.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)
Nick Piggin23044502011-01-07 17:49:31 +110052 * dcache_lru_lock protects:
53 * - 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
Al Viro6637ecd2014-10-26 19:19:16 -040062 * - d_u.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
67 * dcache_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
Nick Piggin23044502011-01-07 17:49:31 +110085static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040086__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Nick Piggin949854d2011-01-07 17:49:37 +110088EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Christoph Lametere18b8902006-12-06 20:33:20 -080090static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * This is the single most critical data structure when it comes
94 * to the dcache: the hashtable for lookups. Somebody should try
95 * to make this good - I've just made it work.
96 *
97 * This hash-function tries to avoid losing too many bits of hash
98 * information, yet avoid using a prime hash-size or similar.
99 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800101static unsigned int d_hash_mask __read_mostly;
102static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100103
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700104static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100105
Linus Torvalds8966be92012-03-02 14:23:30 -0800106static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700107 unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100108{
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700109 hash += (unsigned long) parent / L1_CACHE_BYTES;
Linus Torvaldsd4c96062014-09-13 11:30:10 -0700110 return dentry_hashtable + hash_32(hash, d_hash_shift);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* Statistics gathering. */
114struct dentry_stat_t dentry_stat = {
115 .age_limit = 45,
116};
117
Nick Piggin3e880fb2011-01-07 17:49:19 +1100118static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400119
120#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100121static int get_nr_dentry(void)
122{
123 int i;
124 int sum = 0;
125 for_each_possible_cpu(i)
126 sum += per_cpu(nr_dentry, i);
127 return sum < 0 ? 0 : sum;
128}
129
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400130int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
131 size_t *lenp, loff_t *ppos)
132{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100133 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400134 return proc_dointvec(table, write, buffer, lenp, ppos);
135}
136#endif
137
Linus Torvalds5483f182012-03-04 15:51:42 -0800138/*
139 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
140 * The strings are both count bytes long, and count is non-zero.
141 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700142#ifdef CONFIG_DCACHE_WORD_ACCESS
143
144#include <asm/word-at-a-time.h>
145/*
146 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
147 * aligned allocation for this particular component. We don't
148 * strictly need the load_unaligned_zeropad() safety, but it
149 * doesn't hurt either.
150 *
151 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
152 * need the careful unaligned handling.
153 */
Linus Torvalds94753db52012-05-10 12:19:19 -0700154static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800155{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800156 unsigned long a,b,mask;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800157
158 for (;;) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -0700159 a = *(unsigned long *)cs;
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700160 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800161 if (tcount < sizeof(unsigned long))
162 break;
163 if (unlikely(a != b))
164 return 1;
165 cs += sizeof(unsigned long);
166 ct += sizeof(unsigned long);
167 tcount -= sizeof(unsigned long);
168 if (!tcount)
169 return 0;
170 }
171 mask = ~(~0ul << tcount*8);
172 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700173}
174
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800175#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700176
Linus Torvalds94753db52012-05-10 12:19:19 -0700177static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700178{
Linus Torvalds5483f182012-03-04 15:51:42 -0800179 do {
180 if (*cs != *ct)
181 return 1;
182 cs++;
183 ct++;
184 tcount--;
185 } while (tcount);
186 return 0;
187}
188
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700189#endif
190
Linus Torvalds94753db52012-05-10 12:19:19 -0700191static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
192{
Linus Torvalds6326c712012-05-21 16:14:04 -0700193 const unsigned char *cs;
Linus Torvalds94753db52012-05-10 12:19:19 -0700194 /*
195 * Be careful about RCU walk racing with rename:
196 * use ACCESS_ONCE to fetch the name pointer.
197 *
198 * NOTE! Even if a rename will mean that the length
199 * was not loaded atomically, we don't care. The
200 * RCU walk will check the sequence count eventually,
201 * and catch it. And we won't overrun the buffer,
202 * because we're reading the name pointer atomically,
203 * and a dentry name is guaranteed to be properly
204 * terminated with a NUL byte.
205 *
206 * End result: even if 'len' is wrong, we'll exit
207 * early because the data cannot match (there can
208 * be no NUL in the ct/tcount data)
209 */
Linus Torvalds6326c712012-05-21 16:14:04 -0700210 cs = ACCESS_ONCE(dentry->d_name.name);
211 smp_read_barrier_depends();
212 return dentry_string_cmp(cs, ct, tcount);
Linus Torvalds94753db52012-05-10 12:19:19 -0700213}
214
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400215static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400217 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dname_external(dentry))
220 kfree(dentry->d_name.name);
221 kmem_cache_free(dentry_cache, dentry);
222}
223
224/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100225 * no locks, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227static void d_free(struct dentry *dentry)
228{
Al Viro6637ecd2014-10-26 19:19:16 -0400229 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100230 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100231 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (dentry->d_op && dentry->d_op->d_release)
233 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400234
Linus Torvaldsdea36672011-04-24 07:58:46 -0700235 /* if dentry was never visible to RCU, immediate free is OK */
236 if (!(dentry->d_flags & DCACHE_RCUACCESS))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400237 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800238 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400239 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Nick Piggin31e6b012011-01-07 17:49:52 +1100242/**
243 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800244 * @dentry: the target dentry
Nick Piggin31e6b012011-01-07 17:49:52 +1100245 * After this call, in-progress rcu-walk path lookup will fail. This
246 * should be called after unhashing, and after changing d_inode (if
247 * the dentry has not already been unhashed).
248 */
249static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
250{
251 assert_spin_locked(&dentry->d_lock);
252 /* Go through a barrier */
253 write_seqcount_barrier(&dentry->d_seq);
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * Release the dentry's inode, using the filesystem
Nick Piggin31e6b012011-01-07 17:49:52 +1100258 * d_iput() operation if defined. Dentry has no refcount
259 * and is unhashed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800261static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200262 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100263 __releases(dentry->d_inode->i_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct inode *inode = dentry->d_inode;
266 if (inode) {
267 dentry->d_inode = NULL;
Al Viro6637ecd2014-10-26 19:19:16 -0400268 hlist_del_init(&dentry->d_u.d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100270 spin_unlock(&inode->i_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700271 if (!inode->i_nlink)
272 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (dentry->d_op && dentry->d_op->d_iput)
274 dentry->d_op->d_iput(dentry, inode);
275 else
276 iput(inode);
277 } else {
278 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280}
281
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700282/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100283 * Release the dentry's inode, using the filesystem
284 * d_iput() operation if defined. dentry remains in-use.
285 */
286static void dentry_unlink_inode(struct dentry * dentry)
287 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100288 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100289{
290 struct inode *inode = dentry->d_inode;
291 dentry->d_inode = NULL;
Al Viro6637ecd2014-10-26 19:19:16 -0400292 hlist_del_init(&dentry->d_u.d_alias);
Nick Piggin31e6b012011-01-07 17:49:52 +1100293 dentry_rcuwalk_barrier(dentry);
294 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100295 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100296 if (!inode->i_nlink)
297 fsnotify_inoderemove(inode);
298 if (dentry->d_op && dentry->d_op->d_iput)
299 dentry->d_op->d_iput(dentry, inode);
300 else
301 iput(inode);
302}
303
304/*
Sage Weilf0023bc2011-10-28 10:02:42 -0700305 * dentry_lru_(add|del|prune|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700306 */
307static void dentry_lru_add(struct dentry *dentry)
308{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400309 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100310 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400311 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
312 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100313 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100314 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400315 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700316}
317
Nick Piggin23044502011-01-07 17:49:31 +1100318static void __dentry_lru_del(struct dentry *dentry)
319{
320 list_del_init(&dentry->d_lru);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100321 dentry->d_flags &= ~DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100322 dentry->d_sb->s_nr_dentry_unused--;
323 dentry_stat.nr_unused--;
324}
325
Sage Weilf0023bc2011-10-28 10:02:42 -0700326/*
327 * Remove a dentry with references from the LRU.
328 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700329static void dentry_lru_del(struct dentry *dentry)
330{
331 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100332 spin_lock(&dcache_lru_lock);
333 __dentry_lru_del(dentry);
334 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700335 }
336}
337
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000338static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700339{
Nick Piggin23044502011-01-07 17:49:31 +1100340 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400341 if (list_empty(&dentry->d_lru)) {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000342 list_add_tail(&dentry->d_lru, list);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400343 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100344 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400345 } else {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000346 list_move_tail(&dentry->d_lru, list);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700347 }
Nick Piggin23044502011-01-07 17:49:31 +1100348 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700349}
350
Miklos Szeredid52b9082007-05-08 00:23:46 -0700351/**
352 * d_kill - kill dentry and return parent
353 * @dentry: dentry to kill
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800354 * @parent: parent dentry
Miklos Szeredid52b9082007-05-08 00:23:46 -0700355 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200356 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700357 *
358 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100359 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100360 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
361 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700362 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100363static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200364 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100365 __releases(parent->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100366 __releases(dentry->d_inode->i_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700367{
Al Viro5f03ac12014-10-26 19:31:10 -0400368 __list_del_entry(&dentry->d_child);
Trond Myklebustc83ce982011-03-15 13:36:43 -0400369 /*
Al Viro5f03ac12014-10-26 19:31:10 -0400370 * Inform ascending readers that we are no longer attached to the
Trond Myklebustc83ce982011-03-15 13:36:43 -0400371 * dentry tree
372 */
Miklos Szeredib161dfa62012-09-17 22:31:38 +0200373 dentry->d_flags |= DCACHE_DENTRY_KILLED;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100374 if (parent)
375 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700376 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100377 /*
378 * dentry_iput drops the locks, at which point nobody (except
379 * transient RCU lookups) can reach this dentry.
380 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700381 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900382 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700383}
384
David Howellsc6627c62011-06-07 14:09:20 +0100385/*
386 * Unhash a dentry without inserting an RCU walk barrier or checking that
387 * dentry->d_lock is locked. The caller must take care of that, if
388 * appropriate.
389 */
390static void __d_shrink(struct dentry *dentry)
391{
392 if (!d_unhashed(dentry)) {
393 struct hlist_bl_head *b;
394 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
395 b = &dentry->d_sb->s_anon;
396 else
397 b = d_hash(dentry->d_parent, dentry->d_name.hash);
398
399 hlist_bl_lock(b);
400 __hlist_bl_del(&dentry->d_hash);
401 dentry->d_hash.pprev = NULL;
402 hlist_bl_unlock(b);
403 }
404}
405
Nick Piggin789680d2011-01-07 17:49:30 +1100406/**
407 * d_drop - drop a dentry
408 * @dentry: dentry to drop
409 *
410 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
411 * be found through a VFS lookup any more. Note that this is different from
412 * deleting the dentry - d_delete will try to mark the dentry negative if
413 * possible, giving a successful _negative_ lookup, while d_drop will
414 * just make the cache lookup fail.
415 *
416 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
417 * reason (NFS timeouts or autofs deletes).
418 *
419 * __d_drop requires dentry->d_lock.
420 */
421void __d_drop(struct dentry *dentry)
422{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700423 if (!d_unhashed(dentry)) {
David Howellsc6627c62011-06-07 14:09:20 +0100424 __d_shrink(dentry);
Linus Torvaldsdea36672011-04-24 07:58:46 -0700425 dentry_rcuwalk_barrier(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +1100426 }
427}
428EXPORT_SYMBOL(__d_drop);
429
430void d_drop(struct dentry *dentry)
431{
Nick Piggin789680d2011-01-07 17:49:30 +1100432 spin_lock(&dentry->d_lock);
433 __d_drop(dentry);
434 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100435}
436EXPORT_SYMBOL(d_drop);
437
Nick Piggin77812a1e2011-01-07 17:49:48 +1100438/*
439 * Finish off a dentry we've decided to kill.
440 * dentry->d_lock must be held, returns with it unlocked.
441 * If ref is non-zero, then decrement the refcount too.
442 * Returns dentry requiring refcount drop, or NULL if we're done.
443 */
444static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
445 __releases(dentry->d_lock)
446{
Nick Piggin873feea2011-01-07 17:50:06 +1100447 struct inode *inode;
Nick Piggin77812a1e2011-01-07 17:49:48 +1100448 struct dentry *parent;
449
Nick Piggin873feea2011-01-07 17:50:06 +1100450 inode = dentry->d_inode;
451 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin77812a1e2011-01-07 17:49:48 +1100452relock:
453 spin_unlock(&dentry->d_lock);
Thomas Gleixner3c536052012-03-07 21:00:34 +0100454 cpu_chill();
Nick Piggin77812a1e2011-01-07 17:49:48 +1100455 return dentry; /* try again with same dentry */
456 }
457 if (IS_ROOT(dentry))
458 parent = NULL;
459 else
460 parent = dentry->d_parent;
461 if (parent && !spin_trylock(&parent->d_lock)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100462 if (inode)
463 spin_unlock(&inode->i_lock);
Nick Piggin77812a1e2011-01-07 17:49:48 +1100464 goto relock;
465 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100466
Nick Piggin77812a1e2011-01-07 17:49:48 +1100467 if (ref)
468 dentry->d_count--;
Sage Weilf0023bc2011-10-28 10:02:42 -0700469 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700470 * inform the fs via d_prune that this dentry is about to be
471 * unhashed and destroyed.
472 */
Yan, Zheng61572bb2013-04-15 14:13:21 +0800473 if (dentry->d_flags & DCACHE_OP_PRUNE)
474 dentry->d_op->d_prune(dentry);
475
476 dentry_lru_del(dentry);
Nick Piggin77812a1e2011-01-07 17:49:48 +1100477 /* if it was on the hash then remove it */
478 __d_drop(dentry);
479 return d_kill(dentry, parent);
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*
483 * This is dput
484 *
485 * This is complicated by the fact that we do not want to put
486 * dentries that are no longer on any hash chain on the unused
487 * list: we'd much rather just get rid of them immediately.
488 *
489 * However, that implies that we have to traverse the dentry
490 * tree upwards to the parents which might _also_ now be
491 * scheduled for deletion (it may have been only waiting for
492 * its last child to go away).
493 *
494 * This tail recursion is done by hand as we don't want to depend
495 * on the compiler to always get this right (gcc generally doesn't).
496 * Real recursion would eat up our stack space.
497 */
498
499/*
500 * dput - release a dentry
501 * @dentry: dentry to release
502 *
503 * Release a dentry. This will drop the usage count and if appropriate
504 * call the dentry unlink method as well as removing it from the queues and
505 * releasing its resources. If the parent dentries were scheduled for release
506 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508void dput(struct dentry *dentry)
509{
510 if (!dentry)
511 return;
512
513repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100514 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_lock(&dentry->d_lock);
Nick Piggin61f3dee2011-01-07 17:49:40 +1100517 BUG_ON(!dentry->d_count);
518 if (dentry->d_count > 1) {
519 dentry->d_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
522 }
523
Nick Pigginfb045ad2011-01-07 17:49:55 +1100524 if (dentry->d_flags & DCACHE_OP_DELETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100526 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Nick Piggin265ac902010-10-10 05:36:24 -0400528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Unreachable? Get rid of it */
530 if (d_unhashed(dentry))
531 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400532
Jeff Layton39e3c952012-11-28 11:30:53 -0500533 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400534 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400535
Nick Piggin61f3dee2011-01-07 17:49:40 +1100536 dentry->d_count--;
537 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539
Miklos Szeredid52b9082007-05-08 00:23:46 -0700540kill_it:
Nick Piggin77812a1e2011-01-07 17:49:48 +1100541 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700542 if (dentry)
543 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700545EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547/**
548 * d_invalidate - invalidate a dentry
549 * @dentry: dentry to invalidate
550 *
551 * Try to invalidate the dentry if it turns out to be
552 * possible. If there are other dentries that can be
553 * reached through this one we can't delete it and we
554 * return -EBUSY. On success we return 0.
555 *
556 * no dcache lock.
557 */
558
559int d_invalidate(struct dentry * dentry)
560{
561 /*
562 * If it's already been dropped, return OK.
563 */
Nick Pigginda502952011-01-07 17:49:33 +1100564 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100566 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568 }
569 /*
570 * Check whether to do a partial shrink_dcache
571 * to get rid of unused child entries.
572 */
573 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100574 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100576 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579 /*
580 * Somebody else still using it?
581 *
582 * If it's a directory, we can't drop it
583 * for fear of somebody re-populating it
584 * with children (even though dropping it
585 * would make it unreachable from the root,
586 * we might still populate it if it was a
587 * working directory or similar).
Al Viro50e69632011-11-07 16:39:57 +0000588 * We also need to leave mountpoints alone,
589 * directory or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Al Viro50e69632011-11-07 16:39:57 +0000591 if (dentry->d_count > 1 && dentry->d_inode) {
592 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -EBUSY;
595 }
596 }
597
598 __d_drop(dentry);
599 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700602EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100604/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100605static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100607 dentry->d_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Nick Piggindc0474b2011-01-07 17:49:43 +1100610static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100611{
Nick Piggin23044502011-01-07 17:49:31 +1100612 spin_lock(&dentry->d_lock);
Nick Piggindc0474b2011-01-07 17:49:43 +1100613 __dget_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100614 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100615}
616
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100617struct dentry *dget_parent(struct dentry *dentry)
618{
619 struct dentry *ret;
620
621repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100622 /*
623 * Don't need rcu_dereference because we re-check it was correct under
624 * the lock.
625 */
626 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100627 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100628 spin_lock(&ret->d_lock);
629 if (unlikely(ret != dentry->d_parent)) {
630 spin_unlock(&ret->d_lock);
631 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100632 goto repeat;
633 }
Nick Piggina734eb42011-01-07 17:49:44 +1100634 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100635 BUG_ON(!ret->d_count);
636 ret->d_count++;
637 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100638 return ret;
639}
640EXPORT_SYMBOL(dget_parent);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/**
643 * d_find_alias - grab a hashed alias of inode
644 * @inode: inode in question
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700645 * @want_discon: flag, used by d_splice_alias, to request
646 * that only a DISCONNECTED alias be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
648 * If inode has a hashed alias, or is a directory and has any alias,
649 * acquire the reference to alias and return it. Otherwise return NULL.
650 * Notice that if inode is a directory there can be only one alias and
651 * it can be unhashed only if it has no children, or if it is the root
652 * of a filesystem.
653 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700654 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700655 * any other hashed alias over that one unless @want_discon is set,
656 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700658static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Nick Pigginda502952011-01-07 17:49:33 +1100660 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Nick Pigginda502952011-01-07 17:49:33 +1100662again:
663 discon_alias = NULL;
Al Viro6637ecd2014-10-26 19:19:16 -0400664 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100665 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700667 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100668 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 discon_alias = alias;
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700670 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100671 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100672 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return alias;
674 }
675 }
Nick Pigginda502952011-01-07 17:49:33 +1100676 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Nick Pigginda502952011-01-07 17:49:33 +1100678 if (discon_alias) {
679 alias = discon_alias;
680 spin_lock(&alias->d_lock);
681 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
682 if (IS_ROOT(alias) &&
683 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100684 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100685 spin_unlock(&alias->d_lock);
686 return alias;
687 }
688 }
689 spin_unlock(&alias->d_lock);
690 goto again;
691 }
692 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Nick Pigginda502952011-01-07 17:49:33 +1100695struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
David Howells214fda12006-03-25 03:06:36 -0800697 struct dentry *de = NULL;
698
Al Virob3d9b7a2012-06-09 13:51:19 -0400699 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100700 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -0700701 de = __d_find_alias(inode, 0);
Nick Piggin873feea2011-01-07 17:50:06 +1100702 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return de;
705}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700706EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708/*
709 * Try to kill dentries associated with this inode.
710 * WARNING: you must own a reference to inode.
711 */
712void d_prune_aliases(struct inode *inode)
713{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700714 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100716 spin_lock(&inode->i_lock);
Al Viro6637ecd2014-10-26 19:19:16 -0400717 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100719 if (!dentry->d_count) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100720 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 __d_drop(dentry);
722 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100723 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dput(dentry);
725 goto restart;
726 }
727 spin_unlock(&dentry->d_lock);
728 }
Nick Piggin873feea2011-01-07 17:50:06 +1100729 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700731EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733/*
Nick Piggin77812a1e2011-01-07 17:49:48 +1100734 * Try to throw away a dentry - free the inode, dput the parent.
735 * Requires dentry->d_lock is held, and dentry->d_count == 0.
736 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700737 *
Nick Piggin77812a1e2011-01-07 17:49:48 +1100738 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Nick Piggin77812a1e2011-01-07 17:49:48 +1100740static void try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200741 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Nick Piggin77812a1e2011-01-07 17:49:48 +1100743 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700744
Nick Piggin77812a1e2011-01-07 17:49:48 +1100745 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700746 /*
Nick Piggin77812a1e2011-01-07 17:49:48 +1100747 * If dentry_kill returns NULL, we have nothing more to do.
748 * if it returns the same dentry, trylocks failed. In either
749 * case, just loop again.
750 *
751 * Otherwise, we need to prune ancestors too. This is necessary
752 * to prevent quadratic behavior of shrink_dcache_parent(), but
753 * is also expected to be beneficial in reducing dentry cache
754 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700755 */
Nick Piggin77812a1e2011-01-07 17:49:48 +1100756 if (!parent)
757 return;
758 if (parent == dentry)
759 return;
760
761 /* Prune ancestors. */
762 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700763 while (dentry) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100764 spin_lock(&dentry->d_lock);
Nick Piggin89e60542011-01-07 17:49:45 +1100765 if (dentry->d_count > 1) {
766 dentry->d_count--;
767 spin_unlock(&dentry->d_lock);
768 return;
769 }
Nick Piggin77812a1e2011-01-07 17:49:48 +1100770 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400774static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700776 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700777
Nick Pigginec336792011-01-07 17:49:47 +1100778 rcu_read_lock();
779 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100780 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
781 if (&dentry->d_lru == list)
782 break; /* empty */
783 spin_lock(&dentry->d_lock);
784 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
785 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100786 continue;
787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700791 * the LRU because of laziness during lookup. Do not free
792 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100794 if (dentry->d_count) {
Nick Pigginec336792011-01-07 17:49:47 +1100795 dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700796 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 continue;
798 }
Nick Pigginec336792011-01-07 17:49:47 +1100799
Nick Pigginec336792011-01-07 17:49:47 +1100800 rcu_read_unlock();
Nick Piggin77812a1e2011-01-07 17:49:48 +1100801
802 try_prune_one_dentry(dentry);
803
Nick Pigginec336792011-01-07 17:49:47 +1100804 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Nick Pigginec336792011-01-07 17:49:47 +1100806 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400807}
808
809/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000810 * prune_dcache_sb - shrink the dcache
811 * @sb: superblock
812 * @count: number of entries to try to free
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400813 *
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000814 * Attempt to shrink the superblock dcache LRU by @count entries. This is
815 * done when we need more memory an called from the superblock shrinker
816 * function.
817 *
818 * This function may fail to free any resources if all the dentries are in
819 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400820 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000821void prune_dcache_sb(struct super_block *sb, int count)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400822{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400823 struct dentry *dentry;
824 LIST_HEAD(referenced);
825 LIST_HEAD(tmp);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400826
Nick Piggin23044502011-01-07 17:49:31 +1100827relock:
828 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400829 while (!list_empty(&sb->s_dentry_lru)) {
830 dentry = list_entry(sb->s_dentry_lru.prev,
831 struct dentry, d_lru);
832 BUG_ON(dentry->d_sb != sb);
833
Nick Piggin23044502011-01-07 17:49:31 +1100834 if (!spin_trylock(&dentry->d_lock)) {
835 spin_unlock(&dcache_lru_lock);
Thomas Gleixner3c536052012-03-07 21:00:34 +0100836 cpu_chill();
Nick Piggin23044502011-01-07 17:49:31 +1100837 goto relock;
838 }
839
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000840 if (dentry->d_flags & DCACHE_REFERENCED) {
Nick Piggin23044502011-01-07 17:49:31 +1100841 dentry->d_flags &= ~DCACHE_REFERENCED;
842 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400843 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100844 } else {
845 list_move_tail(&dentry->d_lru, &tmp);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100846 dentry->d_flags |= DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100847 spin_unlock(&dentry->d_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000848 if (!--count)
Nick Piggin23044502011-01-07 17:49:31 +1100849 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400850 }
Nick Pigginec336792011-01-07 17:49:47 +1100851 cond_resched_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400852 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700853 if (!list_empty(&referenced))
854 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100855 spin_unlock(&dcache_lru_lock);
Nick Pigginec336792011-01-07 17:49:47 +1100856
857 shrink_dentry_list(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700860/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * shrink_dcache_sb - shrink dcache for a superblock
862 * @sb: superblock
863 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400864 * Shrink the dcache for the specified super block. This is used to free
865 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400867void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400869 LIST_HEAD(tmp);
870
Nick Piggin23044502011-01-07 17:49:31 +1100871 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400872 while (!list_empty(&sb->s_dentry_lru)) {
873 list_splice_init(&sb->s_dentry_lru, &tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100874 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400875 shrink_dentry_list(&tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100876 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400877 }
Nick Piggin23044502011-01-07 17:49:31 +1100878 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700880EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/*
David Howellsc636ebd2006-10-11 01:22:19 -0700883 * destroy a single subtree of dentries for unmount
884 * - see the comments on shrink_dcache_for_umount() for a description of the
885 * locking
886 */
887static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
888{
889 struct dentry *parent;
890
891 BUG_ON(!IS_ROOT(dentry));
892
David Howellsc636ebd2006-10-11 01:22:19 -0700893 for (;;) {
894 /* descend to the first leaf in the current subtree */
David Howells43c1c9c2011-06-07 14:09:30 +0100895 while (!list_empty(&dentry->d_subdirs))
David Howellsc636ebd2006-10-11 01:22:19 -0700896 dentry = list_entry(dentry->d_subdirs.next,
Al Viro6637ecd2014-10-26 19:19:16 -0400897 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -0700898
899 /* consume the dentries from this leaf up through its parents
900 * until we find one with children or run out altogether */
901 do {
902 struct inode *inode;
903
Sage Weilf0023bc2011-10-28 10:02:42 -0700904 /*
Yan, Zheng61572bb2013-04-15 14:13:21 +0800905 * inform the fs that this dentry is about to be
Sage Weilf0023bc2011-10-28 10:02:42 -0700906 * unhashed and destroyed.
907 */
Yan, Zheng61572bb2013-04-15 14:13:21 +0800908 if (dentry->d_flags & DCACHE_OP_PRUNE)
909 dentry->d_op->d_prune(dentry);
910
911 dentry_lru_del(dentry);
David Howells43c1c9c2011-06-07 14:09:30 +0100912 __d_shrink(dentry);
913
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100914 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700915 printk(KERN_ERR
916 "BUG: Dentry %p{i=%lx,n=%s}"
917 " still in use (%d)"
918 " [unmount of %s %s]\n",
919 dentry,
920 dentry->d_inode ?
921 dentry->d_inode->i_ino : 0UL,
922 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100923 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700924 dentry->d_sb->s_type->name,
925 dentry->d_sb->s_id);
926 BUG();
927 }
928
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100929 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700930 parent = NULL;
Al Viro6637ecd2014-10-26 19:19:16 -0400931 list_del(&dentry->d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100932 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900933 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100934 parent->d_count--;
Al Viro6637ecd2014-10-26 19:19:16 -0400935 list_del(&dentry->d_child);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900936 }
David Howellsc636ebd2006-10-11 01:22:19 -0700937
David Howellsc636ebd2006-10-11 01:22:19 -0700938 inode = dentry->d_inode;
939 if (inode) {
940 dentry->d_inode = NULL;
Al Viro6637ecd2014-10-26 19:19:16 -0400941 hlist_del_init(&dentry->d_u.d_alias);
David Howellsc636ebd2006-10-11 01:22:19 -0700942 if (dentry->d_op && dentry->d_op->d_iput)
943 dentry->d_op->d_iput(dentry, inode);
944 else
945 iput(inode);
946 }
947
948 d_free(dentry);
949
950 /* finished when we fall off the top of the tree,
951 * otherwise we ascend to the parent and move to the
952 * next sibling if there is one */
953 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400954 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700955 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700956 } while (list_empty(&dentry->d_subdirs));
957
958 dentry = list_entry(dentry->d_subdirs.next,
Al Viro6637ecd2014-10-26 19:19:16 -0400959 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -0700960 }
961}
962
963/*
964 * destroy the dentries attached to a superblock on unmounting
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100965 * - we don't need to use dentry->d_lock because:
David Howellsc636ebd2006-10-11 01:22:19 -0700966 * - the superblock is detached from all mountings and open files, so the
967 * dentry trees will not be rearranged by the VFS
968 * - s_umount is write-locked, so the memory pressure shrinker will ignore
969 * any dentries belonging to this superblock that it comes across
970 * - the filesystem itself is no longer permitted to rearrange the dentries
971 * in this superblock
972 */
973void shrink_dcache_for_umount(struct super_block *sb)
974{
975 struct dentry *dentry;
976
977 if (down_read_trylock(&sb->s_umount))
978 BUG();
979
980 dentry = sb->s_root;
981 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100982 dentry->d_count--;
David Howellsc636ebd2006-10-11 01:22:19 -0700983 shrink_dcache_for_umount_subtree(dentry);
984
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100985 while (!hlist_bl_empty(&sb->s_anon)) {
986 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
David Howellsc636ebd2006-10-11 01:22:19 -0700987 shrink_dcache_for_umount_subtree(dentry);
988 }
989}
990
991/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Search for at least 1 mount point in the dentry's subdirs.
993 * We descend to the next level whenever the d_subdirs
994 * list is non-empty and continue searching.
995 */
996
997/**
998 * have_submounts - check for mounts over a dentry
999 * @parent: dentry to check.
1000 *
1001 * Return true if the parent or its subdirectories contain
1002 * a mount point
1003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004int have_submounts(struct dentry *parent)
1005{
Nick Piggin949854d2011-01-07 17:49:37 +11001006 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001008 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11001009 int locked = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11001010
Nick Piggin949854d2011-01-07 17:49:37 +11001011 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001012again:
1013 this_parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (d_mountpoint(parent))
1016 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001017 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018repeat:
1019 next = this_parent->d_subdirs.next;
1020resume:
1021 while (next != &this_parent->d_subdirs) {
1022 struct list_head *tmp = next;
Al Viro6637ecd2014-10-26 19:19:16 -04001023 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001025
1026 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001028 if (d_mountpoint(dentry)) {
1029 spin_unlock(&dentry->d_lock);
1030 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001034 spin_unlock(&this_parent->d_lock);
1035 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001037 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto repeat;
1039 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001040 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 /*
1043 * All done at this level ... ascend and resume the search.
1044 */
Al Viro5f03ac12014-10-26 19:31:10 -04001045 rcu_read_lock();
1046ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001048 struct dentry *child = this_parent;
Al Viro5f03ac12014-10-26 19:31:10 -04001049 this_parent = child->d_parent;
1050
1051 spin_unlock(&child->d_lock);
1052 spin_lock(&this_parent->d_lock);
1053
1054 /* might go back up the wrong parent if we have had a rename. */
1055 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001056 goto rename_retry;
Al Viro6637ecd2014-10-26 19:19:16 -04001057 next = child->d_child.next;
Al Viro5f03ac12014-10-26 19:31:10 -04001058 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
1059 if (next == &this_parent->d_subdirs)
1060 goto ascend;
1061 child = list_entry(next, struct dentry, d_child);
1062 next = next->next;
1063 }
1064 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 goto resume;
1066 }
Nick Piggin58db63d2011-01-07 17:49:39 +11001067 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001068 goto rename_retry;
Al Viro5f03ac12014-10-26 19:31:10 -04001069 spin_unlock(&this_parent->d_lock);
1070 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001071 if (locked)
1072 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0; /* No mount points found in tree */
1074positive:
Nick Piggin58db63d2011-01-07 17:49:39 +11001075 if (!locked && read_seqretry(&rename_lock, seq))
Ben Hutchingsc190d252015-02-11 03:16:35 +00001076 goto rename_retry_unlocked;
Nick Piggin58db63d2011-01-07 17:49:39 +11001077 if (locked)
1078 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001080
1081rename_retry:
Al Viro5f03ac12014-10-26 19:31:10 -04001082 spin_unlock(&this_parent->d_lock);
1083 rcu_read_unlock();
Miklos Szeredi8110e162012-09-17 22:23:30 +02001084 if (locked)
1085 goto again;
Ben Hutchingsc190d252015-02-11 03:16:35 +00001086rename_retry_unlocked:
Nick Piggin58db63d2011-01-07 17:49:39 +11001087 locked = 1;
1088 write_seqlock(&rename_lock);
1089 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001091EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001094 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * and move any unused dentries to the end of the unused
1096 * list for prune_dcache(). We descend to the next level
1097 * whenever the d_subdirs list is non-empty and continue
1098 * searching.
1099 *
1100 * It returns zero iff there are no unused children,
1101 * otherwise it returns the number of children moved to
1102 * the end of the unused list. This may not be the total
1103 * number of unused children, because select_parent can
1104 * drop the lock and return early due to latency
1105 * constraints.
1106 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001107static int select_parent(struct dentry *parent, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Nick Piggin949854d2011-01-07 17:49:37 +11001109 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001111 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int found = 0;
Nick Piggin58db63d2011-01-07 17:49:39 +11001113 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Nick Piggin949854d2011-01-07 17:49:37 +11001115 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001116again:
1117 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001118 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119repeat:
1120 next = this_parent->d_subdirs.next;
1121resume:
1122 while (next != &this_parent->d_subdirs) {
1123 struct list_head *tmp = next;
Al Viro6637ecd2014-10-26 19:19:16 -04001124 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 next = tmp->next;
1126
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001127 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001128
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001129 /*
1130 * move only zero ref count dentries to the dispose list.
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001131 *
1132 * Those which are presently on the shrink list, being processed
1133 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1134 * loop in shrink_dcache_parent() might not make any progress
1135 * and loop forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001137 if (dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001138 dentry_lru_del(dentry);
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001139 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1140 dentry_lru_move_list(dentry, dispose);
1141 dentry->d_flags |= DCACHE_SHRINK_LIST;
1142 found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * We can return to the caller if we have found some (this
1146 * ensures forward progress). We'll be coming back to find
1147 * the rest.
1148 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001149 if (found && need_resched()) {
1150 spin_unlock(&dentry->d_lock);
Ben Hutchingsc190d252015-02-11 03:16:35 +00001151 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /*
1156 * Descend a level if the d_subdirs list is non-empty.
1157 */
1158 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001159 spin_unlock(&this_parent->d_lock);
1160 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001162 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto repeat;
1164 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001165
1166 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 /*
1169 * All done at this level ... ascend and resume the search.
1170 */
Al Viro5f03ac12014-10-26 19:31:10 -04001171 rcu_read_lock();
1172ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001174 struct dentry *child = this_parent;
Al Viro5f03ac12014-10-26 19:31:10 -04001175 this_parent = child->d_parent;
1176
1177 spin_unlock(&child->d_lock);
1178 spin_lock(&this_parent->d_lock);
1179
1180 /* might go back up the wrong parent if we have had a rename. */
1181 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001182 goto rename_retry;
Al Viro6637ecd2014-10-26 19:19:16 -04001183 next = child->d_child.next;
Al Viro5f03ac12014-10-26 19:31:10 -04001184 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
1185 if (next == &this_parent->d_subdirs)
1186 goto ascend;
1187 child = list_entry(next, struct dentry, d_child);
1188 next = next->next;
1189 }
1190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto resume;
1192 }
1193out:
Nick Piggin58db63d2011-01-07 17:49:39 +11001194 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001195 goto rename_retry;
Al Viro5f03ac12014-10-26 19:31:10 -04001196 spin_unlock(&this_parent->d_lock);
1197 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001198 if (locked)
1199 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return found;
Nick Piggin58db63d2011-01-07 17:49:39 +11001201
1202rename_retry:
Al Viro5f03ac12014-10-26 19:31:10 -04001203 spin_unlock(&this_parent->d_lock);
1204 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001205 if (found)
1206 return found;
Miklos Szeredi8110e162012-09-17 22:23:30 +02001207 if (locked)
1208 goto again;
Nick Piggin58db63d2011-01-07 17:49:39 +11001209 locked = 1;
1210 write_seqlock(&rename_lock);
1211 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/**
1215 * shrink_dcache_parent - prune dcache
1216 * @parent: parent of entries to prune
1217 *
1218 * Prune the dcache to remove unused children of the parent dentry.
1219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220void shrink_dcache_parent(struct dentry * parent)
1221{
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001222 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int found;
1224
Greg Thelen421348f2013-04-30 15:26:48 -07001225 while ((found = select_parent(parent, &dispose)) != 0) {
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001226 shrink_dentry_list(&dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001227 cond_resched();
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001230EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/**
Al Viroa4464db2011-07-07 15:03:58 -04001233 * __d_alloc - allocate a dcache entry
1234 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * @name: qstr of the name
1236 *
1237 * Allocates a dentry. It returns %NULL if there is insufficient memory
1238 * available. On a success the dentry is returned. The name passed in is
1239 * copied and the copy passed in may be reused after this call.
1240 */
1241
Al Viroa4464db2011-07-07 15:03:58 -04001242struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct dentry *dentry;
1245 char *dname;
1246
Mel Gormane12ba742007-10-16 01:25:52 -07001247 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!dentry)
1249 return NULL;
1250
Linus Torvalds6326c712012-05-21 16:14:04 -07001251 /*
1252 * We guarantee that the inline name is always NUL-terminated.
1253 * This way the memcpy() done by the name switching in rename
1254 * will still always have a NUL at the end, even if we might
1255 * be overwriting an internal NUL character
1256 */
1257 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (name->len > DNAME_INLINE_LEN-1) {
1259 dname = kmalloc(name->len + 1, GFP_KERNEL);
1260 if (!dname) {
1261 kmem_cache_free(dentry_cache, dentry);
1262 return NULL;
1263 }
1264 } else {
1265 dname = dentry->d_iname;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 dentry->d_name.len = name->len;
1269 dentry->d_name.hash = name->hash;
1270 memcpy(dname, name->name, name->len);
1271 dname[name->len] = 0;
1272
Linus Torvalds6326c712012-05-21 16:14:04 -07001273 /* Make sure we always see the terminating NUL character */
1274 smp_wmb();
1275 dentry->d_name.name = dname;
1276
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001277 dentry->d_count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001278 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001280 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001282 dentry->d_parent = dentry;
1283 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 dentry->d_op = NULL;
1285 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001286 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 INIT_LIST_HEAD(&dentry->d_lru);
1288 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viro6637ecd2014-10-26 19:19:16 -04001289 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1290 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001291 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Nick Piggin3e880fb2011-01-07 17:49:19 +11001293 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return dentry;
1296}
Al Viroa4464db2011-07-07 15:03:58 -04001297
1298/**
1299 * d_alloc - allocate a dcache entry
1300 * @parent: parent of entry to allocate
1301 * @name: qstr of the name
1302 *
1303 * Allocates a dentry. It returns %NULL if there is insufficient memory
1304 * available. On a success the dentry is returned. The name passed in is
1305 * copied and the copy passed in may be reused after this call.
1306 */
1307struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1308{
1309 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1310 if (!dentry)
1311 return NULL;
1312
1313 spin_lock(&parent->d_lock);
1314 /*
1315 * don't need child lock because it is not subject
1316 * to concurrency here
1317 */
1318 __dget_dlock(parent);
1319 dentry->d_parent = parent;
Al Viro6637ecd2014-10-26 19:19:16 -04001320 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001321 spin_unlock(&parent->d_lock);
1322
1323 return dentry;
1324}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001325EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Nick Piggin4b936882011-01-07 17:50:07 +11001327struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1328{
Al Viroa4464db2011-07-07 15:03:58 -04001329 struct dentry *dentry = __d_alloc(sb, name);
1330 if (dentry)
Nick Piggin4b936882011-01-07 17:50:07 +11001331 dentry->d_flags |= DCACHE_DISCONNECTED;
Nick Piggin4b936882011-01-07 17:50:07 +11001332 return dentry;
1333}
1334EXPORT_SYMBOL(d_alloc_pseudo);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1337{
1338 struct qstr q;
1339
1340 q.name = name;
1341 q.len = strlen(name);
1342 q.hash = full_name_hash(q.name, q.len);
1343 return d_alloc(parent, &q);
1344}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001345EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Nick Pigginfb045ad2011-01-07 17:49:55 +11001347void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1348{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001349 WARN_ON_ONCE(dentry->d_op);
1350 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001351 DCACHE_OP_COMPARE |
1352 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001353 DCACHE_OP_WEAK_REVALIDATE |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001354 DCACHE_OP_DELETE ));
1355 dentry->d_op = op;
1356 if (!op)
1357 return;
1358 if (op->d_hash)
1359 dentry->d_flags |= DCACHE_OP_HASH;
1360 if (op->d_compare)
1361 dentry->d_flags |= DCACHE_OP_COMPARE;
1362 if (op->d_revalidate)
1363 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001364 if (op->d_weak_revalidate)
1365 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001366 if (op->d_delete)
1367 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001368 if (op->d_prune)
1369 dentry->d_flags |= DCACHE_OP_PRUNE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001370
1371}
1372EXPORT_SYMBOL(d_set_d_op);
1373
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001374static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1375{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001376 spin_lock(&dentry->d_lock);
David Howells9875cf82011-01-14 18:45:21 +00001377 if (inode) {
1378 if (unlikely(IS_AUTOMOUNT(inode)))
1379 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
Al Viro6637ecd2014-10-26 19:19:16 -04001380 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
David Howells9875cf82011-01-14 18:45:21 +00001381 }
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001382 dentry->d_inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001383 dentry_rcuwalk_barrier(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001384 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001385 fsnotify_d_instantiate(dentry, inode);
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/**
1389 * d_instantiate - fill in inode information for a dentry
1390 * @entry: dentry to complete
1391 * @inode: inode to attach to this dentry
1392 *
1393 * Fill in inode information in the entry.
1394 *
1395 * This turns negative dentries into productive full members
1396 * of society.
1397 *
1398 * NOTE! This assumes that the inode count has been incremented
1399 * (or otherwise set) by the caller to indicate that it is now
1400 * in use by the dcache.
1401 */
1402
1403void d_instantiate(struct dentry *entry, struct inode * inode)
1404{
Al Viro6637ecd2014-10-26 19:19:16 -04001405 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Nick Piggin873feea2011-01-07 17:50:06 +11001406 if (inode)
1407 spin_lock(&inode->i_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001408 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001409 if (inode)
1410 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 security_d_instantiate(entry, inode);
1412}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001413EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415/**
1416 * d_instantiate_unique - instantiate a non-aliased dentry
1417 * @entry: dentry to instantiate
1418 * @inode: inode to attach to this dentry
1419 *
1420 * Fill in inode information in the entry. On success, it returns NULL.
1421 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001422 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
1424 * Note that in order to avoid conflicts with rename() etc, the caller
1425 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001426 *
1427 * This also assumes that the inode count has been incremented
1428 * (or otherwise set) by the caller to indicate that it is now
1429 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
David Howells770bfad2006-08-22 20:06:07 -04001431static struct dentry *__d_instantiate_unique(struct dentry *entry,
1432 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct dentry *alias;
1435 int len = entry->d_name.len;
1436 const char *name = entry->d_name.name;
1437 unsigned int hash = entry->d_name.hash;
1438
David Howells770bfad2006-08-22 20:06:07 -04001439 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001440 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001441 return NULL;
1442 }
1443
Al Viro6637ecd2014-10-26 19:19:16 -04001444 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Piggin9abca362011-01-07 17:49:36 +11001445 /*
1446 * Don't need alias->d_lock here, because aliases with
1447 * d_parent == entry->d_parent are not subject to name or
1448 * parent changes, because the parent inode i_mutex is held.
1449 */
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001450 if (alias->d_name.hash != hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 continue;
1452 if (alias->d_parent != entry->d_parent)
1453 continue;
Linus Torvaldsee983e82012-05-10 12:37:10 -07001454 if (alias->d_name.len != len)
1455 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001456 if (dentry_cmp(alias, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001458 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return alias;
1460 }
David Howells770bfad2006-08-22 20:06:07 -04001461
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001462 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return NULL;
1464}
David Howells770bfad2006-08-22 20:06:07 -04001465
1466struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1467{
1468 struct dentry *result;
1469
Al Viro6637ecd2014-10-26 19:19:16 -04001470 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
David Howells770bfad2006-08-22 20:06:07 -04001471
Nick Piggin873feea2011-01-07 17:50:06 +11001472 if (inode)
1473 spin_lock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001474 result = __d_instantiate_unique(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001475 if (inode)
1476 spin_unlock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001477
1478 if (!result) {
1479 security_d_instantiate(entry, inode);
1480 return NULL;
1481 }
1482
1483 BUG_ON(!d_unhashed(result));
1484 iput(inode);
1485 return result;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488EXPORT_SYMBOL(d_instantiate_unique);
1489
Al Viroadc0e912012-01-08 16:49:21 -05001490struct dentry *d_make_root(struct inode *root_inode)
1491{
1492 struct dentry *res = NULL;
1493
1494 if (root_inode) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07001495 static const struct qstr name = QSTR_INIT("/", 1);
Al Viroadc0e912012-01-08 16:49:21 -05001496
1497 res = __d_alloc(root_inode->i_sb, &name);
1498 if (res)
1499 d_instantiate(res, root_inode);
1500 else
1501 iput(root_inode);
1502 }
1503 return res;
1504}
1505EXPORT_SYMBOL(d_make_root);
1506
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001507static struct dentry * __d_find_any_alias(struct inode *inode)
1508{
1509 struct dentry *alias;
1510
Al Virob3d9b7a2012-06-09 13:51:19 -04001511 if (hlist_empty(&inode->i_dentry))
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001512 return NULL;
Al Viro6637ecd2014-10-26 19:19:16 -04001513 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001514 __dget(alias);
1515 return alias;
1516}
1517
Sage Weil46f72b32012-01-10 09:04:37 -08001518/**
1519 * d_find_any_alias - find any alias for a given inode
1520 * @inode: inode to find an alias for
1521 *
1522 * If any aliases exist for the given inode, take and return a
1523 * reference for one of them. If no aliases exist, return %NULL.
1524 */
1525struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001526{
1527 struct dentry *de;
1528
1529 spin_lock(&inode->i_lock);
1530 de = __d_find_any_alias(inode);
1531 spin_unlock(&inode->i_lock);
1532 return de;
1533}
Sage Weil46f72b32012-01-10 09:04:37 -08001534EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001537 * d_obtain_alias - find or allocate a dentry for a given inode
1538 * @inode: inode to allocate the dentry for
1539 *
1540 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1541 * similar open by handle operations. The returned dentry may be anonymous,
1542 * or may have a full name (if the inode was already in the cache).
1543 *
1544 * When called on a directory inode, we must ensure that the inode only ever
1545 * has one dentry. If a dentry is found, that is returned instead of
1546 * allocating a new one.
1547 *
1548 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001549 * to the dentry. In case of an error the reference on the inode is released.
1550 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1551 * be passed in and will be the error will be propagate to the return value,
1552 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001553 */
1554struct dentry *d_obtain_alias(struct inode *inode)
1555{
NeilBrownb911a6b2012-11-08 16:09:37 -08001556 static const struct qstr anonstring = QSTR_INIT("/", 1);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001557 struct dentry *tmp;
1558 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001559
1560 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001561 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001562 if (IS_ERR(inode))
1563 return ERR_CAST(inode);
1564
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001565 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001566 if (res)
1567 goto out_iput;
1568
Al Viroa4464db2011-07-07 15:03:58 -04001569 tmp = __d_alloc(inode->i_sb, &anonstring);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001570 if (!tmp) {
1571 res = ERR_PTR(-ENOMEM);
1572 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001573 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001574
Nick Piggin873feea2011-01-07 17:50:06 +11001575 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001576 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001577 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001578 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001579 dput(tmp);
1580 goto out_iput;
1581 }
1582
1583 /* attach a disconnected dentry */
1584 spin_lock(&tmp->d_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001585 tmp->d_inode = inode;
1586 tmp->d_flags |= DCACHE_DISCONNECTED;
Al Viro6637ecd2014-10-26 19:19:16 -04001587 hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001588 hlist_bl_lock(&tmp->d_sb->s_anon);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001589 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001590 hlist_bl_unlock(&tmp->d_sb->s_anon);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001591 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001592 spin_unlock(&inode->i_lock);
Josef Bacik24ff6662010-11-18 20:52:55 -05001593 security_d_instantiate(tmp, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001594
Christoph Hellwig9308a612008-08-11 15:49:12 +02001595 return tmp;
1596
1597 out_iput:
Josef Bacik24ff6662010-11-18 20:52:55 -05001598 if (res && !IS_ERR(res))
1599 security_d_instantiate(res, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001600 iput(inode);
1601 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001602}
Benny Halevyadc48722009-02-27 14:02:59 -08001603EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/**
1606 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1607 * @inode: the inode which may have a disconnected dentry
1608 * @dentry: a negative dentry which we want to point to the inode.
1609 *
1610 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1611 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1612 * and return it, else simply d_add the inode to the dentry and return NULL.
1613 *
1614 * This is needed in the lookup routine of any filesystem that is exportable
1615 * (via knfsd) so that we can build dcache paths to directories effectively.
1616 *
1617 * If a dentry was found and moved, then it is returned. Otherwise NULL
1618 * is returned. This matches the expected return value of ->lookup.
1619 *
1620 */
1621struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1622{
1623 struct dentry *new = NULL;
1624
Al Viroa9049372011-07-08 21:20:11 -04001625 if (IS_ERR(inode))
1626 return ERR_CAST(inode);
1627
NeilBrown21c0d8f2006-10-04 02:16:16 -07001628 if (inode && S_ISDIR(inode->i_mode)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001629 spin_lock(&inode->i_lock);
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001630 new = __d_find_alias(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (new) {
Linus Torvalds32ba9c32012-06-08 10:34:03 -07001632 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Piggin873feea2011-01-07 17:50:06 +11001633 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 d_move(new, dentry);
1636 iput(inode);
1637 } else {
Nick Piggin873feea2011-01-07 17:50:06 +11001638 /* already taking inode->i_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001639 __d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001640 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 security_d_instantiate(dentry, inode);
1642 d_rehash(dentry);
1643 }
1644 } else
1645 d_add(dentry, inode);
1646 return new;
1647}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001648EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Barry Naujok94035402008-05-21 16:50:46 +10001650/**
1651 * d_add_ci - lookup or allocate new dentry with case-exact name
1652 * @inode: the inode case-insensitive lookup has found
1653 * @dentry: the negative dentry that was passed to the parent's lookup func
1654 * @name: the case-exact name to be associated with the returned dentry
1655 *
1656 * This is to avoid filling the dcache with case-insensitive names to the
1657 * same inode, only the actual correct case is stored in the dcache for
1658 * case-insensitive filesystems.
1659 *
1660 * For a case-insensitive lookup match and if the the case-exact dentry
1661 * already exists in in the dcache, use it and return it.
1662 *
1663 * If no entry exists with the exact case name, allocate new dentry with
1664 * the exact case, and return the spliced entry.
1665 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001666struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001667 struct qstr *name)
1668{
Barry Naujok94035402008-05-21 16:50:46 +10001669 struct dentry *found;
1670 struct dentry *new;
1671
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001672 /*
1673 * First check if a dentry matching the name already exists,
1674 * if not go ahead and create it now.
1675 */
Barry Naujok94035402008-05-21 16:50:46 +10001676 found = d_hash_and_lookup(dentry->d_parent, name);
Al Viro4f522a22013-02-11 23:20:37 -05001677 if (unlikely(IS_ERR(found)))
1678 goto err_out;
Barry Naujok94035402008-05-21 16:50:46 +10001679 if (!found) {
1680 new = d_alloc(dentry->d_parent, name);
1681 if (!new) {
Al Viro4f522a22013-02-11 23:20:37 -05001682 found = ERR_PTR(-ENOMEM);
Barry Naujok94035402008-05-21 16:50:46 +10001683 goto err_out;
1684 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001685
Barry Naujok94035402008-05-21 16:50:46 +10001686 found = d_splice_alias(inode, new);
1687 if (found) {
1688 dput(new);
1689 return found;
1690 }
1691 return new;
1692 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001693
1694 /*
1695 * If a matching dentry exists, and it's not negative use it.
1696 *
1697 * Decrement the reference count to balance the iget() done
1698 * earlier on.
1699 */
Barry Naujok94035402008-05-21 16:50:46 +10001700 if (found->d_inode) {
1701 if (unlikely(found->d_inode != inode)) {
1702 /* This can't happen because bad inodes are unhashed. */
1703 BUG_ON(!is_bad_inode(inode));
1704 BUG_ON(!is_bad_inode(found->d_inode));
1705 }
Barry Naujok94035402008-05-21 16:50:46 +10001706 iput(inode);
1707 return found;
1708 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001709
Barry Naujok94035402008-05-21 16:50:46 +10001710 /*
1711 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001712 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001713 */
Al Viro4513d892011-07-17 10:52:14 -04001714 new = d_splice_alias(inode, found);
1715 if (new) {
1716 dput(found);
1717 found = new;
Barry Naujok94035402008-05-21 16:50:46 +10001718 }
Al Viro4513d892011-07-17 10:52:14 -04001719 return found;
Barry Naujok94035402008-05-21 16:50:46 +10001720
1721err_out:
1722 iput(inode);
Al Viro4f522a22013-02-11 23:20:37 -05001723 return found;
Barry Naujok94035402008-05-21 16:50:46 +10001724}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001725EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001727/*
1728 * Do the slow-case of the dentry name compare.
1729 *
1730 * Unlike the dentry_cmp() function, we need to atomically
1731 * load the name, length and inode information, so that the
1732 * filesystem can rely on them, and can use the 'name' and
1733 * 'len' information without worrying about walking off the
1734 * end of memory etc.
1735 *
1736 * Thus the read_seqcount_retry() and the "duplicate" info
1737 * in arguments (the low-level filesystem should not look
1738 * at the dentry inode or name contents directly, since
1739 * rename can change them while we're in RCU mode).
1740 */
1741enum slow_d_compare {
1742 D_COMP_OK,
1743 D_COMP_NOMATCH,
1744 D_COMP_SEQRETRY,
1745};
1746
1747static noinline enum slow_d_compare slow_dentry_cmp(
1748 const struct dentry *parent,
1749 struct inode *inode,
1750 struct dentry *dentry,
1751 unsigned int seq,
1752 const struct qstr *name)
1753{
1754 int tlen = dentry->d_name.len;
1755 const char *tname = dentry->d_name.name;
1756 struct inode *i = dentry->d_inode;
1757
1758 if (read_seqcount_retry(&dentry->d_seq, seq)) {
1759 cpu_relax();
1760 return D_COMP_SEQRETRY;
1761 }
1762 if (parent->d_op->d_compare(parent, inode,
1763 dentry, i,
1764 tlen, tname, name))
1765 return D_COMP_NOMATCH;
1766 return D_COMP_OK;
1767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/**
Nick Piggin31e6b012011-01-07 17:49:52 +11001770 * __d_lookup_rcu - search for a dentry (racy, store-free)
1771 * @parent: parent dentry
1772 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07001773 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11001774 * @inode: returns dentry->d_inode when the inode was found valid.
1775 * Returns: dentry, or NULL
1776 *
1777 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1778 * resolution (store-free path walking) design described in
1779 * Documentation/filesystems/path-lookup.txt.
1780 *
1781 * This is not to be used outside core vfs.
1782 *
1783 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1784 * held, and rcu_read_lock held. The returned dentry must not be stored into
1785 * without taking d_lock and checking d_seq sequence count against @seq
1786 * returned here.
1787 *
1788 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1789 * function.
1790 *
1791 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1792 * the returned dentry, so long as its parent's seqlock is checked after the
1793 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1794 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001795 *
1796 * NOTE! The caller *has* to check the resulting dentry against the sequence
1797 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11001798 */
Linus Torvalds8966be92012-03-02 14:23:30 -08001799struct dentry *__d_lookup_rcu(const struct dentry *parent,
1800 const struct qstr *name,
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001801 unsigned *seqp, struct inode *inode)
Nick Piggin31e6b012011-01-07 17:49:52 +11001802{
Linus Torvalds26fe5752012-05-10 13:14:12 -07001803 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11001804 const unsigned char *str = name->name;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001805 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001806 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11001807 struct dentry *dentry;
1808
1809 /*
1810 * Note: There is significant duplication with __d_lookup_rcu which is
1811 * required to prevent single threaded performance regressions
1812 * especially on architectures where smp_rmb (in seqcounts) are costly.
1813 * Keep the two functions in sync.
1814 */
1815
1816 /*
1817 * The hash list is protected using RCU.
1818 *
1819 * Carefully use d_seq when comparing a candidate dentry, to avoid
1820 * races with d_move().
1821 *
1822 * It is possible that concurrent renames can mess up our list
1823 * walk here and result in missing our dentry, resulting in the
1824 * false-negative result. d_lookup() protects against concurrent
1825 * renames using rename_lock seqlock.
1826 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09001827 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11001828 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001829 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08001830 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001831
Nick Piggin31e6b012011-01-07 17:49:52 +11001832seqretry:
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001833 /*
1834 * The dentry sequence count protects us from concurrent
1835 * renames, and thus protects inode, parent and name fields.
1836 *
1837 * The caller must perform a seqcount check in order
1838 * to do anything useful with the returned dentry,
1839 * including using the 'd_inode' pointer.
1840 *
1841 * NOTE! We do a "raw" seqcount_begin here. That means that
1842 * we don't wait for the sequence count to stabilize if it
1843 * is in the middle of a sequence change. If we do the slow
1844 * dentry compare, we will do seqretries until it is stable,
1845 * and if we end up with a successful lookup, we actually
1846 * want to exit RCU lookup anyway.
1847 */
1848 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001849 if (dentry->d_parent != parent)
1850 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07001851 if (d_unhashed(dentry))
1852 continue;
Linus Torvalds8966be92012-03-02 14:23:30 -08001853 *seqp = seq;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001854
1855 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Linus Torvalds26fe5752012-05-10 13:14:12 -07001856 if (dentry->d_name.hash != hashlen_hash(hashlen))
1857 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001858 switch (slow_dentry_cmp(parent, inode, dentry, seq, name)) {
1859 case D_COMP_OK:
1860 return dentry;
1861 case D_COMP_NOMATCH:
1862 continue;
1863 default:
1864 goto seqretry;
1865 }
1866 }
1867
Linus Torvalds26fe5752012-05-10 13:14:12 -07001868 if (dentry->d_name.hash_len != hashlen)
Linus Torvaldsee983e82012-05-10 12:37:10 -07001869 continue;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001870 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001871 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11001872 }
1873 return NULL;
1874}
1875
1876/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * d_lookup - search for a dentry
1878 * @parent: parent dentry
1879 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001880 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001882 * d_lookup searches the children of the parent dentry for the name in
1883 * question. If the dentry is found its reference count is incremented and the
1884 * dentry is returned. The caller must use dput to free the entry when it has
1885 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 */
Al Viroda2d8452013-01-24 18:29:34 -05001887struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Nick Piggin31e6b012011-01-07 17:49:52 +11001889 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11001890 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 do {
1893 seq = read_seqbegin(&rename_lock);
1894 dentry = __d_lookup(parent, name);
1895 if (dentry)
1896 break;
1897 } while (read_seqretry(&rename_lock, seq));
1898 return dentry;
1899}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001900EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Nick Piggin31e6b012011-01-07 17:49:52 +11001902/**
Nick Pigginb04f7842010-08-18 04:37:34 +10001903 * __d_lookup - search for a dentry (racy)
1904 * @parent: parent dentry
1905 * @name: qstr of name we wish to find
1906 * Returns: dentry, or NULL
1907 *
1908 * __d_lookup is like d_lookup, however it may (rarely) return a
1909 * false-negative result due to unrelated rename activity.
1910 *
1911 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1912 * however it must be used carefully, eg. with a following d_lookup in
1913 * the case of failure.
1914 *
1915 * __d_lookup callers must be commented.
1916 */
Al Viroa713ca22013-01-24 18:27:00 -05001917struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 unsigned int len = name->len;
1920 unsigned int hash = name->hash;
1921 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001922 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001923 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11001924 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001925 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Nick Pigginb04f7842010-08-18 04:37:34 +10001927 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11001928 * Note: There is significant duplication with __d_lookup_rcu which is
1929 * required to prevent single threaded performance regressions
1930 * especially on architectures where smp_rmb (in seqcounts) are costly.
1931 * Keep the two functions in sync.
1932 */
1933
1934 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001935 * The hash list is protected using RCU.
1936 *
1937 * Take d_lock when comparing a candidate dentry, to avoid races
1938 * with d_move().
1939 *
1940 * It is possible that concurrent renames can mess up our list
1941 * walk here and result in missing our dentry, resulting in the
1942 * false-negative result. d_lookup() protects against concurrent
1943 * renames using rename_lock seqlock.
1944 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09001945 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10001946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rcu_read_lock();
1948
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001949 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (dentry->d_name.hash != hash)
1952 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (dentry->d_parent != parent)
1956 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001957 if (d_unhashed(dentry))
1958 goto next;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * It is safe to compare names since d_move() cannot
1962 * change the qstr (protected by d_lock).
1963 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11001964 if (parent->d_flags & DCACHE_OP_COMPARE) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001965 int tlen = dentry->d_name.len;
1966 const char *tname = dentry->d_name.name;
Nick Piggin621e1552011-01-07 17:49:27 +11001967 if (parent->d_op->d_compare(parent, parent->d_inode,
1968 dentry, dentry->d_inode,
Nick Piggin31e6b012011-01-07 17:49:52 +11001969 tlen, tname, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto next;
1971 } else {
Linus Torvaldsee983e82012-05-10 12:37:10 -07001972 if (dentry->d_name.len != len)
1973 goto next;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001974 if (dentry_cmp(dentry, str, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto next;
1976 }
1977
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001978 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001979 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 spin_unlock(&dentry->d_lock);
1981 break;
1982next:
1983 spin_unlock(&dentry->d_lock);
1984 }
1985 rcu_read_unlock();
1986
1987 return found;
1988}
1989
1990/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001991 * d_hash_and_lookup - hash the qstr then search for a dentry
1992 * @dir: Directory to search in
1993 * @name: qstr of name we wish to find
1994 *
Al Viro4f522a22013-02-11 23:20:37 -05001995 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001996 */
1997struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1998{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001999 /*
2000 * Check for a fs-specific hash function. Note that we must
2001 * calculate the standard hash first, as the d_op->d_hash()
2002 * routine may choose to leave the hash value unchanged.
2003 */
2004 name->hash = full_name_hash(name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002005 if (dir->d_flags & DCACHE_OP_HASH) {
Al Viro4f522a22013-02-11 23:20:37 -05002006 int err = dir->d_op->d_hash(dir, dir->d_inode, name);
2007 if (unlikely(err < 0))
2008 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002009 }
Al Viro4f522a22013-02-11 23:20:37 -05002010 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002011}
Al Viro4f522a22013-02-11 23:20:37 -05002012EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002013
2014/**
Nick Piggin786a5e12011-01-07 17:49:16 +11002015 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * @dentry: The dentry alleged to be valid child of @dparent
Randy Dunlapff5fdb62011-01-22 20:16:06 -08002017 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *
2019 * An insecure source has sent us a dentry, here we verify it and dget() it.
2020 * This is used by ncpfs in its readdir implementation.
2021 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11002022 *
2023 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 */
Nick Piggind3a23e12011-01-05 20:01:21 +11002025int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Nick Piggin786a5e12011-01-07 17:49:16 +11002027 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11002028
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002029 spin_lock(&dparent->d_lock);
Al Viro6637ecd2014-10-26 19:19:16 -04002030 list_for_each_entry(child, &dparent->d_subdirs, d_child) {
Nick Piggin786a5e12011-01-07 17:49:16 +11002031 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002032 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggindc0474b2011-01-07 17:49:43 +11002033 __dget_dlock(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002034 spin_unlock(&dentry->d_lock);
2035 spin_unlock(&dparent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 1;
2037 }
2038 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002039 spin_unlock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
2042}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002043EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045/*
2046 * When a file is deleted, we have two options:
2047 * - turn this dentry into a negative dentry
2048 * - unhash this dentry and free it.
2049 *
2050 * Usually, we want to just turn this into
2051 * a negative dentry, but if anybody else is
2052 * currently using the dentry or the inode
2053 * we can't do that and we fall back on removing
2054 * it from the hash queues and waiting for
2055 * it to be deleted later when it has no users
2056 */
2057
2058/**
2059 * d_delete - delete a dentry
2060 * @dentry: The dentry to delete
2061 *
2062 * Turn the dentry into a negative dentry if possible, otherwise
2063 * remove it from the hash queues so it can be deleted later
2064 */
2065
2066void d_delete(struct dentry * dentry)
2067{
Nick Piggin873feea2011-01-07 17:50:06 +11002068 struct inode *inode;
John McCutchan7a91bf72005-08-08 13:52:16 -04002069 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Are we the only user?
2072 */
Nick Piggin357f8e62011-01-07 17:49:42 +11002073again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 spin_lock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002075 inode = dentry->d_inode;
2076 isdir = S_ISDIR(inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002077 if (dentry->d_count == 1) {
Alan Cox1fe0c022012-09-19 15:49:51 +01002078 if (!spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002079 spin_unlock(&dentry->d_lock);
Thomas Gleixner3c536052012-03-07 21:00:34 +01002080 cpu_chill();
Nick Piggin357f8e62011-01-07 17:49:42 +11002081 goto again;
2082 }
Al Viro13e3c5e2010-05-21 16:11:04 -04002083 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002084 dentry_unlink_inode(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04002085 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return;
2087 }
2088
2089 if (!d_unhashed(dentry))
2090 __d_drop(dentry);
2091
2092 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04002093
2094 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002096EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002098static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002100 BUG_ON(!d_unhashed(entry));
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002101 hlist_bl_lock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -07002102 entry->d_flags |= DCACHE_RCUACCESS;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002103 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002104 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
David Howells770bfad2006-08-22 20:06:07 -04002107static void _d_rehash(struct dentry * entry)
2108{
2109 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2110}
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/**
2113 * d_rehash - add an entry back to the hash
2114 * @entry: dentry to add to the hash
2115 *
2116 * Adds a dentry to the hash according to its name.
2117 */
2118
2119void d_rehash(struct dentry * entry)
2120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04002122 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002125EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002127/**
2128 * dentry_update_name_case - update case insensitive dentry with a new name
2129 * @dentry: dentry to be updated
2130 * @name: new name
2131 *
2132 * Update a case insensitive dentry with new case of name.
2133 *
2134 * dentry must have been returned by d_lookup with name @name. Old and new
2135 * name lengths must match (ie. no d_compare which allows mismatched name
2136 * lengths).
2137 *
2138 * Parent inode i_mutex must be held over d_lookup and into this call (to
2139 * keep renames and concurrent inserts, and readdir(2) away).
2140 */
2141void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2142{
Linus Torvalds7ebfa572011-04-15 07:34:26 -07002143 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002144 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2145
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002146 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002147 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002148 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002149 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002150 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002151}
2152EXPORT_SYMBOL(dentry_update_name_case);
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154static void switch_names(struct dentry *dentry, struct dentry *target)
2155{
2156 if (dname_external(target)) {
2157 if (dname_external(dentry)) {
2158 /*
2159 * Both external: swap the pointers
2160 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002161 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 } else {
2163 /*
2164 * dentry:internal, target:external. Steal target's
2165 * storage and make target internal.
2166 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002167 memcpy(target->d_iname, dentry->d_name.name,
2168 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 dentry->d_name.name = target->d_name.name;
2170 target->d_name.name = target->d_iname;
2171 }
2172 } else {
2173 if (dname_external(dentry)) {
2174 /*
2175 * dentry:external, target:internal. Give dentry's
2176 * storage to target and make dentry internal
2177 */
2178 memcpy(dentry->d_iname, target->d_name.name,
2179 target->d_name.len + 1);
2180 target->d_name.name = dentry->d_name.name;
2181 dentry->d_name.name = dentry->d_iname;
2182 } else {
2183 /*
2184 * Both are internal. Just copy target to dentry
2185 */
2186 memcpy(dentry->d_iname, target->d_name.name,
2187 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05002188 dentry->d_name.len = target->d_name.len;
2189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002192 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002195static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2196{
2197 /*
2198 * XXXX: do we really need to take target->d_lock?
2199 */
2200 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2201 spin_lock(&target->d_parent->d_lock);
2202 else {
2203 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2204 spin_lock(&dentry->d_parent->d_lock);
2205 spin_lock_nested(&target->d_parent->d_lock,
2206 DENTRY_D_LOCK_NESTED);
2207 } else {
2208 spin_lock(&target->d_parent->d_lock);
2209 spin_lock_nested(&dentry->d_parent->d_lock,
2210 DENTRY_D_LOCK_NESTED);
2211 }
2212 }
2213 if (target < dentry) {
2214 spin_lock_nested(&target->d_lock, 2);
2215 spin_lock_nested(&dentry->d_lock, 3);
2216 } else {
2217 spin_lock_nested(&dentry->d_lock, 2);
2218 spin_lock_nested(&target->d_lock, 3);
2219 }
2220}
2221
2222static void dentry_unlock_parents_for_move(struct dentry *dentry,
2223 struct dentry *target)
2224{
2225 if (target->d_parent != dentry->d_parent)
2226 spin_unlock(&dentry->d_parent->d_lock);
2227 if (target->d_parent != target)
2228 spin_unlock(&target->d_parent->d_lock);
2229}
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002232 * When switching names, the actual string doesn't strictly have to
2233 * be preserved in the target - because we're dropping the target
2234 * anyway. As such, we can just do a simple memcpy() to copy over
2235 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002237 * Note that we have to be a lot more careful about getting the hash
2238 * switched - we have to switch the hash value properly even if it
2239 * then no longer matches the actual (corrupted) string of the target.
2240 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002242/*
Al Viro18367502011-07-12 21:42:24 -04002243 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * @dentry: entry to move
2245 * @target: new dentry
2246 *
2247 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002248 * dcache entries should not be moved in this way. Caller must hold
2249 * rename_lock, the i_mutex of the source and target directories,
2250 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 */
Al Viro18367502011-07-12 21:42:24 -04002252static void __d_move(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!dentry->d_inode)
2255 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2256
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002257 BUG_ON(d_ancestor(dentry, target));
2258 BUG_ON(d_ancestor(target, dentry));
2259
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002260 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Nick Piggin31e6b012011-01-07 17:49:52 +11002262 write_seqcount_begin(&dentry->d_seq);
2263 write_seqcount_begin(&target->d_seq);
2264
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002265 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2266
2267 /*
2268 * Move the dentry to the target hash queue. Don't bother checking
2269 * for the same hash queue because of how unlikely it is.
2270 */
2271 __d_drop(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11002272 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* Unhash the target: dput() will then get rid of it */
2275 __d_drop(target);
2276
Al Viro6637ecd2014-10-26 19:19:16 -04002277 list_del(&dentry->d_child);
2278 list_del(&target->d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /* Switch the names.. */
2281 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002282 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 /* ... and switch the parents */
2285 if (IS_ROOT(dentry)) {
2286 dentry->d_parent = target->d_parent;
2287 target->d_parent = target;
Al Viro6637ecd2014-10-26 19:19:16 -04002288 INIT_LIST_HEAD(&target->d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002290 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 /* And add them back to the (new) parent lists */
Al Viro6637ecd2014-10-26 19:19:16 -04002293 list_add(&target->d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
Al Viro6637ecd2014-10-26 19:19:16 -04002296 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002297
Nick Piggin31e6b012011-01-07 17:49:52 +11002298 write_seqcount_end(&target->d_seq);
2299 write_seqcount_end(&dentry->d_seq);
2300
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002301 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002303 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 spin_unlock(&dentry->d_lock);
Al Viro18367502011-07-12 21:42:24 -04002305}
2306
2307/*
2308 * d_move - move a dentry
2309 * @dentry: entry to move
2310 * @target: new dentry
2311 *
2312 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002313 * dcache entries should not be moved in this way. See the locking
2314 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002315 */
2316void d_move(struct dentry *dentry, struct dentry *target)
2317{
2318 write_seqlock(&rename_lock);
2319 __d_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002321}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002322EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002324/**
2325 * d_ancestor - search for an ancestor
2326 * @p1: ancestor dentry
2327 * @p2: child dentry
2328 *
2329 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2330 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002331 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002332struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002333{
2334 struct dentry *p;
2335
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002336 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002337 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002338 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002339 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002340 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002341}
2342
2343/*
2344 * This helper attempts to cope with remotely renamed directories
2345 *
2346 * It assumes that the caller is already holding
Al Viro18367502011-07-12 21:42:24 -04002347 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002348 *
2349 * Note: If ever the locking in lock_rename() changes, then please
2350 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002351 */
Nick Piggin873feea2011-01-07 17:50:06 +11002352static struct dentry *__d_unalias(struct inode *inode,
2353 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002354{
2355 struct mutex *m1 = NULL, *m2 = NULL;
Al Viroee3efa92012-06-08 15:59:33 -04002356 struct dentry *ret = ERR_PTR(-EBUSY);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002357
2358 /* If alias and dentry share a parent, then no extra locks required */
2359 if (alias->d_parent == dentry->d_parent)
2360 goto out_unalias;
2361
Trond Myklebust9eaef272006-10-21 10:24:20 -07002362 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002363 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2364 goto out_err;
2365 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2366 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2367 goto out_err;
2368 m2 = &alias->d_parent->d_inode->i_mutex;
2369out_unalias:
Al Viroee3efa92012-06-08 15:59:33 -04002370 if (likely(!d_mountpoint(alias))) {
2371 __d_move(alias, dentry);
2372 ret = alias;
2373 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002374out_err:
Nick Piggin873feea2011-01-07 17:50:06 +11002375 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002376 if (m2)
2377 mutex_unlock(m2);
2378 if (m1)
2379 mutex_unlock(m1);
2380 return ret;
2381}
2382
2383/*
David Howells770bfad2006-08-22 20:06:07 -04002384 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2385 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002386 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002387 */
2388static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2389{
Al Viro740da422013-01-30 10:13:38 -05002390 struct dentry *dparent;
David Howells770bfad2006-08-22 20:06:07 -04002391
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002392 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002393
Nick Piggin31e6b012011-01-07 17:49:52 +11002394 write_seqcount_begin(&dentry->d_seq);
2395 write_seqcount_begin(&anon->d_seq);
2396
David Howells770bfad2006-08-22 20:06:07 -04002397 dparent = dentry->d_parent;
David Howells770bfad2006-08-22 20:06:07 -04002398
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002399 switch_names(dentry, anon);
2400 swap(dentry->d_name.hash, anon->d_name.hash);
2401
Al Viro740da422013-01-30 10:13:38 -05002402 dentry->d_parent = dentry;
Al Viro6637ecd2014-10-26 19:19:16 -04002403 list_del_init(&dentry->d_child);
Al Viro740da422013-01-30 10:13:38 -05002404 anon->d_parent = dparent;
Al Viro6637ecd2014-10-26 19:19:16 -04002405 list_move(&anon->d_child, &dparent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002406
Nick Piggin31e6b012011-01-07 17:49:52 +11002407 write_seqcount_end(&dentry->d_seq);
2408 write_seqcount_end(&anon->d_seq);
2409
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002410 dentry_unlock_parents_for_move(anon, dentry);
2411 spin_unlock(&dentry->d_lock);
2412
2413 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002414 anon->d_flags &= ~DCACHE_DISCONNECTED;
2415}
2416
2417/**
2418 * d_materialise_unique - introduce an inode into the tree
2419 * @dentry: candidate dentry
2420 * @inode: inode to bind to the dentry, to which aliases may be attached
2421 *
2422 * Introduces an dentry into the tree, substituting an extant disconnected
Jeff Laytonc46c8872011-07-26 13:33:16 -04002423 * root directory alias in its place if there is one. Caller must hold the
2424 * i_mutex of the parent directory.
David Howells770bfad2006-08-22 20:06:07 -04002425 */
2426struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2427{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002428 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002429
2430 BUG_ON(!d_unhashed(dentry));
2431
David Howells770bfad2006-08-22 20:06:07 -04002432 if (!inode) {
2433 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002434 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002435 d_rehash(actual);
2436 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002437 }
2438
Nick Piggin873feea2011-01-07 17:50:06 +11002439 spin_lock(&inode->i_lock);
Nick Piggin357f8e62011-01-07 17:49:42 +11002440
Trond Myklebust9eaef272006-10-21 10:24:20 -07002441 if (S_ISDIR(inode->i_mode)) {
2442 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002443
Trond Myklebust9eaef272006-10-21 10:24:20 -07002444 /* Does an aliased dentry already exist? */
Linus Torvalds32ba9c32012-06-08 10:34:03 -07002445 alias = __d_find_alias(inode, 0);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002446 if (alias) {
2447 actual = alias;
Al Viro18367502011-07-12 21:42:24 -04002448 write_seqlock(&rename_lock);
2449
2450 if (d_ancestor(alias, dentry)) {
2451 /* Check for loops */
2452 actual = ERR_PTR(-ELOOP);
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002453 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04002454 } else if (IS_ROOT(alias)) {
2455 /* Is this an anonymous mountpoint that we
2456 * could splice into our tree? */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002457 __d_materialise_dentry(dentry, alias);
Al Viro18367502011-07-12 21:42:24 -04002458 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002459 __d_drop(alias);
2460 goto found;
Al Viro18367502011-07-12 21:42:24 -04002461 } else {
2462 /* Nope, but we must(!) avoid directory
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002463 * aliasing. This drops inode->i_lock */
Al Viro18367502011-07-12 21:42:24 -04002464 actual = __d_unalias(inode, dentry, alias);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002465 }
Al Viro18367502011-07-12 21:42:24 -04002466 write_sequnlock(&rename_lock);
David Howellsdd179942011-08-16 15:31:30 +01002467 if (IS_ERR(actual)) {
2468 if (PTR_ERR(actual) == -ELOOP)
2469 pr_warn_ratelimited(
2470 "VFS: Lookup of '%s' in %s %s"
2471 " would have caused loop\n",
2472 dentry->d_name.name,
2473 inode->i_sb->s_type->name,
2474 inode->i_sb->s_id);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002475 dput(alias);
David Howellsdd179942011-08-16 15:31:30 +01002476 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002477 goto out_nolock;
2478 }
David Howells770bfad2006-08-22 20:06:07 -04002479 }
2480
2481 /* Add a unique reference */
2482 actual = __d_instantiate_unique(dentry, inode);
2483 if (!actual)
2484 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002485 else
2486 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002487
David Howells770bfad2006-08-22 20:06:07 -04002488 spin_lock(&actual->d_lock);
2489found:
2490 _d_rehash(actual);
2491 spin_unlock(&actual->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002492 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002493out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002494 if (actual == dentry) {
2495 security_d_instantiate(dentry, inode);
2496 return NULL;
2497 }
2498
2499 iput(inode);
2500 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002501}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002502EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002503
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002504static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002505{
2506 *buflen -= namelen;
2507 if (*buflen < 0)
2508 return -ENAMETOOLONG;
2509 *buffer -= namelen;
2510 memcpy(*buffer, str, namelen);
2511 return 0;
2512}
2513
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002514static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2515{
2516 return prepend(buffer, buflen, name->name, name->len);
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519/**
Randy Dunlap208898c2010-11-18 15:02:49 -08002520 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002521 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002522 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002523 * @buffer: pointer to the end of the buffer
2524 * @buflen: pointer to buffer length
2525 *
Nick Piggin949854d2011-01-07 17:49:37 +11002526 * Caller holds the rename_lock.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002527 */
Al Viro02125a82011-12-05 08:43:34 -05002528static int prepend_path(const struct path *path,
2529 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002530 char **buffer, int *buflen)
2531{
2532 struct dentry *dentry = path->dentry;
2533 struct vfsmount *vfsmnt = path->mnt;
Al Viro0714a532011-11-24 22:19:58 -05002534 struct mount *mnt = real_mount(vfsmnt);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002535 bool slash = false;
2536 int error = 0;
2537
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002538 while (dentry != root->dentry || vfsmnt != root->mnt) {
2539 struct dentry * parent;
2540
2541 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2542 /* Global root? */
Al Viro676da582011-11-24 21:47:05 -05002543 if (!mnt_has_parent(mnt))
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002544 goto global_root;
Al Viroa73324d2011-11-24 22:25:07 -05002545 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002546 mnt = mnt->mnt_parent;
2547 vfsmnt = &mnt->mnt;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002548 continue;
2549 }
2550 parent = dentry->d_parent;
2551 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002552 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002553 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002554 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002555 if (!error)
2556 error = prepend(buffer, buflen, "/", 1);
2557 if (error)
2558 break;
2559
2560 slash = true;
2561 dentry = parent;
2562 }
2563
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002564 if (!error && !slash)
2565 error = prepend(buffer, buflen, "/", 1);
2566
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002567 return error;
2568
2569global_root:
2570 /*
2571 * Filesystems needing to implement special "root names"
2572 * should do so with ->d_dname()
2573 */
2574 if (IS_ROOT(dentry) &&
2575 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2576 WARN(1, "Root dentry has weird name <%.*s>\n",
2577 (int) dentry->d_name.len, dentry->d_name.name);
2578 }
Al Viro02125a82011-12-05 08:43:34 -05002579 if (!slash)
2580 error = prepend(buffer, buflen, "/", 1);
2581 if (!error)
Al Virof7a99c52012-06-09 00:59:08 -04002582 error = is_mounted(vfsmnt) ? 1 : 2;
Al Viro7ea600b2013-03-26 18:25:57 -04002583 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002584}
2585
2586/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002587 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002588 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002589 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07002590 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 * @buflen: buffer length
2592 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002593 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002595 * Returns a pointer into the buffer or an error code if the
2596 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002597 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002598 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002599 *
Al Viro02125a82011-12-05 08:43:34 -05002600 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
Al Viro02125a82011-12-05 08:43:34 -05002602char *__d_path(const struct path *path,
2603 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002604 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002606 char *res = buf + buflen;
2607 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002609 prepend(&res, &buflen, "\0", 1);
Al Viro7ea600b2013-03-26 18:25:57 -04002610 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002611 write_seqlock(&rename_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002612 error = prepend_path(path, root, &res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002613 write_sequnlock(&rename_lock);
Al Viro7ea600b2013-03-26 18:25:57 -04002614 br_read_unlock(&vfsmount_lock);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002615
Al Viro02125a82011-12-05 08:43:34 -05002616 if (error < 0)
2617 return ERR_PTR(error);
2618 if (error > 0)
2619 return NULL;
2620 return res;
2621}
2622
2623char *d_absolute_path(const struct path *path,
2624 char *buf, int buflen)
2625{
2626 struct path root = {};
2627 char *res = buf + buflen;
2628 int error;
2629
2630 prepend(&res, &buflen, "\0", 1);
Al Viro7ea600b2013-03-26 18:25:57 -04002631 br_read_lock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002632 write_seqlock(&rename_lock);
2633 error = prepend_path(path, &root, &res, &buflen);
2634 write_sequnlock(&rename_lock);
Al Viro7ea600b2013-03-26 18:25:57 -04002635 br_read_unlock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002636
2637 if (error > 1)
2638 error = -EINVAL;
2639 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002640 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002641 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002644/*
2645 * same as __d_path but appends "(deleted)" for unlinked files.
2646 */
Al Viro02125a82011-12-05 08:43:34 -05002647static int path_with_deleted(const struct path *path,
2648 const struct path *root,
2649 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002650{
2651 prepend(buf, buflen, "\0", 1);
2652 if (d_unlinked(path->dentry)) {
2653 int error = prepend(buf, buflen, " (deleted)", 10);
2654 if (error)
2655 return error;
2656 }
2657
2658 return prepend_path(path, root, buf, buflen);
2659}
2660
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002661static int prepend_unreachable(char **buffer, int *buflen)
2662{
2663 return prepend(buffer, buflen, "(unreachable)", 13);
2664}
2665
Jan Bluncka03a8a702008-02-14 19:38:32 -08002666/**
2667 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002668 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002669 * @buf: buffer to return value in
2670 * @buflen: buffer length
2671 *
2672 * Convert a dentry into an ASCII path name. If the entry has been deleted
2673 * the string " (deleted)" is appended. Note that this is ambiguous.
2674 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002675 * Returns a pointer into the buffer or an error code if the path was
2676 * too long. Note: Callers should use the returned pointer, not the passed
2677 * in buffer, to use the name! The implementation often starts at an offset
2678 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002679 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002680 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002681 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002682char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002684 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002685 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002686 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002688 /*
2689 * We have various synthetic filesystems that never get mounted. On
2690 * these filesystems dentries are never used for lookup purposes, and
2691 * thus don't need to be hashed. They also don't need a name until a
2692 * user wants to identify the object in /proc/pid/fd/. The little hack
2693 * below allows us to generate a name for these objects on demand:
Eric W. Biederman3717eee2013-11-08 16:31:29 -08002694 *
2695 * Some pseudo inodes are mountable. When they are mounted
2696 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
2697 * and instead have d_path return the mounted path.
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002698 */
Eric W. Biederman3717eee2013-11-08 16:31:29 -08002699 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
2700 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
Jan Blunckcf28b482008-02-14 19:38:44 -08002701 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002702
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002703 get_fs_root(current->fs, &root);
Al Viro7ea600b2013-03-26 18:25:57 -04002704 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002705 write_seqlock(&rename_lock);
Al Viro02125a82011-12-05 08:43:34 -05002706 error = path_with_deleted(path, &root, &res, &buflen);
Al Viro7ea600b2013-03-26 18:25:57 -04002707 write_sequnlock(&rename_lock);
2708 br_read_unlock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002709 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002710 res = ERR_PTR(error);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002711 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return res;
2713}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002714EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002717 * Helper function for dentry_operations.d_dname() members
2718 */
2719char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2720 const char *fmt, ...)
2721{
2722 va_list args;
2723 char temp[64];
2724 int sz;
2725
2726 va_start(args, fmt);
2727 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2728 va_end(args);
2729
2730 if (sz > sizeof(temp) || sz > buflen)
2731 return ERR_PTR(-ENAMETOOLONG);
2732
2733 buffer += buflen - sz;
2734 return memcpy(buffer, temp, sz);
2735}
2736
Al Viroad4c3cc2013-08-24 12:08:17 -04002737char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
2738{
2739 char *end = buffer + buflen;
2740 /* these dentries are never renamed, so d_lock is not needed */
2741 if (prepend(&end, &buflen, " (deleted)", 11) ||
2742 prepend_name(&end, &buflen, &dentry->d_name) ||
2743 prepend(&end, &buflen, "/", 1))
2744 end = ERR_PTR(-ENAMETOOLONG);
2745 return end;
2746}
2747
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002748/*
Ram Pai6092d042008-03-27 13:06:20 +01002749 * Write full pathname from the root of the filesystem into the buffer.
2750 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002751static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002752{
2753 char *end = buf + buflen;
2754 char *retval;
2755
Ram Pai6092d042008-03-27 13:06:20 +01002756 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002757 if (buflen < 1)
2758 goto Elong;
2759 /* Get '/' right */
2760 retval = end-1;
2761 *retval = '/';
2762
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002763 while (!IS_ROOT(dentry)) {
2764 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002765 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002766
Ram Pai6092d042008-03-27 13:06:20 +01002767 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002768 spin_lock(&dentry->d_lock);
2769 error = prepend_name(&end, &buflen, &dentry->d_name);
2770 spin_unlock(&dentry->d_lock);
2771 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002772 goto Elong;
2773
2774 retval = end;
2775 dentry = parent;
2776 }
Al Viroc1031352010-06-06 22:31:14 -04002777 return retval;
2778Elong:
2779 return ERR_PTR(-ENAMETOOLONG);
2780}
Nick Pigginec2447c2011-01-07 17:49:29 +11002781
2782char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2783{
2784 char *retval;
2785
Nick Piggin949854d2011-01-07 17:49:37 +11002786 write_seqlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002787 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002788 write_sequnlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002789
2790 return retval;
2791}
2792EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002793
2794char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2795{
2796 char *p = NULL;
2797 char *retval;
2798
Nick Piggin949854d2011-01-07 17:49:37 +11002799 write_seqlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002800 if (d_unlinked(dentry)) {
2801 p = buf + buflen;
2802 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2803 goto Elong;
2804 buflen++;
2805 }
2806 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002807 write_sequnlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002808 if (!IS_ERR(retval) && p)
2809 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002810 return retval;
2811Elong:
Ram Pai6092d042008-03-27 13:06:20 +01002812 return ERR_PTR(-ENAMETOOLONG);
2813}
2814
2815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * NOTE! The user-level library version returns a
2817 * character pointer. The kernel system call just
2818 * returns the length of the buffer filled (which
2819 * includes the ending '\0' character), or a negative
2820 * error value. So libc would do something like
2821 *
2822 * char *getcwd(char * buf, size_t size)
2823 * {
2824 * int retval;
2825 *
2826 * retval = sys_getcwd(buf, size);
2827 * if (retval >= 0)
2828 * return buf;
2829 * errno = -retval;
2830 * return NULL;
2831 * }
2832 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002833SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Linus Torvalds552ce542007-02-13 12:08:18 -08002835 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002836 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002837 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 if (!page)
2840 return -ENOMEM;
2841
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002842 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Linus Torvalds552ce542007-02-13 12:08:18 -08002844 error = -ENOENT;
Al Viro7ea600b2013-03-26 18:25:57 -04002845 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002846 write_seqlock(&rename_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002847 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002848 unsigned long len;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002849 char *cwd = page + PAGE_SIZE;
2850 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002852 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05002853 error = prepend_path(&pwd, &root, &cwd, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002854 write_sequnlock(&rename_lock);
Al Viro7ea600b2013-03-26 18:25:57 -04002855 br_read_unlock(&vfsmount_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002856
Al Viro02125a82011-12-05 08:43:34 -05002857 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08002858 goto out;
2859
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002860 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05002861 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002862 error = prepend_unreachable(&cwd, &buflen);
2863 if (error)
2864 goto out;
2865 }
2866
Linus Torvalds552ce542007-02-13 12:08:18 -08002867 error = -ERANGE;
2868 len = PAGE_SIZE + page - cwd;
2869 if (len <= size) {
2870 error = len;
2871 if (copy_to_user(buf, cwd, len))
2872 error = -EFAULT;
2873 }
Nick Piggin949854d2011-01-07 17:49:37 +11002874 } else {
2875 write_sequnlock(&rename_lock);
Al Viro7ea600b2013-03-26 18:25:57 -04002876 br_read_unlock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002880 path_put(&pwd);
2881 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 free_page((unsigned long) page);
2883 return error;
2884}
2885
2886/*
2887 * Test whether new_dentry is a subdirectory of old_dentry.
2888 *
2889 * Trivially implemented using the dcache structure
2890 */
2891
2892/**
2893 * is_subdir - is new dentry a subdirectory of old_dentry
2894 * @new_dentry: new dentry
2895 * @old_dentry: old dentry
2896 *
2897 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2898 * Returns 0 otherwise.
2899 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2900 */
2901
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002902int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11002905 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002907 if (new_dentry == old_dentry)
2908 return 1;
2909
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002910 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002913 /*
2914 * Need rcu_readlock to protect against the d_parent trashing
2915 * due to d_move
2916 */
2917 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002918 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002920 else
2921 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11002922 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 return result;
2926}
2927
2928void d_genocide(struct dentry *root)
2929{
Nick Piggin949854d2011-01-07 17:49:37 +11002930 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11002932 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11002933 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Nick Piggin949854d2011-01-07 17:49:37 +11002935 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11002936again:
2937 this_parent = root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002938 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939repeat:
2940 next = this_parent->d_subdirs.next;
2941resume:
2942 while (next != &this_parent->d_subdirs) {
2943 struct list_head *tmp = next;
Al Viro6637ecd2014-10-26 19:19:16 -04002944 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 next = tmp->next;
Nick Piggin949854d2011-01-07 17:49:37 +11002946
Nick Pigginda502952011-01-07 17:49:33 +11002947 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2948 if (d_unhashed(dentry) || !dentry->d_inode) {
2949 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002953 spin_unlock(&this_parent->d_lock);
2954 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002956 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto repeat;
2958 }
Nick Piggin949854d2011-01-07 17:49:37 +11002959 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2960 dentry->d_flags |= DCACHE_GENOCIDE;
2961 dentry->d_count--;
2962 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002963 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Al Viro5f03ac12014-10-26 19:31:10 -04002965 rcu_read_lock();
2966ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (this_parent != root) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07002968 struct dentry *child = this_parent;
Nick Piggin949854d2011-01-07 17:49:37 +11002969 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2970 this_parent->d_flags |= DCACHE_GENOCIDE;
2971 this_parent->d_count--;
2972 }
Al Viro5f03ac12014-10-26 19:31:10 -04002973 this_parent = child->d_parent;
2974
2975 spin_unlock(&child->d_lock);
2976 spin_lock(&this_parent->d_lock);
2977
2978 /* might go back up the wrong parent if we have had a rename. */
2979 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11002980 goto rename_retry;
Al Viro6637ecd2014-10-26 19:19:16 -04002981 next = child->d_child.next;
Al Viro5f03ac12014-10-26 19:31:10 -04002982 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
2983 if (next == &this_parent->d_subdirs)
2984 goto ascend;
2985 child = list_entry(next, struct dentry, d_child);
2986 next = next->next;
2987 }
2988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 goto resume;
2990 }
Nick Piggin58db63d2011-01-07 17:49:39 +11002991 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11002992 goto rename_retry;
Al Viro5f03ac12014-10-26 19:31:10 -04002993 spin_unlock(&this_parent->d_lock);
2994 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11002995 if (locked)
2996 write_sequnlock(&rename_lock);
2997 return;
2998
2999rename_retry:
Al Viro5f03ac12014-10-26 19:31:10 -04003000 spin_unlock(&this_parent->d_lock);
3001 rcu_read_unlock();
Miklos Szeredi8110e162012-09-17 22:23:30 +02003002 if (locked)
3003 goto again;
Nick Piggin58db63d2011-01-07 17:49:39 +11003004 locked = 1;
3005 write_seqlock(&rename_lock);
3006 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
3009/**
3010 * find_inode_number - check for dentry with name
3011 * @dir: directory to check
3012 * @name: Name to find.
3013 *
3014 * Check whether a dentry already exists for the given name,
3015 * and return the inode number if it has an inode. Otherwise
3016 * 0 is returned.
3017 *
3018 * This routine is used to post-process directory listings for
3019 * filesystems using synthetic inode numbers, and is necessary
3020 * to keep getcwd() working.
3021 */
3022
3023ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3024{
3025 struct dentry * dentry;
3026 ino_t ino = 0;
3027
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08003028 dentry = d_hash_and_lookup(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05003029 if (!IS_ERR_OR_NULL(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (dentry->d_inode)
3031 ino = dentry->d_inode->i_ino;
3032 dput(dentry);
3033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return ino;
3035}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003036EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038static __initdata unsigned long dhash_entries;
3039static int __init set_dhash_entries(char *str)
3040{
3041 if (!str)
3042 return 0;
3043 dhash_entries = simple_strtoul(str, &str, 0);
3044 return 1;
3045}
3046__setup("dhash_entries=", set_dhash_entries);
3047
3048static void __init dcache_init_early(void)
3049{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003050 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /* If hashes are distributed across NUMA nodes, defer
3053 * hash allocation until vmalloc space is available.
3054 */
3055 if (hashdist)
3056 return;
3057
3058 dentry_hashtable =
3059 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003060 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 dhash_entries,
3062 13,
3063 HASH_EARLY,
3064 &d_hash_shift,
3065 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003066 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 0);
3068
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003069 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003070 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
Denis Cheng74bf17c2007-10-16 23:26:30 -07003073static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003075 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 /*
3078 * A constructor could be added for stable state like the lists,
3079 * but it is probably not worth it because of the cache nature
3080 * of the dcache.
3081 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003082 dentry_cache = KMEM_CACHE(dentry,
3083 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 /* Hash may have been set up in dcache_init_early */
3086 if (!hashdist)
3087 return;
3088
3089 dentry_hashtable =
3090 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003091 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 dhash_entries,
3093 13,
3094 0,
3095 &d_hash_shift,
3096 &d_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00003097 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 0);
3099
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003100 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003101 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
3104/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003105struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003106EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108EXPORT_SYMBOL(d_genocide);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110void __init vfs_caches_init_early(void)
3111{
3112 dcache_init_early();
3113 inode_init_early();
3114}
3115
3116void __init vfs_caches_init(unsigned long mempages)
3117{
3118 unsigned long reserve;
3119
3120 /* Base hash sizes on available memory, with a reserve equal to
3121 150% of current kernel size */
3122
3123 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3124 mempages -= reserve;
3125
3126 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003127 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Denis Cheng74bf17c2007-10-16 23:26:30 -07003129 dcache_init();
3130 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07003132 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 bdev_cache_init();
3134 chrdev_init();
3135}