blob: 4556f8fd5f6a554de4529b4b38c3af9e4da96adc [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>
David Howells07f3f052006-09-30 20:52:18 +020040#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050041#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Nick Piggin789680d2011-01-07 17:49:30 +110043/*
44 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110045 * dcache->d_inode->i_lock protects:
Al Viroa7cca092014-10-26 19:19:16 -040046 * - i_dentry, d_u.d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110047 * dcache_hash_bucket lock protects:
48 * - the dcache hash table
49 * s_anon bl list spinlock protects:
50 * - the s_anon list (see __d_drop)
Nick Piggin23044502011-01-07 17:49:31 +110051 * dcache_lru_lock protects:
52 * - the dcache lru lists and counters
53 * d_lock protects:
54 * - d_flags
55 * - d_name
56 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110057 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110058 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110059 * - d_parent and d_subdirs
60 * - childrens' d_child and d_parent
Al Viroa7cca092014-10-26 19:19:16 -040061 * - d_u.d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110062 *
63 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110064 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110065 * dentry->d_lock
66 * dcache_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110067 * dcache_hash_bucket lock
68 * s_anon lock
Nick Piggin789680d2011-01-07 17:49:30 +110069 *
Nick Pigginda502952011-01-07 17:49:33 +110070 * If there is an ancestor relationship:
71 * dentry->d_parent->...->d_parent->d_lock
72 * ...
73 * dentry->d_parent->d_lock
74 * dentry->d_lock
75 *
76 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110077 * if (dentry1 < dentry2)
78 * dentry1->d_lock
79 * dentry2->d_lock
80 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080081int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
83
Nick Piggin23044502011-01-07 17:49:31 +110084static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040085__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Nick Piggin949854d2011-01-07 17:49:37 +110087EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Lametere18b8902006-12-06 20:33:20 -080089static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * This is the single most critical data structure when it comes
93 * to the dcache: the hashtable for lookups. Somebody should try
94 * to make this good - I've just made it work.
95 *
96 * This hash-function tries to avoid losing too many bits of hash
97 * information, yet avoid using a prime hash-size or similar.
98 */
99#define D_HASHBITS d_hash_shift
100#define D_HASHMASK d_hash_mask
101
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800102static unsigned int d_hash_mask __read_mostly;
103static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100104
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700105static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100106
Linus Torvalds8966be92012-03-02 14:23:30 -0800107static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700108 unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100109{
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700110 hash += (unsigned long) parent / L1_CACHE_BYTES;
111 hash = hash + (hash >> D_HASHBITS);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100112 return dentry_hashtable + (hash & D_HASHMASK);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Statistics gathering. */
116struct dentry_stat_t dentry_stat = {
117 .age_limit = 45,
118};
119
Nick Piggin3e880fb2011-01-07 17:49:19 +1100120static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400121
122#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100123static int get_nr_dentry(void)
124{
125 int i;
126 int sum = 0;
127 for_each_possible_cpu(i)
128 sum += per_cpu(nr_dentry, i);
129 return sum < 0 ? 0 : sum;
130}
131
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400132int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
133 size_t *lenp, loff_t *ppos)
134{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100135 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400136 return proc_dointvec(table, write, buffer, lenp, ppos);
137}
138#endif
139
Linus Torvalds5483f182012-03-04 15:51:42 -0800140/*
141 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
142 * The strings are both count bytes long, and count is non-zero.
143 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700144#ifdef CONFIG_DCACHE_WORD_ACCESS
145
146#include <asm/word-at-a-time.h>
147/*
148 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
149 * aligned allocation for this particular component. We don't
150 * strictly need the load_unaligned_zeropad() safety, but it
151 * doesn't hurt either.
152 *
153 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
154 * need the careful unaligned handling.
155 */
Linus Torvalds5483f182012-03-04 15:51:42 -0800156static inline int dentry_cmp(const unsigned char *cs, size_t scount,
157 const unsigned char *ct, size_t tcount)
158{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800159 unsigned long a,b,mask;
160
161 if (unlikely(scount != tcount))
162 return 1;
163
164 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700165 a = load_unaligned_zeropad(cs);
166 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800167 if (tcount < sizeof(unsigned long))
168 break;
169 if (unlikely(a != b))
170 return 1;
171 cs += sizeof(unsigned long);
172 ct += sizeof(unsigned long);
173 tcount -= sizeof(unsigned long);
174 if (!tcount)
175 return 0;
176 }
177 mask = ~(~0ul << tcount*8);
178 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700179}
180
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800181#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700182
183static inline int dentry_cmp(const unsigned char *cs, size_t scount,
184 const unsigned char *ct, size_t tcount)
185{
Linus Torvalds5483f182012-03-04 15:51:42 -0800186 if (scount != tcount)
187 return 1;
188
189 do {
190 if (*cs != *ct)
191 return 1;
192 cs++;
193 ct++;
194 tcount--;
195 } while (tcount);
196 return 0;
197}
198
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700199#endif
200
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400201static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400203 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (dname_external(dentry))
206 kfree(dentry->d_name.name);
207 kmem_cache_free(dentry_cache, dentry);
208}
209
gregkh@linuxfoundation.orgeba65b02017-08-04 16:16:18 -0700210void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
211{
212 spin_lock(&dentry->d_lock);
213 if (unlikely(dname_external(dentry))) {
214 u32 len;
215 char *p;
216
217 for (;;) {
218 len = dentry->d_name.len;
219 spin_unlock(&dentry->d_lock);
220
221 p = kmalloc(len + 1, GFP_KERNEL | __GFP_NOFAIL);
222
223 spin_lock(&dentry->d_lock);
224 if (dentry->d_name.len <= len)
225 break;
226 kfree(p);
227 }
228 memcpy(p, dentry->d_name.name, dentry->d_name.len + 1);
229 spin_unlock(&dentry->d_lock);
230
231 name->name = p;
232 } else {
233 memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
234 spin_unlock(&dentry->d_lock);
235 name->name = name->inline_name;
236 }
237}
238EXPORT_SYMBOL(take_dentry_name_snapshot);
239
240void release_dentry_name_snapshot(struct name_snapshot *name)
241{
242 if (unlikely(name->name != name->inline_name))
243 kfree(name->name);
244}
245EXPORT_SYMBOL(release_dentry_name_snapshot);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100248 * no locks, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250static void d_free(struct dentry *dentry)
251{
Al Viroa7cca092014-10-26 19:19:16 -0400252 WARN_ON(!list_empty(&dentry->d_u.d_alias));
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100253 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100254 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (dentry->d_op && dentry->d_op->d_release)
256 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400257
Linus Torvaldsdea36672011-04-24 07:58:46 -0700258 /* if dentry was never visible to RCU, immediate free is OK */
259 if (!(dentry->d_flags & DCACHE_RCUACCESS))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400260 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800261 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400262 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Nick Piggin31e6b012011-01-07 17:49:52 +1100265/**
266 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800267 * @dentry: the target dentry
Nick Piggin31e6b012011-01-07 17:49:52 +1100268 * After this call, in-progress rcu-walk path lookup will fail. This
269 * should be called after unhashing, and after changing d_inode (if
270 * the dentry has not already been unhashed).
271 */
272static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
273{
274 assert_spin_locked(&dentry->d_lock);
275 /* Go through a barrier */
276 write_seqcount_barrier(&dentry->d_seq);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Release the dentry's inode, using the filesystem
Nick Piggin31e6b012011-01-07 17:49:52 +1100281 * d_iput() operation if defined. Dentry has no refcount
282 * and is unhashed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800284static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200285 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100286 __releases(dentry->d_inode->i_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct inode *inode = dentry->d_inode;
289 if (inode) {
290 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400291 list_del_init(&dentry->d_u.d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100293 spin_unlock(&inode->i_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700294 if (!inode->i_nlink)
295 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (dentry->d_op && dentry->d_op->d_iput)
297 dentry->d_op->d_iput(dentry, inode);
298 else
299 iput(inode);
300 } else {
301 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700305/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100306 * Release the dentry's inode, using the filesystem
307 * d_iput() operation if defined. dentry remains in-use.
308 */
309static void dentry_unlink_inode(struct dentry * dentry)
310 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100311 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100312{
313 struct inode *inode = dentry->d_inode;
314 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400315 list_del_init(&dentry->d_u.d_alias);
Nick Piggin31e6b012011-01-07 17:49:52 +1100316 dentry_rcuwalk_barrier(dentry);
317 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100318 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100319 if (!inode->i_nlink)
320 fsnotify_inoderemove(inode);
321 if (dentry->d_op && dentry->d_op->d_iput)
322 dentry->d_op->d_iput(dentry, inode);
323 else
324 iput(inode);
325}
326
327/*
Sage Weilf0023bc2011-10-28 10:02:42 -0700328 * dentry_lru_(add|del|prune|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700329 */
330static void dentry_lru_add(struct dentry *dentry)
331{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400332 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100333 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400334 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
335 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100336 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100337 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400338 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700339}
340
Nick Piggin23044502011-01-07 17:49:31 +1100341static void __dentry_lru_del(struct dentry *dentry)
342{
343 list_del_init(&dentry->d_lru);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100344 dentry->d_flags &= ~DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100345 dentry->d_sb->s_nr_dentry_unused--;
346 dentry_stat.nr_unused--;
347}
348
Sage Weilf0023bc2011-10-28 10:02:42 -0700349/*
350 * Remove a dentry with references from the LRU.
351 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700352static void dentry_lru_del(struct dentry *dentry)
353{
354 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100355 spin_lock(&dcache_lru_lock);
356 __dentry_lru_del(dentry);
357 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700358 }
359}
360
Sage Weilf0023bc2011-10-28 10:02:42 -0700361/*
362 * Remove a dentry that is unreferenced and about to be pruned
363 * (unhashed and destroyed) from the LRU, and inform the file system.
364 * This wrapper should be called _prior_ to unhashing a victim dentry.
365 */
366static void dentry_lru_prune(struct dentry *dentry)
367{
368 if (!list_empty(&dentry->d_lru)) {
369 if (dentry->d_flags & DCACHE_OP_PRUNE)
370 dentry->d_op->d_prune(dentry);
371
372 spin_lock(&dcache_lru_lock);
373 __dentry_lru_del(dentry);
374 spin_unlock(&dcache_lru_lock);
375 }
376}
377
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000378static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700379{
Nick Piggin23044502011-01-07 17:49:31 +1100380 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400381 if (list_empty(&dentry->d_lru)) {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000382 list_add_tail(&dentry->d_lru, list);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400383 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100384 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400385 } else {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000386 list_move_tail(&dentry->d_lru, list);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700387 }
Nick Piggin23044502011-01-07 17:49:31 +1100388 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700389}
390
Miklos Szeredid52b9082007-05-08 00:23:46 -0700391/**
392 * d_kill - kill dentry and return parent
393 * @dentry: dentry to kill
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800394 * @parent: parent dentry
Miklos Szeredid52b9082007-05-08 00:23:46 -0700395 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200396 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700397 *
398 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100399 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100400 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
401 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700402 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100403static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200404 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100405 __releases(parent->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100406 __releases(dentry->d_inode->i_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700407{
Al Viroa7cca092014-10-26 19:19:16 -0400408 list_del(&dentry->d_child);
Trond Myklebustc83ce982011-03-15 13:36:43 -0400409 /*
410 * Inform try_to_ascend() that we are no longer attached to the
411 * dentry tree
412 */
Miklos Szeredibd92dd0d2012-09-17 22:31:38 +0200413 dentry->d_flags |= DCACHE_DENTRY_KILLED;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100414 if (parent)
415 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700416 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100417 /*
418 * dentry_iput drops the locks, at which point nobody (except
419 * transient RCU lookups) can reach this dentry.
420 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700421 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900422 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700423}
424
David Howellsc6627c62011-06-07 14:09:20 +0100425/*
426 * Unhash a dentry without inserting an RCU walk barrier or checking that
427 * dentry->d_lock is locked. The caller must take care of that, if
428 * appropriate.
429 */
430static void __d_shrink(struct dentry *dentry)
431{
432 if (!d_unhashed(dentry)) {
433 struct hlist_bl_head *b;
434 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
435 b = &dentry->d_sb->s_anon;
436 else
437 b = d_hash(dentry->d_parent, dentry->d_name.hash);
438
439 hlist_bl_lock(b);
440 __hlist_bl_del(&dentry->d_hash);
441 dentry->d_hash.pprev = NULL;
442 hlist_bl_unlock(b);
443 }
444}
445
Nick Piggin789680d2011-01-07 17:49:30 +1100446/**
447 * d_drop - drop a dentry
448 * @dentry: dentry to drop
449 *
450 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
451 * be found through a VFS lookup any more. Note that this is different from
452 * deleting the dentry - d_delete will try to mark the dentry negative if
453 * possible, giving a successful _negative_ lookup, while d_drop will
454 * just make the cache lookup fail.
455 *
456 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
457 * reason (NFS timeouts or autofs deletes).
458 *
459 * __d_drop requires dentry->d_lock.
460 */
461void __d_drop(struct dentry *dentry)
462{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700463 if (!d_unhashed(dentry)) {
David Howellsc6627c62011-06-07 14:09:20 +0100464 __d_shrink(dentry);
Linus Torvaldsdea36672011-04-24 07:58:46 -0700465 dentry_rcuwalk_barrier(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +1100466 }
467}
468EXPORT_SYMBOL(__d_drop);
469
470void d_drop(struct dentry *dentry)
471{
Nick Piggin789680d2011-01-07 17:49:30 +1100472 spin_lock(&dentry->d_lock);
473 __d_drop(dentry);
474 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100475}
476EXPORT_SYMBOL(d_drop);
477
Nick Piggin77812a12011-01-07 17:49:48 +1100478/*
Josef Bacik44396f42011-05-31 11:58:49 -0400479 * d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag
480 * @dentry: dentry to drop
481 *
482 * This is called when we do a lookup on a placeholder dentry that needed to be
483 * looked up. The dentry should have been hashed in order for it to be found by
484 * the lookup code, but now needs to be unhashed while we do the actual lookup
485 * and clear the DCACHE_NEED_LOOKUP flag.
486 */
487void d_clear_need_lookup(struct dentry *dentry)
488{
489 spin_lock(&dentry->d_lock);
490 __d_drop(dentry);
491 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
492 spin_unlock(&dentry->d_lock);
493}
494EXPORT_SYMBOL(d_clear_need_lookup);
495
496/*
Nick Piggin77812a12011-01-07 17:49:48 +1100497 * Finish off a dentry we've decided to kill.
498 * dentry->d_lock must be held, returns with it unlocked.
499 * If ref is non-zero, then decrement the refcount too.
500 * Returns dentry requiring refcount drop, or NULL if we're done.
501 */
502static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
503 __releases(dentry->d_lock)
504{
Nick Piggin873feea2011-01-07 17:50:06 +1100505 struct inode *inode;
Nick Piggin77812a12011-01-07 17:49:48 +1100506 struct dentry *parent;
507
Nick Piggin873feea2011-01-07 17:50:06 +1100508 inode = dentry->d_inode;
509 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin77812a12011-01-07 17:49:48 +1100510relock:
511 spin_unlock(&dentry->d_lock);
512 cpu_relax();
513 return dentry; /* try again with same dentry */
514 }
515 if (IS_ROOT(dentry))
516 parent = NULL;
517 else
518 parent = dentry->d_parent;
519 if (parent && !spin_trylock(&parent->d_lock)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100520 if (inode)
521 spin_unlock(&inode->i_lock);
Nick Piggin77812a12011-01-07 17:49:48 +1100522 goto relock;
523 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100524
Nick Piggin77812a12011-01-07 17:49:48 +1100525 if (ref)
526 dentry->d_count--;
Sage Weilf0023bc2011-10-28 10:02:42 -0700527 /*
528 * if dentry was on the d_lru list delete it from there.
529 * inform the fs via d_prune that this dentry is about to be
530 * unhashed and destroyed.
531 */
532 dentry_lru_prune(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100533 /* if it was on the hash then remove it */
534 __d_drop(dentry);
535 return d_kill(dentry, parent);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * This is dput
540 *
541 * This is complicated by the fact that we do not want to put
542 * dentries that are no longer on any hash chain on the unused
543 * list: we'd much rather just get rid of them immediately.
544 *
545 * However, that implies that we have to traverse the dentry
546 * tree upwards to the parents which might _also_ now be
547 * scheduled for deletion (it may have been only waiting for
548 * its last child to go away).
549 *
550 * This tail recursion is done by hand as we don't want to depend
551 * on the compiler to always get this right (gcc generally doesn't).
552 * Real recursion would eat up our stack space.
553 */
554
555/*
556 * dput - release a dentry
557 * @dentry: dentry to release
558 *
559 * Release a dentry. This will drop the usage count and if appropriate
560 * call the dentry unlink method as well as removing it from the queues and
561 * releasing its resources. If the parent dentries were scheduled for release
562 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564void dput(struct dentry *dentry)
565{
566 if (!dentry)
567 return;
568
569repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100570 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_lock(&dentry->d_lock);
Nick Piggin61f3dee2011-01-07 17:49:40 +1100573 BUG_ON(!dentry->d_count);
574 if (dentry->d_count > 1) {
575 dentry->d_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return;
578 }
579
Nick Pigginfb045ad2011-01-07 17:49:55 +1100580 if (dentry->d_flags & DCACHE_OP_DELETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100582 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Nick Piggin265ac902010-10-10 05:36:24 -0400584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Unreachable? Get rid of it */
586 if (d_unhashed(dentry))
587 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400588
Josef Bacik44396f42011-05-31 11:58:49 -0400589 /*
590 * If this dentry needs lookup, don't set the referenced flag so that it
591 * is more likely to be cleaned up by the dcache shrinker in case of
592 * memory pressure.
593 */
594 if (!d_need_lookup(dentry))
595 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400596 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400597
Nick Piggin61f3dee2011-01-07 17:49:40 +1100598 dentry->d_count--;
599 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return;
601
Miklos Szeredid52b9082007-05-08 00:23:46 -0700602kill_it:
Nick Piggin77812a12011-01-07 17:49:48 +1100603 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700604 if (dentry)
605 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700607EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609/**
610 * d_invalidate - invalidate a dentry
611 * @dentry: dentry to invalidate
612 *
613 * Try to invalidate the dentry if it turns out to be
614 * possible. If there are other dentries that can be
615 * reached through this one we can't delete it and we
616 * return -EBUSY. On success we return 0.
617 *
618 * no dcache lock.
619 */
620
621int d_invalidate(struct dentry * dentry)
622{
623 /*
624 * If it's already been dropped, return OK.
625 */
Nick Pigginda502952011-01-07 17:49:33 +1100626 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100628 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630 }
631 /*
632 * Check whether to do a partial shrink_dcache
633 * to get rid of unused child entries.
634 */
635 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100636 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100638 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
641 /*
642 * Somebody else still using it?
643 *
644 * If it's a directory, we can't drop it
645 * for fear of somebody re-populating it
646 * with children (even though dropping it
647 * would make it unreachable from the root,
648 * we might still populate it if it was a
649 * working directory or similar).
Al Viro50e69632011-11-07 16:39:57 +0000650 * We also need to leave mountpoints alone,
651 * directory or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Al Viro50e69632011-11-07 16:39:57 +0000653 if (dentry->d_count > 1 && dentry->d_inode) {
654 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EBUSY;
657 }
658 }
659
660 __d_drop(dentry);
661 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700664EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100666/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100667static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100669 dentry->d_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Nick Piggindc0474b2011-01-07 17:49:43 +1100672static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100673{
Nick Piggin23044502011-01-07 17:49:31 +1100674 spin_lock(&dentry->d_lock);
Nick Piggindc0474b2011-01-07 17:49:43 +1100675 __dget_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100676 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100677}
678
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100679struct dentry *dget_parent(struct dentry *dentry)
680{
681 struct dentry *ret;
682
683repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100684 /*
685 * Don't need rcu_dereference because we re-check it was correct under
686 * the lock.
687 */
688 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100689 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100690 spin_lock(&ret->d_lock);
691 if (unlikely(ret != dentry->d_parent)) {
692 spin_unlock(&ret->d_lock);
693 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100694 goto repeat;
695 }
Nick Piggina734eb42011-01-07 17:49:44 +1100696 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100697 BUG_ON(!ret->d_count);
698 ret->d_count++;
699 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100700 return ret;
701}
702EXPORT_SYMBOL(dget_parent);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/**
705 * d_find_alias - grab a hashed alias of inode
706 * @inode: inode in question
707 * @want_discon: flag, used by d_splice_alias, to request
708 * that only a DISCONNECTED alias be returned.
709 *
710 * If inode has a hashed alias, or is a directory and has any alias,
711 * acquire the reference to alias and return it. Otherwise return NULL.
712 * Notice that if inode is a directory there can be only one alias and
713 * it can be unhashed only if it has no children, or if it is the root
714 * of a filesystem.
715 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700716 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700718 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
Nick Pigginda502952011-01-07 17:49:33 +1100720static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Nick Pigginda502952011-01-07 17:49:33 +1100722 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Nick Pigginda502952011-01-07 17:49:33 +1100724again:
725 discon_alias = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400726 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100727 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700729 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100730 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100732 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100733 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100734 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return alias;
736 }
737 }
Nick Pigginda502952011-01-07 17:49:33 +1100738 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Nick Pigginda502952011-01-07 17:49:33 +1100740 if (discon_alias) {
741 alias = discon_alias;
742 spin_lock(&alias->d_lock);
743 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
744 if (IS_ROOT(alias) &&
745 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100746 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100747 spin_unlock(&alias->d_lock);
748 return alias;
749 }
750 }
751 spin_unlock(&alias->d_lock);
752 goto again;
753 }
754 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Nick Pigginda502952011-01-07 17:49:33 +1100757struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
David Howells214fda12006-03-25 03:06:36 -0800759 struct dentry *de = NULL;
760
761 if (!list_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100762 spin_lock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800763 de = __d_find_alias(inode, 0);
Nick Piggin873feea2011-01-07 17:50:06 +1100764 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return de;
767}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700768EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770/*
771 * Try to kill dentries associated with this inode.
772 * WARNING: you must own a reference to inode.
773 */
774void d_prune_aliases(struct inode *inode)
775{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700776 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100778 spin_lock(&inode->i_lock);
Al Viroa7cca092014-10-26 19:19:16 -0400779 list_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100781 if (!dentry->d_count) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100782 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 __d_drop(dentry);
784 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100785 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dput(dentry);
787 goto restart;
788 }
789 spin_unlock(&dentry->d_lock);
790 }
Nick Piggin873feea2011-01-07 17:50:06 +1100791 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700793EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795/*
Nick Piggin77812a12011-01-07 17:49:48 +1100796 * Try to throw away a dentry - free the inode, dput the parent.
797 * Requires dentry->d_lock is held, and dentry->d_count == 0.
798 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700799 *
Nick Piggin77812a12011-01-07 17:49:48 +1100800 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Nick Piggin77812a12011-01-07 17:49:48 +1100802static void try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200803 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Nick Piggin77812a12011-01-07 17:49:48 +1100805 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700806
Nick Piggin77812a12011-01-07 17:49:48 +1100807 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700808 /*
Nick Piggin77812a12011-01-07 17:49:48 +1100809 * If dentry_kill returns NULL, we have nothing more to do.
810 * if it returns the same dentry, trylocks failed. In either
811 * case, just loop again.
812 *
813 * Otherwise, we need to prune ancestors too. This is necessary
814 * to prevent quadratic behavior of shrink_dcache_parent(), but
815 * is also expected to be beneficial in reducing dentry cache
816 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700817 */
Nick Piggin77812a12011-01-07 17:49:48 +1100818 if (!parent)
819 return;
820 if (parent == dentry)
821 return;
822
823 /* Prune ancestors. */
824 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700825 while (dentry) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100826 spin_lock(&dentry->d_lock);
Nick Piggin89e60542011-01-07 17:49:45 +1100827 if (dentry->d_count > 1) {
828 dentry->d_count--;
829 spin_unlock(&dentry->d_lock);
830 return;
831 }
Nick Piggin77812a12011-01-07 17:49:48 +1100832 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400836static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700838 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700839
Nick Pigginec336792011-01-07 17:49:47 +1100840 rcu_read_lock();
841 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100842 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
843 if (&dentry->d_lru == list)
844 break; /* empty */
845 spin_lock(&dentry->d_lock);
846 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
847 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100848 continue;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /*
852 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700853 * the LRU because of laziness during lookup. Do not free
854 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100856 if (dentry->d_count) {
Nick Pigginec336792011-01-07 17:49:47 +1100857 dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700858 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 continue;
860 }
Nick Pigginec336792011-01-07 17:49:47 +1100861
Nick Pigginec336792011-01-07 17:49:47 +1100862 rcu_read_unlock();
Nick Piggin77812a12011-01-07 17:49:48 +1100863
864 try_prune_one_dentry(dentry);
865
Nick Pigginec336792011-01-07 17:49:47 +1100866 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Nick Pigginec336792011-01-07 17:49:47 +1100868 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400869}
870
871/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000872 * prune_dcache_sb - shrink the dcache
873 * @sb: superblock
874 * @count: number of entries to try to free
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400875 *
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000876 * Attempt to shrink the superblock dcache LRU by @count entries. This is
877 * done when we need more memory an called from the superblock shrinker
878 * function.
879 *
880 * This function may fail to free any resources if all the dentries are in
881 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400882 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000883void prune_dcache_sb(struct super_block *sb, int count)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400884{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400885 struct dentry *dentry;
886 LIST_HEAD(referenced);
887 LIST_HEAD(tmp);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400888
Nick Piggin23044502011-01-07 17:49:31 +1100889relock:
890 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400891 while (!list_empty(&sb->s_dentry_lru)) {
892 dentry = list_entry(sb->s_dentry_lru.prev,
893 struct dentry, d_lru);
894 BUG_ON(dentry->d_sb != sb);
895
Nick Piggin23044502011-01-07 17:49:31 +1100896 if (!spin_trylock(&dentry->d_lock)) {
897 spin_unlock(&dcache_lru_lock);
898 cpu_relax();
899 goto relock;
900 }
901
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000902 if (dentry->d_flags & DCACHE_REFERENCED) {
Nick Piggin23044502011-01-07 17:49:31 +1100903 dentry->d_flags &= ~DCACHE_REFERENCED;
904 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400905 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100906 } else {
907 list_move_tail(&dentry->d_lru, &tmp);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100908 dentry->d_flags |= DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100909 spin_unlock(&dentry->d_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000910 if (!--count)
Nick Piggin23044502011-01-07 17:49:31 +1100911 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400912 }
Nick Pigginec336792011-01-07 17:49:47 +1100913 cond_resched_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400914 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700915 if (!list_empty(&referenced))
916 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100917 spin_unlock(&dcache_lru_lock);
Nick Pigginec336792011-01-07 17:49:47 +1100918
919 shrink_dentry_list(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * shrink_dcache_sb - shrink dcache for a superblock
924 * @sb: superblock
925 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400926 * Shrink the dcache for the specified super block. This is used to free
927 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400929void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400931 LIST_HEAD(tmp);
932
Nick Piggin23044502011-01-07 17:49:31 +1100933 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400934 while (!list_empty(&sb->s_dentry_lru)) {
935 list_splice_init(&sb->s_dentry_lru, &tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100936 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400937 shrink_dentry_list(&tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100938 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400939 }
Nick Piggin23044502011-01-07 17:49:31 +1100940 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700942EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944/*
David Howellsc636ebd2006-10-11 01:22:19 -0700945 * destroy a single subtree of dentries for unmount
946 * - see the comments on shrink_dcache_for_umount() for a description of the
947 * locking
948 */
949static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
950{
951 struct dentry *parent;
952
953 BUG_ON(!IS_ROOT(dentry));
954
David Howellsc636ebd2006-10-11 01:22:19 -0700955 for (;;) {
956 /* descend to the first leaf in the current subtree */
David Howells43c1c9c2011-06-07 14:09:30 +0100957 while (!list_empty(&dentry->d_subdirs))
David Howellsc636ebd2006-10-11 01:22:19 -0700958 dentry = list_entry(dentry->d_subdirs.next,
Al Viroa7cca092014-10-26 19:19:16 -0400959 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -0700960
961 /* consume the dentries from this leaf up through its parents
962 * until we find one with children or run out altogether */
963 do {
964 struct inode *inode;
965
Sage Weilf0023bc2011-10-28 10:02:42 -0700966 /*
967 * remove the dentry from the lru, and inform
968 * the fs that this dentry is about to be
969 * unhashed and destroyed.
970 */
971 dentry_lru_prune(dentry);
David Howells43c1c9c2011-06-07 14:09:30 +0100972 __d_shrink(dentry);
973
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100974 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700975 printk(KERN_ERR
976 "BUG: Dentry %p{i=%lx,n=%s}"
977 " still in use (%d)"
978 " [unmount of %s %s]\n",
979 dentry,
980 dentry->d_inode ?
981 dentry->d_inode->i_ino : 0UL,
982 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100983 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700984 dentry->d_sb->s_type->name,
985 dentry->d_sb->s_id);
986 BUG();
987 }
988
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700990 parent = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400991 list_del(&dentry->d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100992 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900993 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100994 parent->d_count--;
Al Viroa7cca092014-10-26 19:19:16 -0400995 list_del(&dentry->d_child);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900996 }
David Howellsc636ebd2006-10-11 01:22:19 -0700997
David Howellsc636ebd2006-10-11 01:22:19 -0700998 inode = dentry->d_inode;
999 if (inode) {
1000 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -04001001 list_del_init(&dentry->d_u.d_alias);
David Howellsc636ebd2006-10-11 01:22:19 -07001002 if (dentry->d_op && dentry->d_op->d_iput)
1003 dentry->d_op->d_iput(dentry, inode);
1004 else
1005 iput(inode);
1006 }
1007
1008 d_free(dentry);
1009
1010 /* finished when we fall off the top of the tree,
1011 * otherwise we ascend to the parent and move to the
1012 * next sibling if there is one */
1013 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001014 return;
David Howellsc636ebd2006-10-11 01:22:19 -07001015 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -07001016 } while (list_empty(&dentry->d_subdirs));
1017
1018 dentry = list_entry(dentry->d_subdirs.next,
Al Viroa7cca092014-10-26 19:19:16 -04001019 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -07001020 }
1021}
1022
1023/*
1024 * destroy the dentries attached to a superblock on unmounting
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001025 * - we don't need to use dentry->d_lock because:
David Howellsc636ebd2006-10-11 01:22:19 -07001026 * - the superblock is detached from all mountings and open files, so the
1027 * dentry trees will not be rearranged by the VFS
1028 * - s_umount is write-locked, so the memory pressure shrinker will ignore
1029 * any dentries belonging to this superblock that it comes across
1030 * - the filesystem itself is no longer permitted to rearrange the dentries
1031 * in this superblock
1032 */
1033void shrink_dcache_for_umount(struct super_block *sb)
1034{
1035 struct dentry *dentry;
1036
1037 if (down_read_trylock(&sb->s_umount))
1038 BUG();
1039
1040 dentry = sb->s_root;
1041 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001042 dentry->d_count--;
David Howellsc636ebd2006-10-11 01:22:19 -07001043 shrink_dcache_for_umount_subtree(dentry);
1044
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001045 while (!hlist_bl_empty(&sb->s_anon)) {
1046 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
David Howellsc636ebd2006-10-11 01:22:19 -07001047 shrink_dcache_for_umount_subtree(dentry);
1048 }
1049}
1050
1051/*
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001052 * This tries to ascend one level of parenthood, but
1053 * we can race with renaming, so we need to re-check
1054 * the parenthood after dropping the lock and check
1055 * that the sequence number still matches.
1056 */
1057static struct dentry *try_to_ascend(struct dentry *old, int locked, unsigned seq)
1058{
1059 struct dentry *new = old->d_parent;
1060
1061 rcu_read_lock();
1062 spin_unlock(&old->d_lock);
1063 spin_lock(&new->d_lock);
1064
1065 /*
1066 * might go back up the wrong parent if we have had a rename
1067 * or deletion
1068 */
1069 if (new != old->d_parent ||
Miklos Szeredibd92dd0d2012-09-17 22:31:38 +02001070 (old->d_flags & DCACHE_DENTRY_KILLED) ||
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001071 (!locked && read_seqretry(&rename_lock, seq))) {
1072 spin_unlock(&new->d_lock);
1073 new = NULL;
1074 }
1075 rcu_read_unlock();
1076 return new;
1077}
1078
1079
1080/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * Search for at least 1 mount point in the dentry's subdirs.
1082 * We descend to the next level whenever the d_subdirs
1083 * list is non-empty and continue searching.
1084 */
1085
1086/**
1087 * have_submounts - check for mounts over a dentry
1088 * @parent: dentry to check.
1089 *
1090 * Return true if the parent or its subdirectories contain
1091 * a mount point
1092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093int have_submounts(struct dentry *parent)
1094{
Nick Piggin949854d2011-01-07 17:49:37 +11001095 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001097 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11001098 int locked = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11001099
Nick Piggin949854d2011-01-07 17:49:37 +11001100 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001101again:
1102 this_parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (d_mountpoint(parent))
1105 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001106 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107repeat:
1108 next = this_parent->d_subdirs.next;
1109resume:
1110 while (next != &this_parent->d_subdirs) {
1111 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04001112 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001114
1115 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001117 if (d_mountpoint(dentry)) {
1118 spin_unlock(&dentry->d_lock);
1119 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001123 spin_unlock(&this_parent->d_lock);
1124 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001126 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto repeat;
1128 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001129 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 /*
1132 * All done at this level ... ascend and resume the search.
1133 */
1134 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001135 struct dentry *child = this_parent;
1136 this_parent = try_to_ascend(this_parent, locked, seq);
1137 if (!this_parent)
Nick Piggin949854d2011-01-07 17:49:37 +11001138 goto rename_retry;
Al Viroa7cca092014-10-26 19:19:16 -04001139 next = child->d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto resume;
1141 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001142 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001143 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001144 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001145 if (locked)
1146 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return 0; /* No mount points found in tree */
1148positive:
Nick Piggin58db63d2011-01-07 17:49:39 +11001149 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001150 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001151 if (locked)
1152 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001154
1155rename_retry:
1156 locked = 1;
1157 write_seqlock(&rename_lock);
1158 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001160EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/*
1163 * Search the dentry child list for the specified parent,
1164 * and move any unused dentries to the end of the unused
1165 * list for prune_dcache(). We descend to the next level
1166 * whenever the d_subdirs list is non-empty and continue
1167 * searching.
1168 *
1169 * It returns zero iff there are no unused children,
1170 * otherwise it returns the number of children moved to
1171 * the end of the unused list. This may not be the total
1172 * number of unused children, because select_parent can
1173 * drop the lock and return early due to latency
1174 * constraints.
1175 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001176static int select_parent(struct dentry *parent, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Nick Piggin949854d2011-01-07 17:49:37 +11001178 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001180 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int found = 0;
Nick Piggin58db63d2011-01-07 17:49:39 +11001182 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Nick Piggin949854d2011-01-07 17:49:37 +11001184 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001185again:
1186 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001187 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188repeat:
1189 next = this_parent->d_subdirs.next;
1190resume:
1191 while (next != &this_parent->d_subdirs) {
1192 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04001193 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 next = tmp->next;
1195
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001196 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001197
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001198 /*
1199 * move only zero ref count dentries to the dispose list.
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001200 *
1201 * Those which are presently on the shrink list, being processed
1202 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1203 * loop in shrink_dcache_parent() might not make any progress
1204 * and loop forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001206 if (dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001207 dentry_lru_del(dentry);
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001208 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1209 dentry_lru_move_list(dentry, dispose);
1210 dentry->d_flags |= DCACHE_SHRINK_LIST;
1211 found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * We can return to the caller if we have found some (this
1215 * ensures forward progress). We'll be coming back to find
1216 * the rest.
1217 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001218 if (found && need_resched()) {
1219 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /*
1224 * Descend a level if the d_subdirs list is non-empty.
1225 */
1226 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001227 spin_unlock(&this_parent->d_lock);
1228 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001230 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 goto repeat;
1232 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001233
1234 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 /*
1237 * All done at this level ... ascend and resume the search.
1238 */
1239 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001240 struct dentry *child = this_parent;
1241 this_parent = try_to_ascend(this_parent, locked, seq);
1242 if (!this_parent)
Nick Piggin949854d2011-01-07 17:49:37 +11001243 goto rename_retry;
Al Viroa7cca092014-10-26 19:19:16 -04001244 next = child->d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 goto resume;
1246 }
1247out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001248 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001249 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001250 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001251 if (locked)
1252 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return found;
Nick Piggin58db63d2011-01-07 17:49:39 +11001254
1255rename_retry:
1256 if (found)
1257 return found;
1258 locked = 1;
1259 write_seqlock(&rename_lock);
1260 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263/**
1264 * shrink_dcache_parent - prune dcache
1265 * @parent: parent of entries to prune
1266 *
1267 * Prune the dcache to remove unused children of the parent dentry.
1268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269void shrink_dcache_parent(struct dentry * parent)
1270{
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001271 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int found;
1273
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001274 while ((found = select_parent(parent, &dispose)) != 0)
1275 shrink_dentry_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001277EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/**
Al Viroa4464db2011-07-07 15:03:58 -04001280 * __d_alloc - allocate a dcache entry
1281 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * @name: qstr of the name
1283 *
1284 * Allocates a dentry. It returns %NULL if there is insufficient memory
1285 * available. On a success the dentry is returned. The name passed in is
1286 * copied and the copy passed in may be reused after this call.
1287 */
1288
Al Viroa4464db2011-07-07 15:03:58 -04001289struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct dentry *dentry;
1292 char *dname;
1293
Mel Gormane12ba742007-10-16 01:25:52 -07001294 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!dentry)
1296 return NULL;
1297
1298 if (name->len > DNAME_INLINE_LEN-1) {
1299 dname = kmalloc(name->len + 1, GFP_KERNEL);
1300 if (!dname) {
1301 kmem_cache_free(dentry_cache, dentry);
1302 return NULL;
1303 }
1304 } else {
1305 dname = dentry->d_iname;
1306 }
1307 dentry->d_name.name = dname;
1308
1309 dentry->d_name.len = name->len;
1310 dentry->d_name.hash = name->hash;
1311 memcpy(dname, name->name, name->len);
1312 dname[name->len] = 0;
1313
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001314 dentry->d_count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001315 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001317 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001319 dentry->d_parent = dentry;
1320 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 dentry->d_op = NULL;
1322 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001323 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 INIT_LIST_HEAD(&dentry->d_lru);
1325 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viroa7cca092014-10-26 19:19:16 -04001326 INIT_LIST_HEAD(&dentry->d_u.d_alias);
1327 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001328 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Nick Piggin3e880fb2011-01-07 17:49:19 +11001330 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return dentry;
1333}
Al Viroa4464db2011-07-07 15:03:58 -04001334
1335/**
1336 * d_alloc - allocate a dcache entry
1337 * @parent: parent of entry to allocate
1338 * @name: qstr of the name
1339 *
1340 * Allocates a dentry. It returns %NULL if there is insufficient memory
1341 * available. On a success the dentry is returned. The name passed in is
1342 * copied and the copy passed in may be reused after this call.
1343 */
1344struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1345{
1346 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1347 if (!dentry)
1348 return NULL;
1349
1350 spin_lock(&parent->d_lock);
1351 /*
1352 * don't need child lock because it is not subject
1353 * to concurrency here
1354 */
1355 __dget_dlock(parent);
1356 dentry->d_parent = parent;
Al Viroa7cca092014-10-26 19:19:16 -04001357 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001358 spin_unlock(&parent->d_lock);
1359
1360 return dentry;
1361}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001362EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Nick Piggin4b936882011-01-07 17:50:07 +11001364struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1365{
Al Viroa4464db2011-07-07 15:03:58 -04001366 struct dentry *dentry = __d_alloc(sb, name);
1367 if (dentry)
Nick Piggin4b936882011-01-07 17:50:07 +11001368 dentry->d_flags |= DCACHE_DISCONNECTED;
Nick Piggin4b936882011-01-07 17:50:07 +11001369 return dentry;
1370}
1371EXPORT_SYMBOL(d_alloc_pseudo);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1374{
1375 struct qstr q;
1376
1377 q.name = name;
1378 q.len = strlen(name);
1379 q.hash = full_name_hash(q.name, q.len);
1380 return d_alloc(parent, &q);
1381}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001382EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Nick Pigginfb045ad2011-01-07 17:49:55 +11001384void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1385{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001386 WARN_ON_ONCE(dentry->d_op);
1387 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001388 DCACHE_OP_COMPARE |
1389 DCACHE_OP_REVALIDATE |
1390 DCACHE_OP_DELETE ));
1391 dentry->d_op = op;
1392 if (!op)
1393 return;
1394 if (op->d_hash)
1395 dentry->d_flags |= DCACHE_OP_HASH;
1396 if (op->d_compare)
1397 dentry->d_flags |= DCACHE_OP_COMPARE;
1398 if (op->d_revalidate)
1399 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1400 if (op->d_delete)
1401 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001402 if (op->d_prune)
1403 dentry->d_flags |= DCACHE_OP_PRUNE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001404
1405}
1406EXPORT_SYMBOL(d_set_d_op);
1407
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001408static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1409{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001410 spin_lock(&dentry->d_lock);
David Howells9875cf82011-01-14 18:45:21 +00001411 if (inode) {
1412 if (unlikely(IS_AUTOMOUNT(inode)))
1413 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
Al Viroa7cca092014-10-26 19:19:16 -04001414 list_add(&dentry->d_u.d_alias, &inode->i_dentry);
David Howells9875cf82011-01-14 18:45:21 +00001415 }
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001416 dentry->d_inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001417 dentry_rcuwalk_barrier(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001418 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001419 fsnotify_d_instantiate(dentry, inode);
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/**
1423 * d_instantiate - fill in inode information for a dentry
1424 * @entry: dentry to complete
1425 * @inode: inode to attach to this dentry
1426 *
1427 * Fill in inode information in the entry.
1428 *
1429 * This turns negative dentries into productive full members
1430 * of society.
1431 *
1432 * NOTE! This assumes that the inode count has been incremented
1433 * (or otherwise set) by the caller to indicate that it is now
1434 * in use by the dcache.
1435 */
1436
1437void d_instantiate(struct dentry *entry, struct inode * inode)
1438{
Al Viroa7cca092014-10-26 19:19:16 -04001439 BUG_ON(!list_empty(&entry->d_u.d_alias));
Nick Piggin873feea2011-01-07 17:50:06 +11001440 if (inode)
1441 spin_lock(&inode->i_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001442 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001443 if (inode)
1444 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 security_d_instantiate(entry, inode);
1446}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001447EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449/**
1450 * d_instantiate_unique - instantiate a non-aliased dentry
1451 * @entry: dentry to instantiate
1452 * @inode: inode to attach to this dentry
1453 *
1454 * Fill in inode information in the entry. On success, it returns NULL.
1455 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001456 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * Note that in order to avoid conflicts with rename() etc, the caller
1459 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001460 *
1461 * This also assumes that the inode count has been incremented
1462 * (or otherwise set) by the caller to indicate that it is now
1463 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
David Howells770bfad2006-08-22 20:06:07 -04001465static struct dentry *__d_instantiate_unique(struct dentry *entry,
1466 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct dentry *alias;
1469 int len = entry->d_name.len;
1470 const char *name = entry->d_name.name;
1471 unsigned int hash = entry->d_name.hash;
1472
David Howells770bfad2006-08-22 20:06:07 -04001473 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001474 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001475 return NULL;
1476 }
1477
Al Viroa7cca092014-10-26 19:19:16 -04001478 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct qstr *qstr = &alias->d_name;
1480
Nick Piggin9abca362011-01-07 17:49:36 +11001481 /*
1482 * Don't need alias->d_lock here, because aliases with
1483 * d_parent == entry->d_parent are not subject to name or
1484 * parent changes, because the parent inode i_mutex is held.
1485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (qstr->hash != hash)
1487 continue;
1488 if (alias->d_parent != entry->d_parent)
1489 continue;
Nick Piggin9d55c362011-01-07 17:50:09 +11001490 if (dentry_cmp(qstr->name, qstr->len, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001492 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return alias;
1494 }
David Howells770bfad2006-08-22 20:06:07 -04001495
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001496 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return NULL;
1498}
David Howells770bfad2006-08-22 20:06:07 -04001499
1500struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1501{
1502 struct dentry *result;
1503
Al Viroa7cca092014-10-26 19:19:16 -04001504 BUG_ON(!list_empty(&entry->d_u.d_alias));
David Howells770bfad2006-08-22 20:06:07 -04001505
Nick Piggin873feea2011-01-07 17:50:06 +11001506 if (inode)
1507 spin_lock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001508 result = __d_instantiate_unique(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001509 if (inode)
1510 spin_unlock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001511
1512 if (!result) {
1513 security_d_instantiate(entry, inode);
1514 return NULL;
1515 }
1516
1517 BUG_ON(!d_unhashed(result));
1518 iput(inode);
1519 return result;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522EXPORT_SYMBOL(d_instantiate_unique);
1523
Al Viroadc0e912012-01-08 16:49:21 -05001524struct dentry *d_make_root(struct inode *root_inode)
1525{
1526 struct dentry *res = NULL;
1527
1528 if (root_inode) {
1529 static const struct qstr name = { .name = "/", .len = 1 };
1530
1531 res = __d_alloc(root_inode->i_sb, &name);
1532 if (res)
1533 d_instantiate(res, root_inode);
1534 else
1535 iput(root_inode);
1536 }
1537 return res;
1538}
1539EXPORT_SYMBOL(d_make_root);
1540
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001541static struct dentry * __d_find_any_alias(struct inode *inode)
1542{
1543 struct dentry *alias;
1544
1545 if (list_empty(&inode->i_dentry))
1546 return NULL;
Al Viroa7cca092014-10-26 19:19:16 -04001547 alias = list_first_entry(&inode->i_dentry, struct dentry, d_u.d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001548 __dget(alias);
1549 return alias;
1550}
1551
Sage Weil46f72b32012-01-10 09:04:37 -08001552/**
1553 * d_find_any_alias - find any alias for a given inode
1554 * @inode: inode to find an alias for
1555 *
1556 * If any aliases exist for the given inode, take and return a
1557 * reference for one of them. If no aliases exist, return %NULL.
1558 */
1559struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001560{
1561 struct dentry *de;
1562
1563 spin_lock(&inode->i_lock);
1564 de = __d_find_any_alias(inode);
1565 spin_unlock(&inode->i_lock);
1566 return de;
1567}
Sage Weil46f72b32012-01-10 09:04:37 -08001568EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001571 * d_obtain_alias - find or allocate a dentry for a given inode
1572 * @inode: inode to allocate the dentry for
1573 *
1574 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1575 * similar open by handle operations. The returned dentry may be anonymous,
1576 * or may have a full name (if the inode was already in the cache).
1577 *
1578 * When called on a directory inode, we must ensure that the inode only ever
1579 * has one dentry. If a dentry is found, that is returned instead of
1580 * allocating a new one.
1581 *
1582 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001583 * to the dentry. In case of an error the reference on the inode is released.
1584 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1585 * be passed in and will be the error will be propagate to the return value,
1586 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001587 */
1588struct dentry *d_obtain_alias(struct inode *inode)
1589{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001590 static const struct qstr anonstring = { .name = "" };
1591 struct dentry *tmp;
1592 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001593
1594 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001595 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001596 if (IS_ERR(inode))
1597 return ERR_CAST(inode);
1598
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001599 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001600 if (res)
1601 goto out_iput;
1602
Al Viroa4464db2011-07-07 15:03:58 -04001603 tmp = __d_alloc(inode->i_sb, &anonstring);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001604 if (!tmp) {
1605 res = ERR_PTR(-ENOMEM);
1606 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001607 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001608
Nick Piggin873feea2011-01-07 17:50:06 +11001609 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001610 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001611 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001612 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001613 dput(tmp);
1614 goto out_iput;
1615 }
1616
1617 /* attach a disconnected dentry */
1618 spin_lock(&tmp->d_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001619 tmp->d_inode = inode;
1620 tmp->d_flags |= DCACHE_DISCONNECTED;
Al Viroa7cca092014-10-26 19:19:16 -04001621 list_add(&tmp->d_u.d_alias, &inode->i_dentry);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001622 hlist_bl_lock(&tmp->d_sb->s_anon);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001623 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001624 hlist_bl_unlock(&tmp->d_sb->s_anon);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001625 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001626 spin_unlock(&inode->i_lock);
Josef Bacik24ff6662010-11-18 20:52:55 -05001627 security_d_instantiate(tmp, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001628
Christoph Hellwig9308a612008-08-11 15:49:12 +02001629 return tmp;
1630
1631 out_iput:
Josef Bacik24ff6662010-11-18 20:52:55 -05001632 if (res && !IS_ERR(res))
1633 security_d_instantiate(res, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001634 iput(inode);
1635 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001636}
Benny Halevyadc48722009-02-27 14:02:59 -08001637EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639/**
1640 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1641 * @inode: the inode which may have a disconnected dentry
1642 * @dentry: a negative dentry which we want to point to the inode.
1643 *
1644 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1645 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1646 * and return it, else simply d_add the inode to the dentry and return NULL.
1647 *
1648 * This is needed in the lookup routine of any filesystem that is exportable
1649 * (via knfsd) so that we can build dcache paths to directories effectively.
1650 *
1651 * If a dentry was found and moved, then it is returned. Otherwise NULL
1652 * is returned. This matches the expected return value of ->lookup.
1653 *
1654 */
1655struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1656{
1657 struct dentry *new = NULL;
1658
Al Viroa9049372011-07-08 21:20:11 -04001659 if (IS_ERR(inode))
1660 return ERR_CAST(inode);
1661
NeilBrown21c0d8f2006-10-04 02:16:16 -07001662 if (inode && S_ISDIR(inode->i_mode)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001663 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 new = __d_find_alias(inode, 1);
1665 if (new) {
1666 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Piggin873feea2011-01-07 17:50:06 +11001667 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 d_move(new, dentry);
1670 iput(inode);
1671 } else {
Nick Piggin873feea2011-01-07 17:50:06 +11001672 /* already taking inode->i_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001673 __d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001674 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 security_d_instantiate(dentry, inode);
1676 d_rehash(dentry);
1677 }
1678 } else
1679 d_add(dentry, inode);
1680 return new;
1681}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001682EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Barry Naujok94035402008-05-21 16:50:46 +10001684/**
1685 * d_add_ci - lookup or allocate new dentry with case-exact name
1686 * @inode: the inode case-insensitive lookup has found
1687 * @dentry: the negative dentry that was passed to the parent's lookup func
1688 * @name: the case-exact name to be associated with the returned dentry
1689 *
1690 * This is to avoid filling the dcache with case-insensitive names to the
1691 * same inode, only the actual correct case is stored in the dcache for
1692 * case-insensitive filesystems.
1693 *
1694 * For a case-insensitive lookup match and if the the case-exact dentry
1695 * already exists in in the dcache, use it and return it.
1696 *
1697 * If no entry exists with the exact case name, allocate new dentry with
1698 * the exact case, and return the spliced entry.
1699 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001700struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001701 struct qstr *name)
1702{
1703 int error;
1704 struct dentry *found;
1705 struct dentry *new;
1706
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001707 /*
1708 * First check if a dentry matching the name already exists,
1709 * if not go ahead and create it now.
1710 */
Barry Naujok94035402008-05-21 16:50:46 +10001711 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001712 if (!found) {
1713 new = d_alloc(dentry->d_parent, name);
1714 if (!new) {
1715 error = -ENOMEM;
1716 goto err_out;
1717 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001718
Barry Naujok94035402008-05-21 16:50:46 +10001719 found = d_splice_alias(inode, new);
1720 if (found) {
1721 dput(new);
1722 return found;
1723 }
1724 return new;
1725 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001726
1727 /*
1728 * If a matching dentry exists, and it's not negative use it.
1729 *
1730 * Decrement the reference count to balance the iget() done
1731 * earlier on.
1732 */
Barry Naujok94035402008-05-21 16:50:46 +10001733 if (found->d_inode) {
1734 if (unlikely(found->d_inode != inode)) {
1735 /* This can't happen because bad inodes are unhashed. */
1736 BUG_ON(!is_bad_inode(inode));
1737 BUG_ON(!is_bad_inode(found->d_inode));
1738 }
Barry Naujok94035402008-05-21 16:50:46 +10001739 iput(inode);
1740 return found;
1741 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001742
Barry Naujok94035402008-05-21 16:50:46 +10001743 /*
Josef Bacik44396f42011-05-31 11:58:49 -04001744 * We are going to instantiate this dentry, unhash it and clear the
1745 * lookup flag so we can do that.
1746 */
1747 if (unlikely(d_need_lookup(found)))
1748 d_clear_need_lookup(found);
1749
1750 /*
Barry Naujok94035402008-05-21 16:50:46 +10001751 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001752 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001753 */
Al Viro4513d892011-07-17 10:52:14 -04001754 new = d_splice_alias(inode, found);
1755 if (new) {
1756 dput(found);
1757 found = new;
Barry Naujok94035402008-05-21 16:50:46 +10001758 }
Al Viro4513d892011-07-17 10:52:14 -04001759 return found;
Barry Naujok94035402008-05-21 16:50:46 +10001760
1761err_out:
1762 iput(inode);
1763 return ERR_PTR(error);
1764}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001765EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767/**
Nick Piggin31e6b012011-01-07 17:49:52 +11001768 * __d_lookup_rcu - search for a dentry (racy, store-free)
1769 * @parent: parent dentry
1770 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07001771 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11001772 * @inode: returns dentry->d_inode when the inode was found valid.
1773 * Returns: dentry, or NULL
1774 *
1775 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1776 * resolution (store-free path walking) design described in
1777 * Documentation/filesystems/path-lookup.txt.
1778 *
1779 * This is not to be used outside core vfs.
1780 *
1781 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1782 * held, and rcu_read_lock held. The returned dentry must not be stored into
1783 * without taking d_lock and checking d_seq sequence count against @seq
1784 * returned here.
1785 *
1786 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1787 * function.
1788 *
1789 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1790 * the returned dentry, so long as its parent's seqlock is checked after the
1791 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1792 * is formed, giving integrity down the path walk.
1793 */
Linus Torvalds8966be92012-03-02 14:23:30 -08001794struct dentry *__d_lookup_rcu(const struct dentry *parent,
1795 const struct qstr *name,
1796 unsigned *seqp, struct inode **inode)
Nick Piggin31e6b012011-01-07 17:49:52 +11001797{
1798 unsigned int len = name->len;
1799 unsigned int hash = name->hash;
1800 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001801 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001802 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11001803 struct dentry *dentry;
1804
1805 /*
1806 * Note: There is significant duplication with __d_lookup_rcu which is
1807 * required to prevent single threaded performance regressions
1808 * especially on architectures where smp_rmb (in seqcounts) are costly.
1809 * Keep the two functions in sync.
1810 */
1811
1812 /*
1813 * The hash list is protected using RCU.
1814 *
1815 * Carefully use d_seq when comparing a candidate dentry, to avoid
1816 * races with d_move().
1817 *
1818 * It is possible that concurrent renames can mess up our list
1819 * walk here and result in missing our dentry, resulting in the
1820 * false-negative result. d_lookup() protects against concurrent
1821 * renames using rename_lock seqlock.
1822 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09001823 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11001824 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001825 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08001826 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001827 struct inode *i;
1828 const char *tname;
1829 int tlen;
1830
1831 if (dentry->d_name.hash != hash)
1832 continue;
1833
1834seqretry:
Linus Torvalds8966be92012-03-02 14:23:30 -08001835 seq = read_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001836 if (dentry->d_parent != parent)
1837 continue;
1838 if (d_unhashed(dentry))
1839 continue;
1840 tlen = dentry->d_name.len;
1841 tname = dentry->d_name.name;
1842 i = dentry->d_inode;
Nick Piggine1bb5782011-01-07 17:50:08 +11001843 prefetch(tname);
Nick Piggin31e6b012011-01-07 17:49:52 +11001844 /*
1845 * This seqcount check is required to ensure name and
1846 * len are loaded atomically, so as not to walk off the
1847 * edge of memory when walking. If we could load this
1848 * atomically some other way, we could drop this check.
1849 */
Linus Torvalds8966be92012-03-02 14:23:30 -08001850 if (read_seqcount_retry(&dentry->d_seq, seq))
Nick Piggin31e6b012011-01-07 17:49:52 +11001851 goto seqretry;
Linus Torvalds830c0f02011-08-06 22:41:50 -07001852 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001853 if (parent->d_op->d_compare(parent, *inode,
1854 dentry, i,
1855 tlen, tname, name))
1856 continue;
1857 } else {
Nick Piggin9d55c362011-01-07 17:50:09 +11001858 if (dentry_cmp(tname, tlen, str, len))
Nick Piggin31e6b012011-01-07 17:49:52 +11001859 continue;
1860 }
1861 /*
1862 * No extra seqcount check is required after the name
1863 * compare. The caller must perform a seqcount check in
1864 * order to do anything useful with the returned dentry
1865 * anyway.
1866 */
Linus Torvalds8966be92012-03-02 14:23:30 -08001867 *seqp = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001868 *inode = i;
1869 return dentry;
1870 }
1871 return NULL;
1872}
1873
1874/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * d_lookup - search for a dentry
1876 * @parent: parent dentry
1877 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001878 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001880 * d_lookup searches the children of the parent dentry for the name in
1881 * question. If the dentry is found its reference count is incremented and the
1882 * dentry is returned. The caller must use dput to free the entry when it has
1883 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001885struct dentry *d_lookup(struct dentry *parent, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Nick Piggin31e6b012011-01-07 17:49:52 +11001887 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11001888 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 do {
1891 seq = read_seqbegin(&rename_lock);
1892 dentry = __d_lookup(parent, name);
1893 if (dentry)
1894 break;
1895 } while (read_seqretry(&rename_lock, seq));
1896 return dentry;
1897}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001898EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Nick Piggin31e6b012011-01-07 17:49:52 +11001900/**
Nick Pigginb04f7842010-08-18 04:37:34 +10001901 * __d_lookup - search for a dentry (racy)
1902 * @parent: parent dentry
1903 * @name: qstr of name we wish to find
1904 * Returns: dentry, or NULL
1905 *
1906 * __d_lookup is like d_lookup, however it may (rarely) return a
1907 * false-negative result due to unrelated rename activity.
1908 *
1909 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1910 * however it must be used carefully, eg. with a following d_lookup in
1911 * the case of failure.
1912 *
1913 * __d_lookup callers must be commented.
1914 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001915struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 unsigned int len = name->len;
1918 unsigned int hash = name->hash;
1919 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001920 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001921 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11001922 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001923 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Nick Pigginb04f7842010-08-18 04:37:34 +10001925 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11001926 * Note: There is significant duplication with __d_lookup_rcu which is
1927 * required to prevent single threaded performance regressions
1928 * especially on architectures where smp_rmb (in seqcounts) are costly.
1929 * Keep the two functions in sync.
1930 */
1931
1932 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001933 * The hash list is protected using RCU.
1934 *
1935 * Take d_lock when comparing a candidate dentry, to avoid races
1936 * with d_move().
1937 *
1938 * It is possible that concurrent renames can mess up our list
1939 * walk here and result in missing our dentry, resulting in the
1940 * false-negative result. d_lookup() protects against concurrent
1941 * renames using rename_lock seqlock.
1942 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09001943 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10001944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 rcu_read_lock();
1946
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001947 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001948 const char *tname;
1949 int tlen;
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 Piggin31e6b012011-01-07 17:49:52 +11001964 tlen = dentry->d_name.len;
1965 tname = dentry->d_name.name;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001966 if (parent->d_flags & DCACHE_OP_COMPARE) {
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 {
Nick Piggin9d55c362011-01-07 17:50:09 +11001972 if (dentry_cmp(tname, tlen, str, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto next;
1974 }
1975
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001976 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001977 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 spin_unlock(&dentry->d_lock);
1979 break;
1980next:
1981 spin_unlock(&dentry->d_lock);
1982 }
1983 rcu_read_unlock();
1984
1985 return found;
1986}
1987
1988/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001989 * d_hash_and_lookup - hash the qstr then search for a dentry
1990 * @dir: Directory to search in
1991 * @name: qstr of name we wish to find
1992 *
1993 * On hash failure or on lookup failure NULL is returned.
1994 */
1995struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1996{
1997 struct dentry *dentry = NULL;
1998
1999 /*
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) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11002006 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002007 goto out;
2008 }
2009 dentry = d_lookup(dir, name);
2010out:
2011 return dentry;
2012}
2013
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 Viroa7cca092014-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) {
Nick Piggin873feea2011-01-07 17:50:06 +11002078 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002079 spin_unlock(&dentry->d_lock);
2080 cpu_relax();
2081 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 Viroa7cca092014-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 Viroa7cca092014-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 Viroa7cca092014-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 Viroa7cca092014-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;
2356 struct dentry *ret;
2357
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() */
2363 ret = ERR_PTR(-EBUSY);
2364 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2365 goto out_err;
2366 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2367 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2368 goto out_err;
2369 m2 = &alias->d_parent->d_inode->i_mutex;
2370out_unalias:
Al Viro18367502011-07-12 21:42:24 -04002371 __d_move(alias, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002372 ret = alias;
2373out_err:
Nick Piggin873feea2011-01-07 17:50:06 +11002374 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002375 if (m2)
2376 mutex_unlock(m2);
2377 if (m1)
2378 mutex_unlock(m1);
2379 return ret;
2380}
2381
2382/*
David Howells770bfad2006-08-22 20:06:07 -04002383 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2384 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002385 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002386 */
2387static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2388{
2389 struct dentry *dparent, *aparent;
2390
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002391 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002392
Nick Piggin31e6b012011-01-07 17:49:52 +11002393 write_seqcount_begin(&dentry->d_seq);
2394 write_seqcount_begin(&anon->d_seq);
2395
David Howells770bfad2006-08-22 20:06:07 -04002396 dparent = dentry->d_parent;
2397 aparent = anon->d_parent;
2398
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002399 switch_names(dentry, anon);
2400 swap(dentry->d_name.hash, anon->d_name.hash);
2401
David Howells770bfad2006-08-22 20:06:07 -04002402 dentry->d_parent = (aparent == anon) ? dentry : aparent;
Al Viroa7cca092014-10-26 19:19:16 -04002403 list_del(&dentry->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002404 if (!IS_ROOT(dentry))
Al Viroa7cca092014-10-26 19:19:16 -04002405 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002406 else
Al Viroa7cca092014-10-26 19:19:16 -04002407 INIT_LIST_HEAD(&dentry->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002408
2409 anon->d_parent = (dparent == dentry) ? anon : dparent;
Al Viroa7cca092014-10-26 19:19:16 -04002410 list_del(&anon->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002411 if (!IS_ROOT(anon))
Al Viroa7cca092014-10-26 19:19:16 -04002412 list_add(&anon->d_child, &anon->d_parent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002413 else
Al Viroa7cca092014-10-26 19:19:16 -04002414 INIT_LIST_HEAD(&anon->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002415
Nick Piggin31e6b012011-01-07 17:49:52 +11002416 write_seqcount_end(&dentry->d_seq);
2417 write_seqcount_end(&anon->d_seq);
2418
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002419 dentry_unlock_parents_for_move(anon, dentry);
2420 spin_unlock(&dentry->d_lock);
2421
2422 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002423 anon->d_flags &= ~DCACHE_DISCONNECTED;
2424}
2425
2426/**
2427 * d_materialise_unique - introduce an inode into the tree
2428 * @dentry: candidate dentry
2429 * @inode: inode to bind to the dentry, to which aliases may be attached
2430 *
2431 * Introduces an dentry into the tree, substituting an extant disconnected
Jeff Laytonc46c8872011-07-26 13:33:16 -04002432 * root directory alias in its place if there is one. Caller must hold the
2433 * i_mutex of the parent directory.
David Howells770bfad2006-08-22 20:06:07 -04002434 */
2435struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2436{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002437 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002438
2439 BUG_ON(!d_unhashed(dentry));
2440
David Howells770bfad2006-08-22 20:06:07 -04002441 if (!inode) {
2442 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002443 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002444 d_rehash(actual);
2445 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002446 }
2447
Nick Piggin873feea2011-01-07 17:50:06 +11002448 spin_lock(&inode->i_lock);
Nick Piggin357f8e62011-01-07 17:49:42 +11002449
Trond Myklebust9eaef272006-10-21 10:24:20 -07002450 if (S_ISDIR(inode->i_mode)) {
2451 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002452
Trond Myklebust9eaef272006-10-21 10:24:20 -07002453 /* Does an aliased dentry already exist? */
2454 alias = __d_find_alias(inode, 0);
2455 if (alias) {
2456 actual = alias;
Al Viro18367502011-07-12 21:42:24 -04002457 write_seqlock(&rename_lock);
2458
2459 if (d_ancestor(alias, dentry)) {
2460 /* Check for loops */
2461 actual = ERR_PTR(-ELOOP);
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002462 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04002463 } else if (IS_ROOT(alias)) {
2464 /* Is this an anonymous mountpoint that we
2465 * could splice into our tree? */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002466 __d_materialise_dentry(dentry, alias);
Al Viro18367502011-07-12 21:42:24 -04002467 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002468 __d_drop(alias);
2469 goto found;
Al Viro18367502011-07-12 21:42:24 -04002470 } else {
2471 /* Nope, but we must(!) avoid directory
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002472 * aliasing. This drops inode->i_lock */
Al Viro18367502011-07-12 21:42:24 -04002473 actual = __d_unalias(inode, dentry, alias);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002474 }
Al Viro18367502011-07-12 21:42:24 -04002475 write_sequnlock(&rename_lock);
David Howellsdd179942011-08-16 15:31:30 +01002476 if (IS_ERR(actual)) {
2477 if (PTR_ERR(actual) == -ELOOP)
2478 pr_warn_ratelimited(
2479 "VFS: Lookup of '%s' in %s %s"
2480 " would have caused loop\n",
2481 dentry->d_name.name,
2482 inode->i_sb->s_type->name,
2483 inode->i_sb->s_id);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002484 dput(alias);
David Howellsdd179942011-08-16 15:31:30 +01002485 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002486 goto out_nolock;
2487 }
David Howells770bfad2006-08-22 20:06:07 -04002488 }
2489
2490 /* Add a unique reference */
2491 actual = __d_instantiate_unique(dentry, inode);
2492 if (!actual)
2493 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002494 else
2495 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002496
David Howells770bfad2006-08-22 20:06:07 -04002497 spin_lock(&actual->d_lock);
2498found:
2499 _d_rehash(actual);
2500 spin_unlock(&actual->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002501 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002502out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002503 if (actual == dentry) {
2504 security_d_instantiate(dentry, inode);
2505 return NULL;
2506 }
2507
2508 iput(inode);
2509 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002510}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002511EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002512
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002513static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002514{
2515 *buflen -= namelen;
2516 if (*buflen < 0)
2517 return -ENAMETOOLONG;
2518 *buffer -= namelen;
2519 memcpy(*buffer, str, namelen);
2520 return 0;
2521}
2522
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002523static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2524{
2525 return prepend(buffer, buflen, name->name, name->len);
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528/**
Randy Dunlap208898c2010-11-18 15:02:49 -08002529 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002530 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002531 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002532 * @buffer: pointer to the end of the buffer
2533 * @buflen: pointer to buffer length
2534 *
Nick Piggin949854d2011-01-07 17:49:37 +11002535 * Caller holds the rename_lock.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002536 */
Al Viro02125a82011-12-05 08:43:34 -05002537static int prepend_path(const struct path *path,
2538 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002539 char **buffer, int *buflen)
2540{
2541 struct dentry *dentry = path->dentry;
2542 struct vfsmount *vfsmnt = path->mnt;
Al Viro0714a532011-11-24 22:19:58 -05002543 struct mount *mnt = real_mount(vfsmnt);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002544 bool slash = false;
2545 int error = 0;
2546
Nick Piggin99b7db72010-08-18 04:37:39 +10002547 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002548 while (dentry != root->dentry || vfsmnt != root->mnt) {
2549 struct dentry * parent;
2550
2551 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2552 /* Global root? */
Al Viro676da582011-11-24 21:47:05 -05002553 if (!mnt_has_parent(mnt))
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002554 goto global_root;
Al Viroa73324d2011-11-24 22:25:07 -05002555 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002556 mnt = mnt->mnt_parent;
2557 vfsmnt = &mnt->mnt;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002558 continue;
2559 }
2560 parent = dentry->d_parent;
2561 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002562 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002563 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002564 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002565 if (!error)
2566 error = prepend(buffer, buflen, "/", 1);
2567 if (error)
2568 break;
2569
2570 slash = true;
2571 dentry = parent;
2572 }
2573
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002574 if (!error && !slash)
2575 error = prepend(buffer, buflen, "/", 1);
2576
Al Viro02125a82011-12-05 08:43:34 -05002577out:
Nick Piggin99b7db72010-08-18 04:37:39 +10002578 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002579 return error;
2580
2581global_root:
2582 /*
2583 * Filesystems needing to implement special "root names"
2584 * should do so with ->d_dname()
2585 */
2586 if (IS_ROOT(dentry) &&
2587 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2588 WARN(1, "Root dentry has weird name <%.*s>\n",
2589 (int) dentry->d_name.len, dentry->d_name.name);
2590 }
Al Viro02125a82011-12-05 08:43:34 -05002591 if (!slash)
2592 error = prepend(buffer, buflen, "/", 1);
2593 if (!error)
Al Viro143c8c92011-11-25 00:46:35 -05002594 error = real_mount(vfsmnt)->mnt_ns ? 1 : 2;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002595 goto out;
2596}
2597
2598/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002599 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002600 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002601 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07002602 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 * @buflen: buffer length
2604 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002605 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002607 * Returns a pointer into the buffer or an error code if the
2608 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002609 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002610 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002611 *
Al Viro02125a82011-12-05 08:43:34 -05002612 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 */
Al Viro02125a82011-12-05 08:43:34 -05002614char *__d_path(const struct path *path,
2615 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002616 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002618 char *res = buf + buflen;
2619 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002621 prepend(&res, &buflen, "\0", 1);
Nick Piggin949854d2011-01-07 17:49:37 +11002622 write_seqlock(&rename_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002623 error = prepend_path(path, root, &res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002624 write_sequnlock(&rename_lock);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002625
Al Viro02125a82011-12-05 08:43:34 -05002626 if (error < 0)
2627 return ERR_PTR(error);
2628 if (error > 0)
2629 return NULL;
2630 return res;
2631}
2632
2633char *d_absolute_path(const struct path *path,
2634 char *buf, int buflen)
2635{
2636 struct path root = {};
2637 char *res = buf + buflen;
2638 int error;
2639
2640 prepend(&res, &buflen, "\0", 1);
2641 write_seqlock(&rename_lock);
2642 error = prepend_path(path, &root, &res, &buflen);
2643 write_sequnlock(&rename_lock);
2644
2645 if (error > 1)
2646 error = -EINVAL;
2647 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002648 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002649 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002652/*
2653 * same as __d_path but appends "(deleted)" for unlinked files.
2654 */
Al Viro02125a82011-12-05 08:43:34 -05002655static int path_with_deleted(const struct path *path,
2656 const struct path *root,
2657 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002658{
2659 prepend(buf, buflen, "\0", 1);
2660 if (d_unlinked(path->dentry)) {
2661 int error = prepend(buf, buflen, " (deleted)", 10);
2662 if (error)
2663 return error;
2664 }
2665
2666 return prepend_path(path, root, buf, buflen);
2667}
2668
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002669static int prepend_unreachable(char **buffer, int *buflen)
2670{
2671 return prepend(buffer, buflen, "(unreachable)", 13);
2672}
2673
Jan Bluncka03a8a702008-02-14 19:38:32 -08002674/**
2675 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002676 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002677 * @buf: buffer to return value in
2678 * @buflen: buffer length
2679 *
2680 * Convert a dentry into an ASCII path name. If the entry has been deleted
2681 * the string " (deleted)" is appended. Note that this is ambiguous.
2682 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002683 * Returns a pointer into the buffer or an error code if the path was
2684 * too long. Note: Callers should use the returned pointer, not the passed
2685 * in buffer, to use the name! The implementation often starts at an offset
2686 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002687 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002688 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002689 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002690char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002692 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002693 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002694 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002696 /*
2697 * We have various synthetic filesystems that never get mounted. On
2698 * these filesystems dentries are never used for lookup purposes, and
2699 * thus don't need to be hashed. They also don't need a name until a
2700 * user wants to identify the object in /proc/pid/fd/. The little hack
2701 * below allows us to generate a name for these objects on demand:
2702 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002703 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2704 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002705
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002706 get_fs_root(current->fs, &root);
Nick Piggin949854d2011-01-07 17:49:37 +11002707 write_seqlock(&rename_lock);
Al Viro02125a82011-12-05 08:43:34 -05002708 error = path_with_deleted(path, &root, &res, &buflen);
2709 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002710 res = ERR_PTR(error);
Nick Piggin949854d2011-01-07 17:49:37 +11002711 write_sequnlock(&rename_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002712 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return res;
2714}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002715EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002717/**
2718 * d_path_with_unreachable - return the path of a dentry
2719 * @path: path to report
2720 * @buf: buffer to return value in
2721 * @buflen: buffer length
2722 *
2723 * The difference from d_path() is that this prepends "(unreachable)"
2724 * to paths which are unreachable from the current process' root.
2725 */
2726char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2727{
2728 char *res = buf + buflen;
2729 struct path root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002730 int error;
2731
2732 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2733 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2734
2735 get_fs_root(current->fs, &root);
Nick Piggin949854d2011-01-07 17:49:37 +11002736 write_seqlock(&rename_lock);
Al Viro02125a82011-12-05 08:43:34 -05002737 error = path_with_deleted(path, &root, &res, &buflen);
2738 if (error > 0)
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002739 error = prepend_unreachable(&res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002740 write_sequnlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002741 path_put(&root);
2742 if (error)
2743 res = ERR_PTR(error);
2744
2745 return res;
2746}
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002749 * Helper function for dentry_operations.d_dname() members
2750 */
2751char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2752 const char *fmt, ...)
2753{
2754 va_list args;
2755 char temp[64];
2756 int sz;
2757
2758 va_start(args, fmt);
2759 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2760 va_end(args);
2761
2762 if (sz > sizeof(temp) || sz > buflen)
2763 return ERR_PTR(-ENAMETOOLONG);
2764
2765 buffer += buflen - sz;
2766 return memcpy(buffer, temp, sz);
2767}
2768
2769/*
Ram Pai6092d042008-03-27 13:06:20 +01002770 * Write full pathname from the root of the filesystem into the buffer.
2771 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002772static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002773{
2774 char *end = buf + buflen;
2775 char *retval;
2776
Ram Pai6092d042008-03-27 13:06:20 +01002777 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002778 if (buflen < 1)
2779 goto Elong;
2780 /* Get '/' right */
2781 retval = end-1;
2782 *retval = '/';
2783
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002784 while (!IS_ROOT(dentry)) {
2785 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002786 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002787
Ram Pai6092d042008-03-27 13:06:20 +01002788 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002789 spin_lock(&dentry->d_lock);
2790 error = prepend_name(&end, &buflen, &dentry->d_name);
2791 spin_unlock(&dentry->d_lock);
2792 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002793 goto Elong;
2794
2795 retval = end;
2796 dentry = parent;
2797 }
Al Viroc1031352010-06-06 22:31:14 -04002798 return retval;
2799Elong:
2800 return ERR_PTR(-ENAMETOOLONG);
2801}
Nick Pigginec2447c2011-01-07 17:49:29 +11002802
2803char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2804{
2805 char *retval;
2806
Nick Piggin949854d2011-01-07 17:49:37 +11002807 write_seqlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002808 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002809 write_sequnlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002810
2811 return retval;
2812}
2813EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002814
2815char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2816{
2817 char *p = NULL;
2818 char *retval;
2819
Nick Piggin949854d2011-01-07 17:49:37 +11002820 write_seqlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002821 if (d_unlinked(dentry)) {
2822 p = buf + buflen;
2823 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2824 goto Elong;
2825 buflen++;
2826 }
2827 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002828 write_sequnlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002829 if (!IS_ERR(retval) && p)
2830 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002831 return retval;
2832Elong:
Ram Pai6092d042008-03-27 13:06:20 +01002833 return ERR_PTR(-ENAMETOOLONG);
2834}
2835
2836/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 * NOTE! The user-level library version returns a
2838 * character pointer. The kernel system call just
2839 * returns the length of the buffer filled (which
2840 * includes the ending '\0' character), or a negative
2841 * error value. So libc would do something like
2842 *
2843 * char *getcwd(char * buf, size_t size)
2844 * {
2845 * int retval;
2846 *
2847 * retval = sys_getcwd(buf, size);
2848 * if (retval >= 0)
2849 * return buf;
2850 * errno = -retval;
2851 * return NULL;
2852 * }
2853 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002854SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Linus Torvalds552ce542007-02-13 12:08:18 -08002856 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002857 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002858 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 if (!page)
2861 return -ENOMEM;
2862
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002863 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Linus Torvalds552ce542007-02-13 12:08:18 -08002865 error = -ENOENT;
Nick Piggin949854d2011-01-07 17:49:37 +11002866 write_seqlock(&rename_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002867 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002868 unsigned long len;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002869 char *cwd = page + PAGE_SIZE;
2870 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002872 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05002873 error = prepend_path(&pwd, &root, &cwd, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002874 write_sequnlock(&rename_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002875
Al Viro02125a82011-12-05 08:43:34 -05002876 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08002877 goto out;
2878
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002879 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05002880 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002881 error = prepend_unreachable(&cwd, &buflen);
2882 if (error)
2883 goto out;
2884 }
2885
Linus Torvalds552ce542007-02-13 12:08:18 -08002886 error = -ERANGE;
2887 len = PAGE_SIZE + page - cwd;
2888 if (len <= size) {
2889 error = len;
2890 if (copy_to_user(buf, cwd, len))
2891 error = -EFAULT;
2892 }
Nick Piggin949854d2011-01-07 17:49:37 +11002893 } else {
2894 write_sequnlock(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002898 path_put(&pwd);
2899 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 free_page((unsigned long) page);
2901 return error;
2902}
2903
2904/*
2905 * Test whether new_dentry is a subdirectory of old_dentry.
2906 *
2907 * Trivially implemented using the dcache structure
2908 */
2909
2910/**
2911 * is_subdir - is new dentry a subdirectory of old_dentry
2912 * @new_dentry: new dentry
2913 * @old_dentry: old dentry
2914 *
2915 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2916 * Returns 0 otherwise.
2917 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2918 */
2919
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002920int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11002923 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002925 if (new_dentry == old_dentry)
2926 return 1;
2927
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002928 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002931 /*
2932 * Need rcu_readlock to protect against the d_parent trashing
2933 * due to d_move
2934 */
2935 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002936 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002938 else
2939 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11002940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 return result;
2944}
2945
2946void d_genocide(struct dentry *root)
2947{
Nick Piggin949854d2011-01-07 17:49:37 +11002948 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11002950 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11002951 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Nick Piggin949854d2011-01-07 17:49:37 +11002953 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11002954again:
2955 this_parent = root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002956 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957repeat:
2958 next = this_parent->d_subdirs.next;
2959resume:
2960 while (next != &this_parent->d_subdirs) {
2961 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04002962 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 next = tmp->next;
Nick Piggin949854d2011-01-07 17:49:37 +11002964
Nick Pigginda502952011-01-07 17:49:33 +11002965 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2966 if (d_unhashed(dentry) || !dentry->d_inode) {
2967 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002971 spin_unlock(&this_parent->d_lock);
2972 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002974 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 goto repeat;
2976 }
Nick Piggin949854d2011-01-07 17:49:37 +11002977 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2978 dentry->d_flags |= DCACHE_GENOCIDE;
2979 dentry->d_count--;
2980 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002981 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983 if (this_parent != root) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07002984 struct dentry *child = this_parent;
Nick Piggin949854d2011-01-07 17:49:37 +11002985 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2986 this_parent->d_flags |= DCACHE_GENOCIDE;
2987 this_parent->d_count--;
2988 }
Linus Torvaldsc826cb72011-03-15 15:29:21 -07002989 this_parent = try_to_ascend(this_parent, locked, seq);
2990 if (!this_parent)
Nick Piggin949854d2011-01-07 17:49:37 +11002991 goto rename_retry;
Al Viroa7cca092014-10-26 19:19:16 -04002992 next = child->d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 goto resume;
2994 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002995 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11002996 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11002997 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11002998 if (locked)
2999 write_sequnlock(&rename_lock);
3000 return;
3001
3002rename_retry:
3003 locked = 1;
3004 write_seqlock(&rename_lock);
3005 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
3008/**
3009 * find_inode_number - check for dentry with name
3010 * @dir: directory to check
3011 * @name: Name to find.
3012 *
3013 * Check whether a dentry already exists for the given name,
3014 * and return the inode number if it has an inode. Otherwise
3015 * 0 is returned.
3016 *
3017 * This routine is used to post-process directory listings for
3018 * filesystems using synthetic inode numbers, and is necessary
3019 * to keep getcwd() working.
3020 */
3021
3022ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3023{
3024 struct dentry * dentry;
3025 ino_t ino = 0;
3026
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08003027 dentry = d_hash_and_lookup(dir, name);
3028 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (dentry->d_inode)
3030 ino = dentry->d_inode->i_ino;
3031 dput(dentry);
3032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return ino;
3034}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003035EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037static __initdata unsigned long dhash_entries;
3038static int __init set_dhash_entries(char *str)
3039{
3040 if (!str)
3041 return 0;
3042 dhash_entries = simple_strtoul(str, &str, 0);
3043 return 1;
3044}
3045__setup("dhash_entries=", set_dhash_entries);
3046
3047static void __init dcache_init_early(void)
3048{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003049 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 /* If hashes are distributed across NUMA nodes, defer
3052 * hash allocation until vmalloc space is available.
3053 */
3054 if (hashdist)
3055 return;
3056
3057 dentry_hashtable =
3058 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003059 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 dhash_entries,
3061 13,
3062 HASH_EARLY,
3063 &d_hash_shift,
3064 &d_hash_mask,
3065 0);
3066
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003067 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003068 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
Denis Cheng74bf17c2007-10-16 23:26:30 -07003071static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003073 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 /*
3076 * A constructor could be added for stable state like the lists,
3077 * but it is probably not worth it because of the cache nature
3078 * of the dcache.
3079 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003080 dentry_cache = KMEM_CACHE(dentry,
3081 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 /* Hash may have been set up in dcache_init_early */
3084 if (!hashdist)
3085 return;
3086
3087 dentry_hashtable =
3088 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003089 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 dhash_entries,
3091 13,
3092 0,
3093 &d_hash_shift,
3094 &d_hash_mask,
3095 0);
3096
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003097 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003098 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
3101/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003102struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003103EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105EXPORT_SYMBOL(d_genocide);
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107void __init vfs_caches_init_early(void)
3108{
3109 dcache_init_early();
3110 inode_init_early();
3111}
3112
3113void __init vfs_caches_init(unsigned long mempages)
3114{
3115 unsigned long reserve;
3116
3117 /* Base hash sizes on available memory, with a reserve equal to
3118 150% of current kernel size */
3119
3120 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3121 mempages -= reserve;
3122
3123 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003124 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Denis Cheng74bf17c2007-10-16 23:26:30 -07003126 dcache_init();
3127 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07003129 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 bdev_cache_init();
3131 chrdev_init();
3132}
gregkh@linuxfoundation.orgeba65b02017-08-04 16:16:18 -07003133