blob: 67a137772f1367a57fa6cf0a2369e563986ae817 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -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
17#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/fsnotify.h>
22#include <linux/slab.h>
23#include <linux/init.h>
24#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/export.h>
27#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>
34#include <linux/fs_struct.h>
35#include <linux/hardirq.h>
36#include <linux/bit_spinlock.h>
37#include <linux/rculist_bl.h>
38#include <linux/prefetch.h>
39#include <linux/ratelimit.h>
40#include <linux/delay.h>
41#include "internal.h"
42#include "mount.h"
43
44/*
45 * Usage:
46 * dcache->d_inode->i_lock protects:
47 * - i_dentry, d_u.d_alias, d_inode of aliases
48 * dcache_hash_bucket lock protects:
49 * - the dcache hash table
50 * s_anon bl list spinlock protects:
51 * - the s_anon list (see __d_drop)
52 * dcache_lru_lock protects:
53 * - the dcache lru lists and counters
54 * d_lock protects:
55 * - d_flags
56 * - d_name
57 * - d_lru
58 * - d_count
59 * - d_unhashed()
60 * - d_parent and d_subdirs
61 * - childrens' d_child and d_parent
62 * - d_u.d_alias, d_inode
63 *
64 * Ordering:
65 * dentry->d_inode->i_lock
66 * dentry->d_lock
67 * dcache_lru_lock
68 * dcache_hash_bucket lock
69 * s_anon lock
70 *
71 * If there is an ancestor relationship:
72 * dentry->d_parent->...->d_parent->d_lock
73 * ...
74 * dentry->d_parent->d_lock
75 * dentry->d_lock
76 *
77 * If no ancestor relationship:
78 * if (dentry1 < dentry2)
79 * dentry1->d_lock
80 * dentry2->d_lock
81 */
82int sysctl_vfs_cache_pressure __read_mostly = 100;
83EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
84
85static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
86__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
87
88EXPORT_SYMBOL(rename_lock);
89
90static struct kmem_cache *dentry_cache __read_mostly;
91
92/*
93 * This is the single most critical data structure when it comes
94 * to the dcache: the hashtable for lookups. Somebody should try
95 * to make this good - I've just made it work.
96 *
97 * This hash-function tries to avoid losing too many bits of hash
98 * information, yet avoid using a prime hash-size or similar.
99 */
100#define D_HASHBITS d_hash_shift
101#define D_HASHMASK d_hash_mask
102
103static unsigned int d_hash_mask __read_mostly;
104static unsigned int d_hash_shift __read_mostly;
105
106static struct hlist_bl_head *dentry_hashtable __read_mostly;
107
108static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
109 unsigned int hash)
110{
111 hash += (unsigned long) parent / L1_CACHE_BYTES;
112 hash = hash + (hash >> D_HASHBITS);
113 return dentry_hashtable + (hash & D_HASHMASK);
114}
115
116/* Statistics gathering. */
117struct dentry_stat_t dentry_stat = {
118 .age_limit = 45,
119};
120
121static DEFINE_PER_CPU(unsigned int, nr_dentry);
122
123#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
124static int get_nr_dentry(void)
125{
126 int i;
127 int sum = 0;
128 for_each_possible_cpu(i)
129 sum += per_cpu(nr_dentry, i);
130 return sum < 0 ? 0 : sum;
131}
132
133int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
134 size_t *lenp, loff_t *ppos)
135{
136 dentry_stat.nr_dentry = get_nr_dentry();
137 return proc_dointvec(table, write, buffer, lenp, ppos);
138}
139#endif
140
141/*
142 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
143 * The strings are both count bytes long, and count is non-zero.
144 */
145#ifdef CONFIG_DCACHE_WORD_ACCESS
146
147#include <asm/word-at-a-time.h>
148/*
149 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
150 * aligned allocation for this particular component. We don't
151 * strictly need the load_unaligned_zeropad() safety, but it
152 * doesn't hurt either.
153 *
154 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
155 * need the careful unaligned handling.
156 */
157static inline int dentry_cmp(const unsigned char *cs, size_t scount,
158 const unsigned char *ct, size_t tcount)
159{
160 unsigned long a,b,mask;
161
162 if (unlikely(scount != tcount))
163 return 1;
164
165 for (;;) {
166 a = load_unaligned_zeropad(cs);
167 b = load_unaligned_zeropad(ct);
168 if (tcount < sizeof(unsigned long))
169 break;
170 if (unlikely(a != b))
171 return 1;
172 cs += sizeof(unsigned long);
173 ct += sizeof(unsigned long);
174 tcount -= sizeof(unsigned long);
175 if (!tcount)
176 return 0;
177 }
178 mask = ~(~0ul << tcount*8);
179 return unlikely(!!((a ^ b) & mask));
180}
181
182#else
183
184static inline int dentry_cmp(const unsigned char *cs, size_t scount,
185 const unsigned char *ct, size_t tcount)
186{
187 if (scount != tcount)
188 return 1;
189
190 do {
191 if (*cs != *ct)
192 return 1;
193 cs++;
194 ct++;
195 tcount--;
196 } while (tcount);
197 return 0;
198}
199
200#endif
201
202static void __d_free(struct rcu_head *head)
203{
204 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
205
206 if (dname_external(dentry))
207 kfree(dentry->d_name.name);
208 kmem_cache_free(dentry_cache, dentry);
209}
210
211/*
212 * no locks, please.
213 */
214static void d_free(struct dentry *dentry)
215{
216 WARN_ON(!list_empty(&dentry->d_u.d_alias));
217 BUG_ON(dentry->d_count);
218 this_cpu_dec(nr_dentry);
219 if (dentry->d_op && dentry->d_op->d_release)
220 dentry->d_op->d_release(dentry);
221
222 /* if dentry was never visible to RCU, immediate free is OK */
223 if (!(dentry->d_flags & DCACHE_RCUACCESS))
224 __d_free(&dentry->d_u.d_rcu);
225 else
226 call_rcu(&dentry->d_u.d_rcu, __d_free);
227}
228
229/**
230 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
231 * @dentry: the target dentry
232 * After this call, in-progress rcu-walk path lookup will fail. This
233 * should be called after unhashing, and after changing d_inode (if
234 * the dentry has not already been unhashed).
235 */
236static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
237{
238 assert_spin_locked(&dentry->d_lock);
239 /* Go through a barrier */
240 write_seqcount_barrier(&dentry->d_seq);
241}
242
243/*
244 * Release the dentry's inode, using the filesystem
245 * d_iput() operation if defined. Dentry has no refcount
246 * and is unhashed.
247 */
248static void dentry_iput(struct dentry * dentry)
249 __releases(dentry->d_lock)
250 __releases(dentry->d_inode->i_lock)
251{
252 struct inode *inode = dentry->d_inode;
253 if (inode) {
254 dentry->d_inode = NULL;
255 list_del_init(&dentry->d_u.d_alias);
256 spin_unlock(&dentry->d_lock);
257 spin_unlock(&inode->i_lock);
258 if (!inode->i_nlink)
259 fsnotify_inoderemove(inode);
260 if (dentry->d_op && dentry->d_op->d_iput)
261 dentry->d_op->d_iput(dentry, inode);
262 else
263 iput(inode);
264 } else {
265 spin_unlock(&dentry->d_lock);
266 }
267}
268
269/*
270 * Release the dentry's inode, using the filesystem
271 * d_iput() operation if defined. dentry remains in-use.
272 */
273static void dentry_unlink_inode(struct dentry * dentry)
274 __releases(dentry->d_lock)
275 __releases(dentry->d_inode->i_lock)
276{
277 struct inode *inode = dentry->d_inode;
278 dentry->d_inode = NULL;
279 list_del_init(&dentry->d_u.d_alias);
280 dentry_rcuwalk_barrier(dentry);
281 spin_unlock(&dentry->d_lock);
282 spin_unlock(&inode->i_lock);
283 if (!inode->i_nlink)
284 fsnotify_inoderemove(inode);
285 if (dentry->d_op && dentry->d_op->d_iput)
286 dentry->d_op->d_iput(dentry, inode);
287 else
288 iput(inode);
289}
290
291/*
292 * dentry_lru_(add|del|prune|move_tail) must be called with d_lock held.
293 */
294static void dentry_lru_add(struct dentry *dentry)
295{
296 if (list_empty(&dentry->d_lru)) {
297 spin_lock(&dcache_lru_lock);
298 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
299 dentry->d_sb->s_nr_dentry_unused++;
300 dentry_stat.nr_unused++;
301 spin_unlock(&dcache_lru_lock);
302 }
303}
304
305static void __dentry_lru_del(struct dentry *dentry)
306{
307 list_del_init(&dentry->d_lru);
308 dentry->d_flags &= ~DCACHE_SHRINK_LIST;
309 dentry->d_sb->s_nr_dentry_unused--;
310 dentry_stat.nr_unused--;
311}
312
313/*
314 * Remove a dentry with references from the LRU.
315 */
316static void dentry_lru_del(struct dentry *dentry)
317{
318 if (!list_empty(&dentry->d_lru)) {
319 spin_lock(&dcache_lru_lock);
320 __dentry_lru_del(dentry);
321 spin_unlock(&dcache_lru_lock);
322 }
323}
324
325/*
326 * Remove a dentry that is unreferenced and about to be pruned
327 * (unhashed and destroyed) from the LRU, and inform the file system.
328 * This wrapper should be called _prior_ to unhashing a victim dentry.
329 */
330static void dentry_lru_prune(struct dentry *dentry)
331{
332 if (!list_empty(&dentry->d_lru)) {
333 if (dentry->d_flags & DCACHE_OP_PRUNE)
334 dentry->d_op->d_prune(dentry);
335
336 spin_lock(&dcache_lru_lock);
337 __dentry_lru_del(dentry);
338 spin_unlock(&dcache_lru_lock);
339 }
340}
341
342static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
343{
344 spin_lock(&dcache_lru_lock);
345 if (list_empty(&dentry->d_lru)) {
346 list_add_tail(&dentry->d_lru, list);
347 dentry->d_sb->s_nr_dentry_unused++;
348 dentry_stat.nr_unused++;
349 } else {
350 list_move_tail(&dentry->d_lru, list);
351 }
352 spin_unlock(&dcache_lru_lock);
353}
354
355/**
356 * d_kill - kill dentry and return parent
357 * @dentry: dentry to kill
358 * @parent: parent dentry
359 *
360 * The dentry must already be unhashed and removed from the LRU.
361 *
362 * If this is the root of the dentry tree, return NULL.
363 *
364 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
365 * d_kill.
366 */
367static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
368 __releases(dentry->d_lock)
369 __releases(parent->d_lock)
370 __releases(dentry->d_inode->i_lock)
371{
372 __list_del_entry(&dentry->d_child);
373 /*
374 * Inform ascending readers that we are no longer attached to the
375 * dentry tree
376 */
377 dentry->d_flags |= DCACHE_DENTRY_KILLED;
378 if (parent)
379 spin_unlock(&parent->d_lock);
380 dentry_iput(dentry);
381 /*
382 * dentry_iput drops the locks, at which point nobody (except
383 * transient RCU lookups) can reach this dentry.
384 */
385 d_free(dentry);
386 return parent;
387}
388
389/*
390 * Unhash a dentry without inserting an RCU walk barrier or checking that
391 * dentry->d_lock is locked. The caller must take care of that, if
392 * appropriate.
393 */
394static void __d_shrink(struct dentry *dentry)
395{
396 if (!d_unhashed(dentry)) {
397 struct hlist_bl_head *b;
398 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
399 b = &dentry->d_sb->s_anon;
400 else
401 b = d_hash(dentry->d_parent, dentry->d_name.hash);
402
403 hlist_bl_lock(b);
404 __hlist_bl_del(&dentry->d_hash);
405 dentry->d_hash.pprev = NULL;
406 hlist_bl_unlock(b);
407 }
408}
409
410/**
411 * d_drop - drop a dentry
412 * @dentry: dentry to drop
413 *
414 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
415 * be found through a VFS lookup any more. Note that this is different from
416 * deleting the dentry - d_delete will try to mark the dentry negative if
417 * possible, giving a successful _negative_ lookup, while d_drop will
418 * just make the cache lookup fail.
419 *
420 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
421 * reason (NFS timeouts or autofs deletes).
422 *
423 * __d_drop requires dentry->d_lock.
424 */
425void __d_drop(struct dentry *dentry)
426{
427 if (!d_unhashed(dentry)) {
428 __d_shrink(dentry);
429 dentry_rcuwalk_barrier(dentry);
430 }
431}
432EXPORT_SYMBOL(__d_drop);
433
434void d_drop(struct dentry *dentry)
435{
436 spin_lock(&dentry->d_lock);
437 __d_drop(dentry);
438 spin_unlock(&dentry->d_lock);
439}
440EXPORT_SYMBOL(d_drop);
441
442/*
443 * d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag
444 * @dentry: dentry to drop
445 *
446 * This is called when we do a lookup on a placeholder dentry that needed to be
447 * looked up. The dentry should have been hashed in order for it to be found by
448 * the lookup code, but now needs to be unhashed while we do the actual lookup
449 * and clear the DCACHE_NEED_LOOKUP flag.
450 */
451void d_clear_need_lookup(struct dentry *dentry)
452{
453 spin_lock(&dentry->d_lock);
454 __d_drop(dentry);
455 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
456 spin_unlock(&dentry->d_lock);
457}
458EXPORT_SYMBOL(d_clear_need_lookup);
459
460/*
461 * Finish off a dentry we've decided to kill.
462 * dentry->d_lock must be held, returns with it unlocked.
463 * If ref is non-zero, then decrement the refcount too.
464 * Returns dentry requiring refcount drop, or NULL if we're done.
465 */
466static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
467 __releases(dentry->d_lock)
468{
469 struct inode *inode;
470 struct dentry *parent;
471
472 inode = dentry->d_inode;
473 if (inode && !spin_trylock(&inode->i_lock)) {
474relock:
475 spin_unlock(&dentry->d_lock);
476 cpu_chill();
477 return dentry; /* try again with same dentry */
478 }
479 if (IS_ROOT(dentry))
480 parent = NULL;
481 else
482 parent = dentry->d_parent;
483 if (parent && !spin_trylock(&parent->d_lock)) {
484 if (inode)
485 spin_unlock(&inode->i_lock);
486 goto relock;
487 }
488
489 if (ref)
490 dentry->d_count--;
491 /*
492 * if dentry was on the d_lru list delete it from there.
493 * inform the fs via d_prune that this dentry is about to be
494 * unhashed and destroyed.
495 */
496 dentry_lru_prune(dentry);
497 /* if it was on the hash then remove it */
498 __d_drop(dentry);
499 return d_kill(dentry, parent);
500}
501
502/*
503 * This is dput
504 *
505 * This is complicated by the fact that we do not want to put
506 * dentries that are no longer on any hash chain on the unused
507 * list: we'd much rather just get rid of them immediately.
508 *
509 * However, that implies that we have to traverse the dentry
510 * tree upwards to the parents which might _also_ now be
511 * scheduled for deletion (it may have been only waiting for
512 * its last child to go away).
513 *
514 * This tail recursion is done by hand as we don't want to depend
515 * on the compiler to always get this right (gcc generally doesn't).
516 * Real recursion would eat up our stack space.
517 */
518
519/*
520 * dput - release a dentry
521 * @dentry: dentry to release
522 *
523 * Release a dentry. This will drop the usage count and if appropriate
524 * call the dentry unlink method as well as removing it from the queues and
525 * releasing its resources. If the parent dentries were scheduled for release
526 * they too may now get deleted.
527 */
528void dput(struct dentry *dentry)
529{
530 if (!dentry)
531 return;
532
533repeat:
534 if (dentry->d_count == 1)
535 might_sleep();
536 spin_lock(&dentry->d_lock);
537 BUG_ON(!dentry->d_count);
538 if (dentry->d_count > 1) {
539 dentry->d_count--;
540 spin_unlock(&dentry->d_lock);
541 return;
542 }
543
544 if (dentry->d_flags & DCACHE_OP_DELETE) {
545 if (dentry->d_op->d_delete(dentry))
546 goto kill_it;
547 }
548
549 /* Unreachable? Get rid of it */
550 if (d_unhashed(dentry))
551 goto kill_it;
552
553 /*
554 * If this dentry needs lookup, don't set the referenced flag so that it
555 * is more likely to be cleaned up by the dcache shrinker in case of
556 * memory pressure.
557 */
558 if (!d_need_lookup(dentry))
559 dentry->d_flags |= DCACHE_REFERENCED;
560 dentry_lru_add(dentry);
561
562 dentry->d_count--;
563 spin_unlock(&dentry->d_lock);
564 return;
565
566kill_it:
567 dentry = dentry_kill(dentry, 1);
568 if (dentry)
569 goto repeat;
570}
571EXPORT_SYMBOL(dput);
572
573/**
574 * d_invalidate - invalidate a dentry
575 * @dentry: dentry to invalidate
576 *
577 * Try to invalidate the dentry if it turns out to be
578 * possible. If there are other dentries that can be
579 * reached through this one we can't delete it and we
580 * return -EBUSY. On success we return 0.
581 *
582 * no dcache lock.
583 */
584
585int d_invalidate(struct dentry * dentry)
586{
587 /*
588 * If it's already been dropped, return OK.
589 */
590 spin_lock(&dentry->d_lock);
591 if (d_unhashed(dentry)) {
592 spin_unlock(&dentry->d_lock);
593 return 0;
594 }
595 /*
596 * Check whether to do a partial shrink_dcache
597 * to get rid of unused child entries.
598 */
599 if (!list_empty(&dentry->d_subdirs)) {
600 spin_unlock(&dentry->d_lock);
601 shrink_dcache_parent(dentry);
602 spin_lock(&dentry->d_lock);
603 }
604
605 /*
606 * Somebody else still using it?
607 *
608 * If it's a directory, we can't drop it
609 * for fear of somebody re-populating it
610 * with children (even though dropping it
611 * would make it unreachable from the root,
612 * we might still populate it if it was a
613 * working directory or similar).
614 * We also need to leave mountpoints alone,
615 * directory or not.
616 */
617 if (dentry->d_count > 1 && dentry->d_inode) {
618 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
619 spin_unlock(&dentry->d_lock);
620 return -EBUSY;
621 }
622 }
623
624 __d_drop(dentry);
625 spin_unlock(&dentry->d_lock);
626 return 0;
627}
628EXPORT_SYMBOL(d_invalidate);
629
630/* This must be called with d_lock held */
631static inline void __dget_dlock(struct dentry *dentry)
632{
633 dentry->d_count++;
634}
635
636static inline void __dget(struct dentry *dentry)
637{
638 spin_lock(&dentry->d_lock);
639 __dget_dlock(dentry);
640 spin_unlock(&dentry->d_lock);
641}
642
643struct dentry *dget_parent(struct dentry *dentry)
644{
645 struct dentry *ret;
646
647repeat:
648 /*
649 * Don't need rcu_dereference because we re-check it was correct under
650 * the lock.
651 */
652 rcu_read_lock();
653 ret = dentry->d_parent;
654 spin_lock(&ret->d_lock);
655 if (unlikely(ret != dentry->d_parent)) {
656 spin_unlock(&ret->d_lock);
657 rcu_read_unlock();
658 goto repeat;
659 }
660 rcu_read_unlock();
661 BUG_ON(!ret->d_count);
662 ret->d_count++;
663 spin_unlock(&ret->d_lock);
664 return ret;
665}
666EXPORT_SYMBOL(dget_parent);
667
668/**
669 * d_find_alias - grab a hashed alias of inode
670 * @inode: inode in question
671 * @want_discon: flag, used by d_splice_alias, to request
672 * that only a DISCONNECTED alias be returned.
673 *
674 * If inode has a hashed alias, or is a directory and has any alias,
675 * acquire the reference to alias and return it. Otherwise return NULL.
676 * Notice that if inode is a directory there can be only one alias and
677 * it can be unhashed only if it has no children, or if it is the root
678 * of a filesystem.
679 *
680 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
681 * any other hashed alias over that one unless @want_discon is set,
682 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
683 */
684static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
685{
686 struct dentry *alias, *discon_alias;
687
688again:
689 discon_alias = NULL;
690 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
691 spin_lock(&alias->d_lock);
692 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
693 if (IS_ROOT(alias) &&
694 (alias->d_flags & DCACHE_DISCONNECTED)) {
695 discon_alias = alias;
696 } else if (!want_discon) {
697 __dget_dlock(alias);
698 spin_unlock(&alias->d_lock);
699 return alias;
700 }
701 }
702 spin_unlock(&alias->d_lock);
703 }
704 if (discon_alias) {
705 alias = discon_alias;
706 spin_lock(&alias->d_lock);
707 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
708 if (IS_ROOT(alias) &&
709 (alias->d_flags & DCACHE_DISCONNECTED)) {
710 __dget_dlock(alias);
711 spin_unlock(&alias->d_lock);
712 return alias;
713 }
714 }
715 spin_unlock(&alias->d_lock);
716 goto again;
717 }
718 return NULL;
719}
720
721struct dentry *d_find_alias(struct inode *inode)
722{
723 struct dentry *de = NULL;
724
725 if (!list_empty(&inode->i_dentry)) {
726 spin_lock(&inode->i_lock);
727 de = __d_find_alias(inode, 0);
728 spin_unlock(&inode->i_lock);
729 }
730 return de;
731}
732EXPORT_SYMBOL(d_find_alias);
733
734/*
735 * Try to kill dentries associated with this inode.
736 * WARNING: you must own a reference to inode.
737 */
738void d_prune_aliases(struct inode *inode)
739{
740 struct dentry *dentry;
741restart:
742 spin_lock(&inode->i_lock);
743 list_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
744 spin_lock(&dentry->d_lock);
745 if (!dentry->d_count) {
746 __dget_dlock(dentry);
747 __d_drop(dentry);
748 spin_unlock(&dentry->d_lock);
749 spin_unlock(&inode->i_lock);
750 dput(dentry);
751 goto restart;
752 }
753 spin_unlock(&dentry->d_lock);
754 }
755 spin_unlock(&inode->i_lock);
756}
757EXPORT_SYMBOL(d_prune_aliases);
758
759/*
760 * Try to throw away a dentry - free the inode, dput the parent.
761 * Requires dentry->d_lock is held, and dentry->d_count == 0.
762 * Releases dentry->d_lock.
763 *
764 * This may fail if locks cannot be acquired no problem, just try again.
765 */
766static void try_prune_one_dentry(struct dentry *dentry)
767 __releases(dentry->d_lock)
768{
769 struct dentry *parent;
770
771 parent = dentry_kill(dentry, 0);
772 /*
773 * If dentry_kill returns NULL, we have nothing more to do.
774 * if it returns the same dentry, trylocks failed. In either
775 * case, just loop again.
776 *
777 * Otherwise, we need to prune ancestors too. This is necessary
778 * to prevent quadratic behavior of shrink_dcache_parent(), but
779 * is also expected to be beneficial in reducing dentry cache
780 * fragmentation.
781 */
782 if (!parent)
783 return;
784 if (parent == dentry)
785 return;
786
787 /* Prune ancestors. */
788 dentry = parent;
789 while (dentry) {
790 spin_lock(&dentry->d_lock);
791 if (dentry->d_count > 1) {
792 dentry->d_count--;
793 spin_unlock(&dentry->d_lock);
794 return;
795 }
796 dentry = dentry_kill(dentry, 1);
797 }
798}
799
800static void shrink_dentry_list(struct list_head *list)
801{
802 struct dentry *dentry;
803
804 rcu_read_lock();
805 for (;;) {
806 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
807 if (&dentry->d_lru == list)
808 break; /* empty */
809 spin_lock(&dentry->d_lock);
810 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
811 spin_unlock(&dentry->d_lock);
812 continue;
813 }
814
815 /*
816 * We found an inuse dentry which was not removed from
817 * the LRU because of laziness during lookup. Do not free
818 * it - just keep it off the LRU list.
819 */
820 if (dentry->d_count) {
821 dentry_lru_del(dentry);
822 spin_unlock(&dentry->d_lock);
823 continue;
824 }
825
826 rcu_read_unlock();
827
828 try_prune_one_dentry(dentry);
829
830 rcu_read_lock();
831 }
832 rcu_read_unlock();
833}
834
835/**
836 * prune_dcache_sb - shrink the dcache
837 * @sb: superblock
838 * @count: number of entries to try to free
839 *
840 * Attempt to shrink the superblock dcache LRU by @count entries. This is
841 * done when we need more memory an called from the superblock shrinker
842 * function.
843 *
844 * This function may fail to free any resources if all the dentries are in
845 * use.
846 */
847void prune_dcache_sb(struct super_block *sb, int count)
848{
849 struct dentry *dentry;
850 LIST_HEAD(referenced);
851 LIST_HEAD(tmp);
852
853relock:
854 spin_lock(&dcache_lru_lock);
855 while (!list_empty(&sb->s_dentry_lru)) {
856 dentry = list_entry(sb->s_dentry_lru.prev,
857 struct dentry, d_lru);
858 BUG_ON(dentry->d_sb != sb);
859
860 if (!spin_trylock(&dentry->d_lock)) {
861 spin_unlock(&dcache_lru_lock);
862 cpu_chill();
863 goto relock;
864 }
865
866 if (dentry->d_flags & DCACHE_REFERENCED) {
867 dentry->d_flags &= ~DCACHE_REFERENCED;
868 list_move(&dentry->d_lru, &referenced);
869 spin_unlock(&dentry->d_lock);
870 } else {
871 list_move_tail(&dentry->d_lru, &tmp);
872 dentry->d_flags |= DCACHE_SHRINK_LIST;
873 spin_unlock(&dentry->d_lock);
874 if (!--count)
875 break;
876 }
877 cond_resched_lock(&dcache_lru_lock);
878 }
879 if (!list_empty(&referenced))
880 list_splice(&referenced, &sb->s_dentry_lru);
881 spin_unlock(&dcache_lru_lock);
882
883 shrink_dentry_list(&tmp);
884}
885
886/**
887 * shrink_dcache_sb - shrink dcache for a superblock
888 * @sb: superblock
889 *
890 * Shrink the dcache for the specified super block. This is used to free
891 * the dcache before unmounting a file system.
892 */
893void shrink_dcache_sb(struct super_block *sb)
894{
895 LIST_HEAD(tmp);
896
897 spin_lock(&dcache_lru_lock);
898 while (!list_empty(&sb->s_dentry_lru)) {
899 list_splice_init(&sb->s_dentry_lru, &tmp);
900 spin_unlock(&dcache_lru_lock);
901 shrink_dentry_list(&tmp);
902 spin_lock(&dcache_lru_lock);
903 }
904 spin_unlock(&dcache_lru_lock);
905}
906EXPORT_SYMBOL(shrink_dcache_sb);
907
908/*
909 * destroy a single subtree of dentries for unmount
910 * - see the comments on shrink_dcache_for_umount() for a description of the
911 * locking
912 */
913static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
914{
915 struct dentry *parent;
916
917 BUG_ON(!IS_ROOT(dentry));
918
919 for (;;) {
920 /* descend to the first leaf in the current subtree */
921 while (!list_empty(&dentry->d_subdirs))
922 dentry = list_entry(dentry->d_subdirs.next,
923 struct dentry, d_child);
924
925 /* consume the dentries from this leaf up through its parents
926 * until we find one with children or run out altogether */
927 do {
928 struct inode *inode;
929
930 /*
931 * remove the dentry from the lru, and inform
932 * the fs that this dentry is about to be
933 * unhashed and destroyed.
934 */
935 dentry_lru_prune(dentry);
936 __d_shrink(dentry);
937
938 if (dentry->d_count != 0) {
939 printk(KERN_ERR
940 "BUG: Dentry %p{i=%lx,n=%s}"
941 " still in use (%d)"
942 " [unmount of %s %s]\n",
943 dentry,
944 dentry->d_inode ?
945 dentry->d_inode->i_ino : 0UL,
946 dentry->d_name.name,
947 dentry->d_count,
948 dentry->d_sb->s_type->name,
949 dentry->d_sb->s_id);
950 BUG();
951 }
952
953 if (IS_ROOT(dentry)) {
954 parent = NULL;
955 list_del(&dentry->d_child);
956 } else {
957 parent = dentry->d_parent;
958 parent->d_count--;
959 list_del(&dentry->d_child);
960 }
961
962 inode = dentry->d_inode;
963 if (inode) {
964 dentry->d_inode = NULL;
965 list_del_init(&dentry->d_u.d_alias);
966 if (dentry->d_op && dentry->d_op->d_iput)
967 dentry->d_op->d_iput(dentry, inode);
968 else
969 iput(inode);
970 }
971
972 d_free(dentry);
973
974 /* finished when we fall off the top of the tree,
975 * otherwise we ascend to the parent and move to the
976 * next sibling if there is one */
977 if (!parent)
978 return;
979 dentry = parent;
980 } while (list_empty(&dentry->d_subdirs));
981
982 dentry = list_entry(dentry->d_subdirs.next,
983 struct dentry, d_child);
984 }
985}
986
987/*
988 * destroy the dentries attached to a superblock on unmounting
989 * - we don't need to use dentry->d_lock because:
990 * - the superblock is detached from all mountings and open files, so the
991 * dentry trees will not be rearranged by the VFS
992 * - s_umount is write-locked, so the memory pressure shrinker will ignore
993 * any dentries belonging to this superblock that it comes across
994 * - the filesystem itself is no longer permitted to rearrange the dentries
995 * in this superblock
996 */
997void shrink_dcache_for_umount(struct super_block *sb)
998{
999 struct dentry *dentry;
1000
1001 if (down_read_trylock(&sb->s_umount))
1002 BUG();
1003
1004 dentry = sb->s_root;
1005 sb->s_root = NULL;
1006 dentry->d_count--;
1007 shrink_dcache_for_umount_subtree(dentry);
1008
1009 while (!hlist_bl_empty(&sb->s_anon)) {
1010 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
1011 shrink_dcache_for_umount_subtree(dentry);
1012 }
1013}
1014
1015
1016/*
1017 * Search for at least 1 mount point in the dentry's subdirs.
1018 * We descend to the next level whenever the d_subdirs
1019 * list is non-empty and continue searching.
1020 */
1021
1022/**
1023 * have_submounts - check for mounts over a dentry
1024 * @parent: dentry to check.
1025 *
1026 * Return true if the parent or its subdirectories contain
1027 * a mount point
1028 */
1029int have_submounts(struct dentry *parent)
1030{
1031 struct dentry *this_parent;
1032 struct list_head *next;
1033 unsigned seq;
1034 int locked = 0;
1035
1036 seq = read_seqbegin(&rename_lock);
1037again:
1038 this_parent = parent;
1039
1040 if (d_mountpoint(parent))
1041 goto positive;
1042 spin_lock(&this_parent->d_lock);
1043repeat:
1044 next = this_parent->d_subdirs.next;
1045resume:
1046 while (next != &this_parent->d_subdirs) {
1047 struct list_head *tmp = next;
1048 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1049 next = tmp->next;
1050
1051 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1052 /* Have we found a mount point ? */
1053 if (d_mountpoint(dentry)) {
1054 spin_unlock(&dentry->d_lock);
1055 spin_unlock(&this_parent->d_lock);
1056 goto positive;
1057 }
1058 if (!list_empty(&dentry->d_subdirs)) {
1059 spin_unlock(&this_parent->d_lock);
1060 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1061 this_parent = dentry;
1062 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1063 goto repeat;
1064 }
1065 spin_unlock(&dentry->d_lock);
1066 }
1067 /*
1068 * All done at this level ... ascend and resume the search.
1069 */
1070 rcu_read_lock();
1071ascend:
1072 if (this_parent != parent) {
1073 struct dentry *child = this_parent;
1074 this_parent = child->d_parent;
1075
1076 spin_unlock(&child->d_lock);
1077 spin_lock(&this_parent->d_lock);
1078
1079 /* might go back up the wrong parent if we have had a rename */
1080 if (!locked && read_seqretry(&rename_lock, seq))
1081 goto rename_retry;
1082 next = child->d_child.next;
1083 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
1084 if (next == &this_parent->d_subdirs)
1085 goto ascend;
1086 child = list_entry(next, struct dentry, d_child);
1087 next = next->next;
1088 }
1089 rcu_read_unlock();
1090 goto resume;
1091 }
1092 if (!locked && read_seqretry(&rename_lock, seq))
1093 goto rename_retry;
1094 spin_unlock(&this_parent->d_lock);
1095 rcu_read_unlock();
1096 if (locked)
1097 write_sequnlock(&rename_lock);
1098 return 0; /* No mount points found in tree */
1099positive:
1100 if (!locked && read_seqretry(&rename_lock, seq))
1101 goto rename_retry_unlocked;
1102 if (locked)
1103 write_sequnlock(&rename_lock);
1104 return 1;
1105
1106rename_retry:
1107 spin_unlock(&this_parent->d_lock);
1108 rcu_read_unlock();
1109 if (locked)
1110 goto again;
1111rename_retry_unlocked:
1112 locked = 1;
1113 write_seqlock(&rename_lock);
1114 goto again;
1115}
1116EXPORT_SYMBOL(have_submounts);
1117
1118/*
1119 * Search the dentry child list for the specified parent,
1120 * and move any unused dentries to the end of the unused
1121 * list for prune_dcache(). We descend to the next level
1122 * whenever the d_subdirs list is non-empty and continue
1123 * searching.
1124 *
1125 * It returns zero iff there are no unused children,
1126 * otherwise it returns the number of children moved to
1127 * the end of the unused list. This may not be the total
1128 * number of unused children, because select_parent can
1129 * drop the lock and return early due to latency
1130 * constraints.
1131 */
1132static int select_parent(struct dentry *parent, struct list_head *dispose)
1133{
1134 struct dentry *this_parent;
1135 struct list_head *next;
1136 unsigned seq;
1137 int found = 0;
1138 int locked = 0;
1139
1140 seq = read_seqbegin(&rename_lock);
1141again:
1142 this_parent = parent;
1143 spin_lock(&this_parent->d_lock);
1144repeat:
1145 next = this_parent->d_subdirs.next;
1146resume:
1147 while (next != &this_parent->d_subdirs) {
1148 struct list_head *tmp = next;
1149 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1150 next = tmp->next;
1151
1152 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1153
1154 /*
1155 * move only zero ref count dentries to the dispose list.
1156 *
1157 * Those which are presently on the shrink list, being processed
1158 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1159 * loop in shrink_dcache_parent() might not make any progress
1160 * and loop forever.
1161 */
1162 if (dentry->d_count) {
1163 dentry_lru_del(dentry);
1164 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1165 dentry_lru_move_list(dentry, dispose);
1166 dentry->d_flags |= DCACHE_SHRINK_LIST;
1167 found++;
1168 }
1169 /*
1170 * We can return to the caller if we have found some (this
1171 * ensures forward progress). We'll be coming back to find
1172 * the rest.
1173 */
1174 if (found && need_resched()) {
1175 spin_unlock(&dentry->d_lock);
1176 rcu_read_lock();
1177 goto out;
1178 }
1179
1180 /*
1181 * Descend a level if the d_subdirs list is non-empty.
1182 */
1183 if (!list_empty(&dentry->d_subdirs)) {
1184 spin_unlock(&this_parent->d_lock);
1185 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1186 this_parent = dentry;
1187 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1188 goto repeat;
1189 }
1190
1191 spin_unlock(&dentry->d_lock);
1192 }
1193 /*
1194 * All done at this level ... ascend and resume the search.
1195 */
1196 rcu_read_lock();
1197ascend:
1198 if (this_parent != parent) {
1199 struct dentry *child = this_parent;
1200 this_parent = child->d_parent;
1201
1202 spin_unlock(&child->d_lock);
1203 spin_lock(&this_parent->d_lock);
1204
1205 /* might go back up the wrong parent if we have had a rename */
1206 if (!locked && read_seqretry(&rename_lock, seq))
1207 goto rename_retry;
1208 /* go into the first sibling still alive */
1209 do {
1210 next = child->d_child.next;
1211 if (next == &this_parent->d_subdirs)
1212 goto ascend;
1213 child = list_entry(next, struct dentry, d_child);
1214 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
1215 rcu_read_unlock();
1216 goto resume;
1217 }
1218out:
1219 if (!locked && read_seqretry(&rename_lock, seq))
1220 goto rename_retry;
1221 spin_unlock(&this_parent->d_lock);
1222 rcu_read_unlock();
1223 if (locked)
1224 write_sequnlock(&rename_lock);
1225 return found;
1226
1227rename_retry:
1228 spin_unlock(&this_parent->d_lock);
1229 rcu_read_unlock();
1230 if (found)
1231 return found;
1232 if (locked)
1233 goto again;
1234 locked = 1;
1235 write_seqlock(&rename_lock);
1236 goto again;
1237}
1238
1239/**
1240 * shrink_dcache_parent - prune dcache
1241 * @parent: parent of entries to prune
1242 *
1243 * Prune the dcache to remove unused children of the parent dentry.
1244 */
1245void shrink_dcache_parent(struct dentry * parent)
1246{
1247 LIST_HEAD(dispose);
1248 int found;
1249
1250 while ((found = select_parent(parent, &dispose)) != 0) {
1251 shrink_dentry_list(&dispose);
1252 cond_resched();
1253 }
1254}
1255EXPORT_SYMBOL(shrink_dcache_parent);
1256
1257/**
1258 * __d_alloc - allocate a dcache entry
1259 * @sb: filesystem it will belong to
1260 * @name: qstr of the name
1261 *
1262 * Allocates a dentry. It returns %NULL if there is insufficient memory
1263 * available. On a success the dentry is returned. The name passed in is
1264 * copied and the copy passed in may be reused after this call.
1265 */
1266
1267struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1268{
1269 struct dentry *dentry;
1270 char *dname;
1271
1272 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1273 if (!dentry)
1274 return NULL;
1275
1276 if (name->len > DNAME_INLINE_LEN-1) {
1277 dname = kmalloc(name->len + 1, GFP_KERNEL);
1278 if (!dname) {
1279 kmem_cache_free(dentry_cache, dentry);
1280 return NULL;
1281 }
1282 } else {
1283 dname = dentry->d_iname;
1284 }
1285 dentry->d_name.name = dname;
1286
1287 dentry->d_name.len = name->len;
1288 dentry->d_name.hash = name->hash;
1289 memcpy(dname, name->name, name->len);
1290 dname[name->len] = 0;
1291
1292 dentry->d_count = 1;
1293 dentry->d_flags = 0;
1294 spin_lock_init(&dentry->d_lock);
1295 seqcount_init(&dentry->d_seq);
1296 dentry->d_inode = NULL;
1297 dentry->d_parent = dentry;
1298 dentry->d_sb = sb;
1299 dentry->d_op = NULL;
1300 dentry->d_fsdata = NULL;
1301 INIT_HLIST_BL_NODE(&dentry->d_hash);
1302 INIT_LIST_HEAD(&dentry->d_lru);
1303 INIT_LIST_HEAD(&dentry->d_subdirs);
1304 INIT_LIST_HEAD(&dentry->d_u.d_alias);
1305 INIT_LIST_HEAD(&dentry->d_child);
1306 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1307
1308 this_cpu_inc(nr_dentry);
1309
1310 return dentry;
1311}
1312
1313/**
1314 * d_alloc - allocate a dcache entry
1315 * @parent: parent of entry to allocate
1316 * @name: qstr of the name
1317 *
1318 * Allocates a dentry. It returns %NULL if there is insufficient memory
1319 * available. On a success the dentry is returned. The name passed in is
1320 * copied and the copy passed in may be reused after this call.
1321 */
1322struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1323{
1324 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1325 if (!dentry)
1326 return NULL;
1327
1328 spin_lock(&parent->d_lock);
1329 /*
1330 * don't need child lock because it is not subject
1331 * to concurrency here
1332 */
1333 __dget_dlock(parent);
1334 dentry->d_parent = parent;
1335 list_add(&dentry->d_child, &parent->d_subdirs);
1336 spin_unlock(&parent->d_lock);
1337
1338 return dentry;
1339}
1340EXPORT_SYMBOL(d_alloc);
1341
1342struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1343{
1344 struct dentry *dentry = __d_alloc(sb, name);
1345 if (dentry)
1346 dentry->d_flags |= DCACHE_DISCONNECTED;
1347 return dentry;
1348}
1349EXPORT_SYMBOL(d_alloc_pseudo);
1350
1351struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1352{
1353 struct qstr q;
1354
1355 q.name = name;
1356 q.len = strlen(name);
1357 q.hash = full_name_hash(q.name, q.len);
1358 return d_alloc(parent, &q);
1359}
1360EXPORT_SYMBOL(d_alloc_name);
1361
1362void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1363{
1364 WARN_ON_ONCE(dentry->d_op);
1365 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1366 DCACHE_OP_COMPARE |
1367 DCACHE_OP_REVALIDATE |
1368 DCACHE_OP_DELETE ));
1369 dentry->d_op = op;
1370 if (!op)
1371 return;
1372 if (op->d_hash)
1373 dentry->d_flags |= DCACHE_OP_HASH;
1374 if (op->d_compare)
1375 dentry->d_flags |= DCACHE_OP_COMPARE;
1376 if (op->d_revalidate)
1377 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1378 if (op->d_delete)
1379 dentry->d_flags |= DCACHE_OP_DELETE;
1380 if (op->d_prune)
1381 dentry->d_flags |= DCACHE_OP_PRUNE;
1382
1383}
1384EXPORT_SYMBOL(d_set_d_op);
1385
1386static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1387{
1388 spin_lock(&dentry->d_lock);
1389 if (inode) {
1390 if (unlikely(IS_AUTOMOUNT(inode)))
1391 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
1392 list_add(&dentry->d_u.d_alias, &inode->i_dentry);
1393 }
1394 dentry->d_inode = inode;
1395 dentry_rcuwalk_barrier(dentry);
1396 spin_unlock(&dentry->d_lock);
1397 fsnotify_d_instantiate(dentry, inode);
1398}
1399
1400/**
1401 * d_instantiate - fill in inode information for a dentry
1402 * @entry: dentry to complete
1403 * @inode: inode to attach to this dentry
1404 *
1405 * Fill in inode information in the entry.
1406 *
1407 * This turns negative dentries into productive full members
1408 * of society.
1409 *
1410 * NOTE! This assumes that the inode count has been incremented
1411 * (or otherwise set) by the caller to indicate that it is now
1412 * in use by the dcache.
1413 */
1414
1415void d_instantiate(struct dentry *entry, struct inode * inode)
1416{
1417 BUG_ON(!list_empty(&entry->d_u.d_alias));
1418 if (inode)
1419 spin_lock(&inode->i_lock);
1420 __d_instantiate(entry, inode);
1421 if (inode)
1422 spin_unlock(&inode->i_lock);
1423 security_d_instantiate(entry, inode);
1424}
1425EXPORT_SYMBOL(d_instantiate);
1426
1427/**
1428 * d_instantiate_unique - instantiate a non-aliased dentry
1429 * @entry: dentry to instantiate
1430 * @inode: inode to attach to this dentry
1431 *
1432 * Fill in inode information in the entry. On success, it returns NULL.
1433 * If an unhashed alias of "entry" already exists, then we return the
1434 * aliased dentry instead and drop one reference to inode.
1435 *
1436 * Note that in order to avoid conflicts with rename() etc, the caller
1437 * had better be holding the parent directory semaphore.
1438 *
1439 * This also assumes that the inode count has been incremented
1440 * (or otherwise set) by the caller to indicate that it is now
1441 * in use by the dcache.
1442 */
1443static struct dentry *__d_instantiate_unique(struct dentry *entry,
1444 struct inode *inode)
1445{
1446 struct dentry *alias;
1447 int len = entry->d_name.len;
1448 const char *name = entry->d_name.name;
1449 unsigned int hash = entry->d_name.hash;
1450
1451 if (!inode) {
1452 __d_instantiate(entry, NULL);
1453 return NULL;
1454 }
1455
1456 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1457 struct qstr *qstr = &alias->d_name;
1458
1459 /*
1460 * Don't need alias->d_lock here, because aliases with
1461 * d_parent == entry->d_parent are not subject to name or
1462 * parent changes, because the parent inode i_mutex is held.
1463 */
1464 if (qstr->hash != hash)
1465 continue;
1466 if (alias->d_parent != entry->d_parent)
1467 continue;
1468 if (dentry_cmp(qstr->name, qstr->len, name, len))
1469 continue;
1470 __dget(alias);
1471 return alias;
1472 }
1473
1474 __d_instantiate(entry, inode);
1475 return NULL;
1476}
1477
1478struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1479{
1480 struct dentry *result;
1481
1482 BUG_ON(!list_empty(&entry->d_u.d_alias));
1483
1484 if (inode)
1485 spin_lock(&inode->i_lock);
1486 result = __d_instantiate_unique(entry, inode);
1487 if (inode)
1488 spin_unlock(&inode->i_lock);
1489
1490 if (!result) {
1491 security_d_instantiate(entry, inode);
1492 return NULL;
1493 }
1494
1495 BUG_ON(!d_unhashed(result));
1496 iput(inode);
1497 return result;
1498}
1499
1500EXPORT_SYMBOL(d_instantiate_unique);
1501
1502struct dentry *d_make_root(struct inode *root_inode)
1503{
1504 struct dentry *res = NULL;
1505
1506 if (root_inode) {
1507 static const struct qstr name = { .name = "/", .len = 1 };
1508
1509 res = __d_alloc(root_inode->i_sb, &name);
1510 if (res)
1511 d_instantiate(res, root_inode);
1512 else
1513 iput(root_inode);
1514 }
1515 return res;
1516}
1517EXPORT_SYMBOL(d_make_root);
1518
1519static struct dentry * __d_find_any_alias(struct inode *inode)
1520{
1521 struct dentry *alias;
1522
1523 if (list_empty(&inode->i_dentry))
1524 return NULL;
1525 alias = list_first_entry(&inode->i_dentry, struct dentry, d_u.d_alias);
1526 __dget(alias);
1527 return alias;
1528}
1529
1530/**
1531 * d_find_any_alias - find any alias for a given inode
1532 * @inode: inode to find an alias for
1533 *
1534 * If any aliases exist for the given inode, take and return a
1535 * reference for one of them. If no aliases exist, return %NULL.
1536 */
1537struct dentry *d_find_any_alias(struct inode *inode)
1538{
1539 struct dentry *de;
1540
1541 spin_lock(&inode->i_lock);
1542 de = __d_find_any_alias(inode);
1543 spin_unlock(&inode->i_lock);
1544 return de;
1545}
1546EXPORT_SYMBOL(d_find_any_alias);
1547
1548/**
1549 * d_obtain_alias - find or allocate a dentry for a given inode
1550 * @inode: inode to allocate the dentry for
1551 *
1552 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1553 * similar open by handle operations. The returned dentry may be anonymous,
1554 * or may have a full name (if the inode was already in the cache).
1555 *
1556 * When called on a directory inode, we must ensure that the inode only ever
1557 * has one dentry. If a dentry is found, that is returned instead of
1558 * allocating a new one.
1559 *
1560 * On successful return, the reference to the inode has been transferred
1561 * to the dentry. In case of an error the reference on the inode is released.
1562 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1563 * be passed in and will be the error will be propagate to the return value,
1564 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1565 */
1566struct dentry *d_obtain_alias(struct inode *inode)
1567{
1568 static const struct qstr anonstring = { .name = "/", .len = 1 };
1569 struct dentry *tmp;
1570 struct dentry *res;
1571
1572 if (!inode)
1573 return ERR_PTR(-ESTALE);
1574 if (IS_ERR(inode))
1575 return ERR_CAST(inode);
1576
1577 res = d_find_any_alias(inode);
1578 if (res)
1579 goto out_iput;
1580
1581 tmp = __d_alloc(inode->i_sb, &anonstring);
1582 if (!tmp) {
1583 res = ERR_PTR(-ENOMEM);
1584 goto out_iput;
1585 }
1586
1587 spin_lock(&inode->i_lock);
1588 res = __d_find_any_alias(inode);
1589 if (res) {
1590 spin_unlock(&inode->i_lock);
1591 dput(tmp);
1592 goto out_iput;
1593 }
1594
1595 /* attach a disconnected dentry */
1596 spin_lock(&tmp->d_lock);
1597 tmp->d_inode = inode;
1598 tmp->d_flags |= DCACHE_DISCONNECTED;
1599 list_add(&tmp->d_u.d_alias, &inode->i_dentry);
1600 hlist_bl_lock(&tmp->d_sb->s_anon);
1601 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
1602 hlist_bl_unlock(&tmp->d_sb->s_anon);
1603 spin_unlock(&tmp->d_lock);
1604 spin_unlock(&inode->i_lock);
1605 security_d_instantiate(tmp, inode);
1606
1607 return tmp;
1608
1609 out_iput:
1610 if (res && !IS_ERR(res))
1611 security_d_instantiate(res, inode);
1612 iput(inode);
1613 return res;
1614}
1615EXPORT_SYMBOL(d_obtain_alias);
1616
1617/**
1618 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1619 * @inode: the inode which may have a disconnected dentry
1620 * @dentry: a negative dentry which we want to point to the inode.
1621 *
1622 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1623 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1624 * and return it, else simply d_add the inode to the dentry and return NULL.
1625 *
1626 * This is needed in the lookup routine of any filesystem that is exportable
1627 * (via knfsd) so that we can build dcache paths to directories effectively.
1628 *
1629 * If a dentry was found and moved, then it is returned. Otherwise NULL
1630 * is returned. This matches the expected return value of ->lookup.
1631 *
1632 */
1633struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1634{
1635 struct dentry *new = NULL;
1636
1637 if (IS_ERR(inode))
1638 return ERR_CAST(inode);
1639
1640 if (inode && S_ISDIR(inode->i_mode)) {
1641 spin_lock(&inode->i_lock);
1642 new = __d_find_alias(inode, 1);
1643 if (new) {
1644 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1645 spin_unlock(&inode->i_lock);
1646 security_d_instantiate(new, inode);
1647 d_move(new, dentry);
1648 iput(inode);
1649 } else {
1650 /* already taking inode->i_lock, so d_add() by hand */
1651 __d_instantiate(dentry, inode);
1652 spin_unlock(&inode->i_lock);
1653 security_d_instantiate(dentry, inode);
1654 d_rehash(dentry);
1655 }
1656 } else
1657 d_add(dentry, inode);
1658 return new;
1659}
1660EXPORT_SYMBOL(d_splice_alias);
1661
1662/**
1663 * d_add_ci - lookup or allocate new dentry with case-exact name
1664 * @inode: the inode case-insensitive lookup has found
1665 * @dentry: the negative dentry that was passed to the parent's lookup func
1666 * @name: the case-exact name to be associated with the returned dentry
1667 *
1668 * This is to avoid filling the dcache with case-insensitive names to the
1669 * same inode, only the actual correct case is stored in the dcache for
1670 * case-insensitive filesystems.
1671 *
1672 * For a case-insensitive lookup match and if the the case-exact dentry
1673 * already exists in in the dcache, use it and return it.
1674 *
1675 * If no entry exists with the exact case name, allocate new dentry with
1676 * the exact case, and return the spliced entry.
1677 */
1678struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1679 struct qstr *name)
1680{
1681 int error;
1682 struct dentry *found;
1683 struct dentry *new;
1684
1685 /*
1686 * First check if a dentry matching the name already exists,
1687 * if not go ahead and create it now.
1688 */
1689 found = d_hash_and_lookup(dentry->d_parent, name);
1690 if (!found) {
1691 new = d_alloc(dentry->d_parent, name);
1692 if (!new) {
1693 error = -ENOMEM;
1694 goto err_out;
1695 }
1696
1697 found = d_splice_alias(inode, new);
1698 if (found) {
1699 dput(new);
1700 return found;
1701 }
1702 return new;
1703 }
1704
1705 /*
1706 * If a matching dentry exists, and it's not negative use it.
1707 *
1708 * Decrement the reference count to balance the iget() done
1709 * earlier on.
1710 */
1711 if (found->d_inode) {
1712 if (unlikely(found->d_inode != inode)) {
1713 /* This can't happen because bad inodes are unhashed. */
1714 BUG_ON(!is_bad_inode(inode));
1715 BUG_ON(!is_bad_inode(found->d_inode));
1716 }
1717 iput(inode);
1718 return found;
1719 }
1720
1721 /*
1722 * We are going to instantiate this dentry, unhash it and clear the
1723 * lookup flag so we can do that.
1724 */
1725 if (unlikely(d_need_lookup(found)))
1726 d_clear_need_lookup(found);
1727
1728 /*
1729 * Negative dentry: instantiate it unless the inode is a directory and
1730 * already has a dentry.
1731 */
1732 new = d_splice_alias(inode, found);
1733 if (new) {
1734 dput(found);
1735 found = new;
1736 }
1737 return found;
1738
1739err_out:
1740 iput(inode);
1741 return ERR_PTR(error);
1742}
1743EXPORT_SYMBOL(d_add_ci);
1744
1745/**
1746 * __d_lookup_rcu - search for a dentry (racy, store-free)
1747 * @parent: parent dentry
1748 * @name: qstr of name we wish to find
1749 * @seqp: returns d_seq value at the point where the dentry was found
1750 * @inode: returns dentry->d_inode when the inode was found valid.
1751 * Returns: dentry, or NULL
1752 *
1753 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1754 * resolution (store-free path walking) design described in
1755 * Documentation/filesystems/path-lookup.txt.
1756 *
1757 * This is not to be used outside core vfs.
1758 *
1759 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1760 * held, and rcu_read_lock held. The returned dentry must not be stored into
1761 * without taking d_lock and checking d_seq sequence count against @seq
1762 * returned here.
1763 *
1764 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1765 * function.
1766 *
1767 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1768 * the returned dentry, so long as its parent's seqlock is checked after the
1769 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1770 * is formed, giving integrity down the path walk.
1771 */
1772struct dentry *__d_lookup_rcu(const struct dentry *parent,
1773 const struct qstr *name,
1774 unsigned *seqp, struct inode **inode)
1775{
1776 unsigned int len = name->len;
1777 unsigned int hash = name->hash;
1778 const unsigned char *str = name->name;
1779 struct hlist_bl_head *b = d_hash(parent, hash);
1780 struct hlist_bl_node *node;
1781 struct dentry *dentry;
1782
1783 /*
1784 * Note: There is significant duplication with __d_lookup_rcu which is
1785 * required to prevent single threaded performance regressions
1786 * especially on architectures where smp_rmb (in seqcounts) are costly.
1787 * Keep the two functions in sync.
1788 */
1789
1790 /*
1791 * The hash list is protected using RCU.
1792 *
1793 * Carefully use d_seq when comparing a candidate dentry, to avoid
1794 * races with d_move().
1795 *
1796 * It is possible that concurrent renames can mess up our list
1797 * walk here and result in missing our dentry, resulting in the
1798 * false-negative result. d_lookup() protects against concurrent
1799 * renames using rename_lock seqlock.
1800 *
1801 * See Documentation/filesystems/path-lookup.txt for more details.
1802 */
1803 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
1804 unsigned seq;
1805 struct inode *i;
1806 const char *tname;
1807 int tlen;
1808
1809 if (dentry->d_name.hash != hash)
1810 continue;
1811
1812seqretry:
1813 seq = read_seqcount_begin(&dentry->d_seq);
1814 if (dentry->d_parent != parent)
1815 continue;
1816 if (d_unhashed(dentry))
1817 continue;
1818 tlen = dentry->d_name.len;
1819 tname = dentry->d_name.name;
1820 i = dentry->d_inode;
1821 prefetch(tname);
1822 /*
1823 * This seqcount check is required to ensure name and
1824 * len are loaded atomically, so as not to walk off the
1825 * edge of memory when walking. If we could load this
1826 * atomically some other way, we could drop this check.
1827 */
1828 if (read_seqcount_retry(&dentry->d_seq, seq))
1829 goto seqretry;
1830 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
1831 if (parent->d_op->d_compare(parent, *inode,
1832 dentry, i,
1833 tlen, tname, name))
1834 continue;
1835 } else {
1836 if (dentry_cmp(tname, tlen, str, len))
1837 continue;
1838 }
1839 /*
1840 * No extra seqcount check is required after the name
1841 * compare. The caller must perform a seqcount check in
1842 * order to do anything useful with the returned dentry
1843 * anyway.
1844 */
1845 *seqp = seq;
1846 *inode = i;
1847 return dentry;
1848 }
1849 return NULL;
1850}
1851
1852/**
1853 * d_lookup - search for a dentry
1854 * @parent: parent dentry
1855 * @name: qstr of name we wish to find
1856 * Returns: dentry, or NULL
1857 *
1858 * d_lookup searches the children of the parent dentry for the name in
1859 * question. If the dentry is found its reference count is incremented and the
1860 * dentry is returned. The caller must use dput to free the entry when it has
1861 * finished using it. %NULL is returned if the dentry does not exist.
1862 */
1863struct dentry *d_lookup(struct dentry *parent, struct qstr *name)
1864{
1865 struct dentry *dentry;
1866 unsigned seq;
1867
1868 do {
1869 seq = read_seqbegin(&rename_lock);
1870 dentry = __d_lookup(parent, name);
1871 if (dentry)
1872 break;
1873 } while (read_seqretry(&rename_lock, seq));
1874 return dentry;
1875}
1876EXPORT_SYMBOL(d_lookup);
1877
1878/**
1879 * __d_lookup - search for a dentry (racy)
1880 * @parent: parent dentry
1881 * @name: qstr of name we wish to find
1882 * Returns: dentry, or NULL
1883 *
1884 * __d_lookup is like d_lookup, however it may (rarely) return a
1885 * false-negative result due to unrelated rename activity.
1886 *
1887 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1888 * however it must be used carefully, eg. with a following d_lookup in
1889 * the case of failure.
1890 *
1891 * __d_lookup callers must be commented.
1892 */
1893struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
1894{
1895 unsigned int len = name->len;
1896 unsigned int hash = name->hash;
1897 const unsigned char *str = name->name;
1898 struct hlist_bl_head *b = d_hash(parent, hash);
1899 struct hlist_bl_node *node;
1900 struct dentry *found = NULL;
1901 struct dentry *dentry;
1902
1903 /*
1904 * Note: There is significant duplication with __d_lookup_rcu which is
1905 * required to prevent single threaded performance regressions
1906 * especially on architectures where smp_rmb (in seqcounts) are costly.
1907 * Keep the two functions in sync.
1908 */
1909
1910 /*
1911 * The hash list is protected using RCU.
1912 *
1913 * Take d_lock when comparing a candidate dentry, to avoid races
1914 * with d_move().
1915 *
1916 * It is possible that concurrent renames can mess up our list
1917 * walk here and result in missing our dentry, resulting in the
1918 * false-negative result. d_lookup() protects against concurrent
1919 * renames using rename_lock seqlock.
1920 *
1921 * See Documentation/filesystems/path-lookup.txt for more details.
1922 */
1923 rcu_read_lock();
1924
1925 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
1926 const char *tname;
1927 int tlen;
1928
1929 if (dentry->d_name.hash != hash)
1930 continue;
1931
1932 spin_lock(&dentry->d_lock);
1933 if (dentry->d_parent != parent)
1934 goto next;
1935 if (d_unhashed(dentry))
1936 goto next;
1937
1938 /*
1939 * It is safe to compare names since d_move() cannot
1940 * change the qstr (protected by d_lock).
1941 */
1942 tlen = dentry->d_name.len;
1943 tname = dentry->d_name.name;
1944 if (parent->d_flags & DCACHE_OP_COMPARE) {
1945 if (parent->d_op->d_compare(parent, parent->d_inode,
1946 dentry, dentry->d_inode,
1947 tlen, tname, name))
1948 goto next;
1949 } else {
1950 if (dentry_cmp(tname, tlen, str, len))
1951 goto next;
1952 }
1953
1954 dentry->d_count++;
1955 found = dentry;
1956 spin_unlock(&dentry->d_lock);
1957 break;
1958next:
1959 spin_unlock(&dentry->d_lock);
1960 }
1961 rcu_read_unlock();
1962
1963 return found;
1964}
1965
1966/**
1967 * d_hash_and_lookup - hash the qstr then search for a dentry
1968 * @dir: Directory to search in
1969 * @name: qstr of name we wish to find
1970 *
1971 * On hash failure or on lookup failure NULL is returned.
1972 */
1973struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1974{
1975 struct dentry *dentry = NULL;
1976
1977 /*
1978 * Check for a fs-specific hash function. Note that we must
1979 * calculate the standard hash first, as the d_op->d_hash()
1980 * routine may choose to leave the hash value unchanged.
1981 */
1982 name->hash = full_name_hash(name->name, name->len);
1983 if (dir->d_flags & DCACHE_OP_HASH) {
1984 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1985 goto out;
1986 }
1987 dentry = d_lookup(dir, name);
1988out:
1989 return dentry;
1990}
1991
1992/**
1993 * d_validate - verify dentry provided from insecure source (deprecated)
1994 * @dentry: The dentry alleged to be valid child of @dparent
1995 * @dparent: The parent dentry (known to be valid)
1996 *
1997 * An insecure source has sent us a dentry, here we verify it and dget() it.
1998 * This is used by ncpfs in its readdir implementation.
1999 * Zero is returned in the dentry is invalid.
2000 *
2001 * This function is slow for big directories, and deprecated, do not use it.
2002 */
2003int d_validate(struct dentry *dentry, struct dentry *dparent)
2004{
2005 struct dentry *child;
2006
2007 spin_lock(&dparent->d_lock);
2008 list_for_each_entry(child, &dparent->d_subdirs, d_child) {
2009 if (dentry == child) {
2010 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2011 __dget_dlock(dentry);
2012 spin_unlock(&dentry->d_lock);
2013 spin_unlock(&dparent->d_lock);
2014 return 1;
2015 }
2016 }
2017 spin_unlock(&dparent->d_lock);
2018
2019 return 0;
2020}
2021EXPORT_SYMBOL(d_validate);
2022
2023/*
2024 * When a file is deleted, we have two options:
2025 * - turn this dentry into a negative dentry
2026 * - unhash this dentry and free it.
2027 *
2028 * Usually, we want to just turn this into
2029 * a negative dentry, but if anybody else is
2030 * currently using the dentry or the inode
2031 * we can't do that and we fall back on removing
2032 * it from the hash queues and waiting for
2033 * it to be deleted later when it has no users
2034 */
2035
2036/**
2037 * d_delete - delete a dentry
2038 * @dentry: The dentry to delete
2039 *
2040 * Turn the dentry into a negative dentry if possible, otherwise
2041 * remove it from the hash queues so it can be deleted later
2042 */
2043
2044void d_delete(struct dentry * dentry)
2045{
2046 struct inode *inode;
2047 int isdir = 0;
2048 /*
2049 * Are we the only user?
2050 */
2051again:
2052 spin_lock(&dentry->d_lock);
2053 inode = dentry->d_inode;
2054 isdir = S_ISDIR(inode->i_mode);
2055 if (dentry->d_count == 1) {
2056 if (inode && !spin_trylock(&inode->i_lock)) {
2057 spin_unlock(&dentry->d_lock);
2058 cpu_chill();
2059 goto again;
2060 }
2061 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2062 dentry_unlink_inode(dentry);
2063 fsnotify_nameremove(dentry, isdir);
2064 return;
2065 }
2066
2067 if (!d_unhashed(dentry))
2068 __d_drop(dentry);
2069
2070 spin_unlock(&dentry->d_lock);
2071
2072 fsnotify_nameremove(dentry, isdir);
2073}
2074EXPORT_SYMBOL(d_delete);
2075
2076static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
2077{
2078 BUG_ON(!d_unhashed(entry));
2079 hlist_bl_lock(b);
2080 entry->d_flags |= DCACHE_RCUACCESS;
2081 hlist_bl_add_head_rcu(&entry->d_hash, b);
2082 hlist_bl_unlock(b);
2083}
2084
2085static void _d_rehash(struct dentry * entry)
2086{
2087 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2088}
2089
2090/**
2091 * d_rehash - add an entry back to the hash
2092 * @entry: dentry to add to the hash
2093 *
2094 * Adds a dentry to the hash according to its name.
2095 */
2096
2097void d_rehash(struct dentry * entry)
2098{
2099 spin_lock(&entry->d_lock);
2100 _d_rehash(entry);
2101 spin_unlock(&entry->d_lock);
2102}
2103EXPORT_SYMBOL(d_rehash);
2104
2105/**
2106 * dentry_update_name_case - update case insensitive dentry with a new name
2107 * @dentry: dentry to be updated
2108 * @name: new name
2109 *
2110 * Update a case insensitive dentry with new case of name.
2111 *
2112 * dentry must have been returned by d_lookup with name @name. Old and new
2113 * name lengths must match (ie. no d_compare which allows mismatched name
2114 * lengths).
2115 *
2116 * Parent inode i_mutex must be held over d_lookup and into this call (to
2117 * keep renames and concurrent inserts, and readdir(2) away).
2118 */
2119void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2120{
2121 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
2122 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2123
2124 spin_lock(&dentry->d_lock);
2125 write_seqcount_begin(&dentry->d_seq);
2126 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
2127 write_seqcount_end(&dentry->d_seq);
2128 spin_unlock(&dentry->d_lock);
2129}
2130EXPORT_SYMBOL(dentry_update_name_case);
2131
2132static void switch_names(struct dentry *dentry, struct dentry *target)
2133{
2134 if (dname_external(target)) {
2135 if (dname_external(dentry)) {
2136 /*
2137 * Both external: swap the pointers
2138 */
2139 swap(target->d_name.name, dentry->d_name.name);
2140 } else {
2141 /*
2142 * dentry:internal, target:external. Steal target's
2143 * storage and make target internal.
2144 */
2145 memcpy(target->d_iname, dentry->d_name.name,
2146 dentry->d_name.len + 1);
2147 dentry->d_name.name = target->d_name.name;
2148 target->d_name.name = target->d_iname;
2149 }
2150 } else {
2151 if (dname_external(dentry)) {
2152 /*
2153 * dentry:external, target:internal. Give dentry's
2154 * storage to target and make dentry internal
2155 */
2156 memcpy(dentry->d_iname, target->d_name.name,
2157 target->d_name.len + 1);
2158 target->d_name.name = dentry->d_name.name;
2159 dentry->d_name.name = dentry->d_iname;
2160 } else {
2161 /*
2162 * Both are internal. Just copy target to dentry
2163 */
2164 memcpy(dentry->d_iname, target->d_name.name,
2165 target->d_name.len + 1);
2166 dentry->d_name.len = target->d_name.len;
2167 return;
2168 }
2169 }
2170 swap(dentry->d_name.len, target->d_name.len);
2171}
2172
2173static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2174{
2175 /*
2176 * XXXX: do we really need to take target->d_lock?
2177 */
2178 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2179 spin_lock(&target->d_parent->d_lock);
2180 else {
2181 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2182 spin_lock(&dentry->d_parent->d_lock);
2183 spin_lock_nested(&target->d_parent->d_lock,
2184 DENTRY_D_LOCK_NESTED);
2185 } else {
2186 spin_lock(&target->d_parent->d_lock);
2187 spin_lock_nested(&dentry->d_parent->d_lock,
2188 DENTRY_D_LOCK_NESTED);
2189 }
2190 }
2191 if (target < dentry) {
2192 spin_lock_nested(&target->d_lock, 2);
2193 spin_lock_nested(&dentry->d_lock, 3);
2194 } else {
2195 spin_lock_nested(&dentry->d_lock, 2);
2196 spin_lock_nested(&target->d_lock, 3);
2197 }
2198}
2199
2200static void dentry_unlock_parents_for_move(struct dentry *dentry,
2201 struct dentry *target)
2202{
2203 if (target->d_parent != dentry->d_parent)
2204 spin_unlock(&dentry->d_parent->d_lock);
2205 if (target->d_parent != target)
2206 spin_unlock(&target->d_parent->d_lock);
2207}
2208
2209/*
2210 * When switching names, the actual string doesn't strictly have to
2211 * be preserved in the target - because we're dropping the target
2212 * anyway. As such, we can just do a simple memcpy() to copy over
2213 * the new name before we switch.
2214 *
2215 * Note that we have to be a lot more careful about getting the hash
2216 * switched - we have to switch the hash value properly even if it
2217 * then no longer matches the actual (corrupted) string of the target.
2218 * The hash value has to match the hash queue that the dentry is on..
2219 */
2220/*
2221 * __d_move - move a dentry
2222 * @dentry: entry to move
2223 * @target: new dentry
2224 *
2225 * Update the dcache to reflect the move of a file name. Negative
2226 * dcache entries should not be moved in this way. Caller must hold
2227 * rename_lock, the i_mutex of the source and target directories,
2228 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
2229 */
2230static void __d_move(struct dentry * dentry, struct dentry * target)
2231{
2232 if (!dentry->d_inode)
2233 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2234
2235 BUG_ON(d_ancestor(dentry, target));
2236 BUG_ON(d_ancestor(target, dentry));
2237
2238 dentry_lock_for_move(dentry, target);
2239
2240 write_seqcount_begin(&dentry->d_seq);
2241 write_seqcount_begin(&target->d_seq);
2242
2243 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2244
2245 /*
2246 * Move the dentry to the target hash queue. Don't bother checking
2247 * for the same hash queue because of how unlikely it is.
2248 */
2249 __d_drop(dentry);
2250 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2251
2252 /* Unhash the target: dput() will then get rid of it */
2253 __d_drop(target);
2254
2255 list_del(&dentry->d_child);
2256 list_del(&target->d_child);
2257
2258 /* Switch the names.. */
2259 switch_names(dentry, target);
2260 swap(dentry->d_name.hash, target->d_name.hash);
2261
2262 /* ... and switch the parents */
2263 if (IS_ROOT(dentry)) {
2264 dentry->d_parent = target->d_parent;
2265 target->d_parent = target;
2266 INIT_LIST_HEAD(&target->d_child);
2267 } else {
2268 swap(dentry->d_parent, target->d_parent);
2269
2270 /* And add them back to the (new) parent lists */
2271 list_add(&target->d_child, &target->d_parent->d_subdirs);
2272 }
2273
2274 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
2275
2276 write_seqcount_end(&target->d_seq);
2277 write_seqcount_end(&dentry->d_seq);
2278
2279 dentry_unlock_parents_for_move(dentry, target);
2280 spin_unlock(&target->d_lock);
2281 fsnotify_d_move(dentry);
2282 spin_unlock(&dentry->d_lock);
2283}
2284
2285/*
2286 * d_move - move a dentry
2287 * @dentry: entry to move
2288 * @target: new dentry
2289 *
2290 * Update the dcache to reflect the move of a file name. Negative
2291 * dcache entries should not be moved in this way. See the locking
2292 * requirements for __d_move.
2293 */
2294void d_move(struct dentry *dentry, struct dentry *target)
2295{
2296 write_seqlock(&rename_lock);
2297 __d_move(dentry, target);
2298 write_sequnlock(&rename_lock);
2299}
2300EXPORT_SYMBOL(d_move);
2301
2302/**
2303 * d_ancestor - search for an ancestor
2304 * @p1: ancestor dentry
2305 * @p2: child dentry
2306 *
2307 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2308 * an ancestor of p2, else NULL.
2309 */
2310struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2311{
2312 struct dentry *p;
2313
2314 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2315 if (p->d_parent == p1)
2316 return p;
2317 }
2318 return NULL;
2319}
2320
2321/*
2322 * This helper attempts to cope with remotely renamed directories
2323 *
2324 * It assumes that the caller is already holding
2325 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
2326 *
2327 * Note: If ever the locking in lock_rename() changes, then please
2328 * remember to update this too...
2329 */
2330static struct dentry *__d_unalias(struct inode *inode,
2331 struct dentry *dentry, struct dentry *alias)
2332{
2333 struct mutex *m1 = NULL, *m2 = NULL;
2334 struct dentry *ret;
2335
2336 /* If alias and dentry share a parent, then no extra locks required */
2337 if (alias->d_parent == dentry->d_parent)
2338 goto out_unalias;
2339
2340 /* See lock_rename() */
2341 ret = ERR_PTR(-EBUSY);
2342 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2343 goto out_err;
2344 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2345 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2346 goto out_err;
2347 m2 = &alias->d_parent->d_inode->i_mutex;
2348out_unalias:
2349 __d_move(alias, dentry);
2350 ret = alias;
2351out_err:
2352 spin_unlock(&inode->i_lock);
2353 if (m2)
2354 mutex_unlock(m2);
2355 if (m1)
2356 mutex_unlock(m1);
2357 return ret;
2358}
2359
2360/*
2361 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2362 * named dentry in place of the dentry to be replaced.
2363 * returns with anon->d_lock held!
2364 */
2365static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2366{
2367 struct dentry *dparent, *aparent;
2368
2369 dentry_lock_for_move(anon, dentry);
2370
2371 write_seqcount_begin(&dentry->d_seq);
2372 write_seqcount_begin(&anon->d_seq);
2373
2374 dparent = dentry->d_parent;
2375 aparent = anon->d_parent;
2376
2377 switch_names(dentry, anon);
2378 swap(dentry->d_name.hash, anon->d_name.hash);
2379
2380 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2381 list_del(&dentry->d_child);
2382 if (!IS_ROOT(dentry))
2383 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
2384 else
2385 INIT_LIST_HEAD(&dentry->d_child);
2386
2387 anon->d_parent = (dparent == dentry) ? anon : dparent;
2388 list_del(&anon->d_child);
2389 if (!IS_ROOT(anon))
2390 list_add(&anon->d_child, &anon->d_parent->d_subdirs);
2391 else
2392 INIT_LIST_HEAD(&anon->d_child);
2393
2394 write_seqcount_end(&dentry->d_seq);
2395 write_seqcount_end(&anon->d_seq);
2396
2397 dentry_unlock_parents_for_move(anon, dentry);
2398 spin_unlock(&dentry->d_lock);
2399
2400 /* anon->d_lock still locked, returns locked */
2401 anon->d_flags &= ~DCACHE_DISCONNECTED;
2402}
2403
2404/**
2405 * d_materialise_unique - introduce an inode into the tree
2406 * @dentry: candidate dentry
2407 * @inode: inode to bind to the dentry, to which aliases may be attached
2408 *
2409 * Introduces an dentry into the tree, substituting an extant disconnected
2410 * root directory alias in its place if there is one. Caller must hold the
2411 * i_mutex of the parent directory.
2412 */
2413struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2414{
2415 struct dentry *actual;
2416
2417 BUG_ON(!d_unhashed(dentry));
2418
2419 if (!inode) {
2420 actual = dentry;
2421 __d_instantiate(dentry, NULL);
2422 d_rehash(actual);
2423 goto out_nolock;
2424 }
2425
2426 spin_lock(&inode->i_lock);
2427
2428 if (S_ISDIR(inode->i_mode)) {
2429 struct dentry *alias;
2430
2431 /* Does an aliased dentry already exist? */
2432 alias = __d_find_alias(inode, 0);
2433 if (alias) {
2434 actual = alias;
2435 write_seqlock(&rename_lock);
2436
2437 if (d_ancestor(alias, dentry)) {
2438 /* Check for loops */
2439 actual = ERR_PTR(-ELOOP);
2440 spin_unlock(&inode->i_lock);
2441 } else if (IS_ROOT(alias)) {
2442 /* Is this an anonymous mountpoint that we
2443 * could splice into our tree? */
2444 __d_materialise_dentry(dentry, alias);
2445 write_sequnlock(&rename_lock);
2446 __d_drop(alias);
2447 goto found;
2448 } else {
2449 /* Nope, but we must(!) avoid directory
2450 * aliasing. This drops inode->i_lock */
2451 actual = __d_unalias(inode, dentry, alias);
2452 }
2453 write_sequnlock(&rename_lock);
2454 if (IS_ERR(actual)) {
2455 if (PTR_ERR(actual) == -ELOOP)
2456 pr_warn_ratelimited(
2457 "VFS: Lookup of '%s' in %s %s"
2458 " would have caused loop\n",
2459 dentry->d_name.name,
2460 inode->i_sb->s_type->name,
2461 inode->i_sb->s_id);
2462 dput(alias);
2463 }
2464 goto out_nolock;
2465 }
2466 }
2467
2468 /* Add a unique reference */
2469 actual = __d_instantiate_unique(dentry, inode);
2470 if (!actual)
2471 actual = dentry;
2472 else
2473 BUG_ON(!d_unhashed(actual));
2474
2475 spin_lock(&actual->d_lock);
2476found:
2477 _d_rehash(actual);
2478 spin_unlock(&actual->d_lock);
2479 spin_unlock(&inode->i_lock);
2480out_nolock:
2481 if (actual == dentry) {
2482 security_d_instantiate(dentry, inode);
2483 return NULL;
2484 }
2485
2486 iput(inode);
2487 return actual;
2488}
2489EXPORT_SYMBOL_GPL(d_materialise_unique);
2490
2491static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2492{
2493 *buflen -= namelen;
2494 if (*buflen < 0)
2495 return -ENAMETOOLONG;
2496 *buffer -= namelen;
2497 memcpy(*buffer, str, namelen);
2498 return 0;
2499}
2500
2501static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2502{
2503 return prepend(buffer, buflen, name->name, name->len);
2504}
2505
2506/**
2507 * prepend_path - Prepend path string to a buffer
2508 * @path: the dentry/vfsmount to report
2509 * @root: root vfsmnt/dentry
2510 * @buffer: pointer to the end of the buffer
2511 * @buflen: pointer to buffer length
2512 *
2513 * Caller holds the rename_lock.
2514 */
2515static int prepend_path(const struct path *path,
2516 const struct path *root,
2517 char **buffer, int *buflen)
2518{
2519 struct dentry *dentry = path->dentry;
2520 struct vfsmount *vfsmnt = path->mnt;
2521 struct mount *mnt = real_mount(vfsmnt);
2522 char *orig_buffer = *buffer;
2523 int orig_len = *buflen;
2524 bool slash = false;
2525 int error = 0;
2526
2527 while (dentry != root->dentry || vfsmnt != root->mnt) {
2528 struct dentry * parent;
2529
2530 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2531 /* Escaped? */
2532 if (dentry != vfsmnt->mnt_root) {
2533 *buffer = orig_buffer;
2534 *buflen = orig_len;
2535 slash = false;
2536 error = 3;
2537 goto global_root;
2538 }
2539 /* Global root? */
2540 if (!mnt_has_parent(mnt))
2541 goto global_root;
2542 dentry = mnt->mnt_mountpoint;
2543 mnt = mnt->mnt_parent;
2544 vfsmnt = &mnt->mnt;
2545 continue;
2546 }
2547 parent = dentry->d_parent;
2548 prefetch(parent);
2549 spin_lock(&dentry->d_lock);
2550 error = prepend_name(buffer, buflen, &dentry->d_name);
2551 spin_unlock(&dentry->d_lock);
2552 if (!error)
2553 error = prepend(buffer, buflen, "/", 1);
2554 if (error)
2555 break;
2556
2557 slash = true;
2558 dentry = parent;
2559 }
2560
2561 if (!error && !slash)
2562 error = prepend(buffer, buflen, "/", 1);
2563
2564 return error;
2565
2566global_root:
2567 /*
2568 * Filesystems needing to implement special "root names"
2569 * should do so with ->d_dname()
2570 */
2571 if (IS_ROOT(dentry) &&
2572 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2573 WARN(1, "Root dentry has weird name <%.*s>\n",
2574 (int) dentry->d_name.len, dentry->d_name.name);
2575 }
2576 if (!slash)
2577 error = prepend(buffer, buflen, "/", 1);
2578 if (!error)
2579 error = real_mount(vfsmnt)->mnt_ns ? 1 : 2;
2580 return error;
2581}
2582
2583/**
2584 * __d_path - return the path of a dentry
2585 * @path: the dentry/vfsmount to report
2586 * @root: root vfsmnt/dentry
2587 * @buf: buffer to return value in
2588 * @buflen: buffer length
2589 *
2590 * Convert a dentry into an ASCII path name.
2591 *
2592 * Returns a pointer into the buffer or an error code if the
2593 * path was too long.
2594 *
2595 * "buflen" should be positive.
2596 *
2597 * If the path is not reachable from the supplied root, return %NULL.
2598 */
2599char *__d_path(const struct path *path,
2600 const struct path *root,
2601 char *buf, int buflen)
2602{
2603 char *res = buf + buflen;
2604 int error;
2605
2606 prepend(&res, &buflen, "\0", 1);
2607 br_read_lock(vfsmount_lock);
2608 write_seqlock(&rename_lock);
2609 error = prepend_path(path, root, &res, &buflen);
2610 write_sequnlock(&rename_lock);
2611 br_read_unlock(vfsmount_lock);
2612
2613 if (error < 0)
2614 return ERR_PTR(error);
2615 if (error > 0)
2616 return NULL;
2617 return res;
2618}
2619
2620char *d_absolute_path(const struct path *path,
2621 char *buf, int buflen)
2622{
2623 struct path root = {};
2624 char *res = buf + buflen;
2625 int error;
2626
2627 prepend(&res, &buflen, "\0", 1);
2628 br_read_lock(vfsmount_lock);
2629 write_seqlock(&rename_lock);
2630 error = prepend_path(path, &root, &res, &buflen);
2631 write_sequnlock(&rename_lock);
2632 br_read_unlock(vfsmount_lock);
2633
2634 if (error > 1)
2635 error = -EINVAL;
2636 if (error < 0)
2637 return ERR_PTR(error);
2638 return res;
2639}
2640
2641/*
2642 * same as __d_path but appends "(deleted)" for unlinked files.
2643 */
2644static int path_with_deleted(const struct path *path,
2645 const struct path *root,
2646 char **buf, int *buflen)
2647{
2648 prepend(buf, buflen, "\0", 1);
2649 if (d_unlinked(path->dentry)) {
2650 int error = prepend(buf, buflen, " (deleted)", 10);
2651 if (error)
2652 return error;
2653 }
2654
2655 return prepend_path(path, root, buf, buflen);
2656}
2657
2658static int prepend_unreachable(char **buffer, int *buflen)
2659{
2660 return prepend(buffer, buflen, "(unreachable)", 13);
2661}
2662
2663/**
2664 * d_path - return the path of a dentry
2665 * @path: path to report
2666 * @buf: buffer to return value in
2667 * @buflen: buffer length
2668 *
2669 * Convert a dentry into an ASCII path name. If the entry has been deleted
2670 * the string " (deleted)" is appended. Note that this is ambiguous.
2671 *
2672 * Returns a pointer into the buffer or an error code if the path was
2673 * too long. Note: Callers should use the returned pointer, not the passed
2674 * in buffer, to use the name! The implementation often starts at an offset
2675 * into the buffer, and may leave 0 bytes at the start.
2676 *
2677 * "buflen" should be positive.
2678 */
2679char *d_path(const struct path *path, char *buf, int buflen)
2680{
2681 char *res = buf + buflen;
2682 struct path root;
2683 int error;
2684
2685 /*
2686 * We have various synthetic filesystems that never get mounted. On
2687 * these filesystems dentries are never used for lookup purposes, and
2688 * thus don't need to be hashed. They also don't need a name until a
2689 * user wants to identify the object in /proc/pid/fd/. The little hack
2690 * below allows us to generate a name for these objects on demand:
2691 */
2692 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2693 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2694
2695 get_fs_root(current->fs, &root);
2696 br_read_lock(vfsmount_lock);
2697 write_seqlock(&rename_lock);
2698 error = path_with_deleted(path, &root, &res, &buflen);
2699 write_sequnlock(&rename_lock);
2700 br_read_unlock(vfsmount_lock);
2701 if (error < 0)
2702 res = ERR_PTR(error);
2703 path_put(&root);
2704 return res;
2705}
2706EXPORT_SYMBOL(d_path);
2707
2708/**
2709 * d_path_with_unreachable - return the path of a dentry
2710 * @path: path to report
2711 * @buf: buffer to return value in
2712 * @buflen: buffer length
2713 *
2714 * The difference from d_path() is that this prepends "(unreachable)"
2715 * to paths which are unreachable from the current process' root.
2716 */
2717char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2718{
2719 char *res = buf + buflen;
2720 struct path root;
2721 int error;
2722
2723 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2724 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2725
2726 get_fs_root(current->fs, &root);
2727 write_seqlock(&rename_lock);
2728 error = path_with_deleted(path, &root, &res, &buflen);
2729 if (error > 0)
2730 error = prepend_unreachable(&res, &buflen);
2731 write_sequnlock(&rename_lock);
2732 path_put(&root);
2733 if (error)
2734 res = ERR_PTR(error);
2735
2736 return res;
2737}
2738
2739/*
2740 * Helper function for dentry_operations.d_dname() members
2741 */
2742char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2743 const char *fmt, ...)
2744{
2745 va_list args;
2746 char temp[64];
2747 int sz;
2748
2749 va_start(args, fmt);
2750 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2751 va_end(args);
2752
2753 if (sz > sizeof(temp) || sz > buflen)
2754 return ERR_PTR(-ENAMETOOLONG);
2755
2756 buffer += buflen - sz;
2757 return memcpy(buffer, temp, sz);
2758}
2759
2760/*
2761 * Write full pathname from the root of the filesystem into the buffer.
2762 */
2763static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2764{
2765 char *end = buf + buflen;
2766 char *retval;
2767
2768 prepend(&end, &buflen, "\0", 1);
2769 if (buflen < 1)
2770 goto Elong;
2771 /* Get '/' right */
2772 retval = end-1;
2773 *retval = '/';
2774
2775 while (!IS_ROOT(dentry)) {
2776 struct dentry *parent = dentry->d_parent;
2777 int error;
2778
2779 prefetch(parent);
2780 spin_lock(&dentry->d_lock);
2781 error = prepend_name(&end, &buflen, &dentry->d_name);
2782 spin_unlock(&dentry->d_lock);
2783 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
2784 goto Elong;
2785
2786 retval = end;
2787 dentry = parent;
2788 }
2789 return retval;
2790Elong:
2791 return ERR_PTR(-ENAMETOOLONG);
2792}
2793
2794char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2795{
2796 char *retval;
2797
2798 write_seqlock(&rename_lock);
2799 retval = __dentry_path(dentry, buf, buflen);
2800 write_sequnlock(&rename_lock);
2801
2802 return retval;
2803}
2804EXPORT_SYMBOL(dentry_path_raw);
2805
2806char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2807{
2808 char *p = NULL;
2809 char *retval;
2810
2811 write_seqlock(&rename_lock);
2812 if (d_unlinked(dentry)) {
2813 p = buf + buflen;
2814 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2815 goto Elong;
2816 buflen++;
2817 }
2818 retval = __dentry_path(dentry, buf, buflen);
2819 write_sequnlock(&rename_lock);
2820 if (!IS_ERR(retval) && p)
2821 *p = '/'; /* restore '/' overriden with '\0' */
2822 return retval;
2823Elong:
2824 return ERR_PTR(-ENAMETOOLONG);
2825}
2826
2827/*
2828 * NOTE! The user-level library version returns a
2829 * character pointer. The kernel system call just
2830 * returns the length of the buffer filled (which
2831 * includes the ending '\0' character), or a negative
2832 * error value. So libc would do something like
2833 *
2834 * char *getcwd(char * buf, size_t size)
2835 * {
2836 * int retval;
2837 *
2838 * retval = sys_getcwd(buf, size);
2839 * if (retval >= 0)
2840 * return buf;
2841 * errno = -retval;
2842 * return NULL;
2843 * }
2844 */
2845SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2846{
2847 int error;
2848 struct path pwd, root;
2849 char *page = (char *) __get_free_page(GFP_USER);
2850
2851 if (!page)
2852 return -ENOMEM;
2853
2854 get_fs_root_and_pwd(current->fs, &root, &pwd);
2855
2856 error = -ENOENT;
2857 br_read_lock(vfsmount_lock);
2858 write_seqlock(&rename_lock);
2859 if (!d_unlinked(pwd.dentry)) {
2860 unsigned long len;
2861 char *cwd = page + PAGE_SIZE;
2862 int buflen = PAGE_SIZE;
2863
2864 prepend(&cwd, &buflen, "\0", 1);
2865 error = prepend_path(&pwd, &root, &cwd, &buflen);
2866 write_sequnlock(&rename_lock);
2867 br_read_unlock(vfsmount_lock);
2868
2869 if (error < 0)
2870 goto out;
2871
2872 /* Unreachable from current root */
2873 if (error > 0) {
2874 error = prepend_unreachable(&cwd, &buflen);
2875 if (error)
2876 goto out;
2877 }
2878
2879 error = -ERANGE;
2880 len = PAGE_SIZE + page - cwd;
2881 if (len <= size) {
2882 error = len;
2883 if (copy_to_user(buf, cwd, len))
2884 error = -EFAULT;
2885 }
2886 } else {
2887 write_sequnlock(&rename_lock);
2888 br_read_unlock(vfsmount_lock);
2889 }
2890
2891out:
2892 path_put(&pwd);
2893 path_put(&root);
2894 free_page((unsigned long) page);
2895 return error;
2896}
2897
2898/*
2899 * Test whether new_dentry is a subdirectory of old_dentry.
2900 *
2901 * Trivially implemented using the dcache structure
2902 */
2903
2904/**
2905 * is_subdir - is new dentry a subdirectory of old_dentry
2906 * @new_dentry: new dentry
2907 * @old_dentry: old dentry
2908 *
2909 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2910 * Returns 0 otherwise.
2911 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2912 */
2913
2914int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2915{
2916 int result;
2917 unsigned seq;
2918
2919 if (new_dentry == old_dentry)
2920 return 1;
2921
2922 do {
2923 /* for restarting inner loop in case of seq retry */
2924 seq = read_seqbegin(&rename_lock);
2925 /*
2926 * Need rcu_readlock to protect against the d_parent trashing
2927 * due to d_move
2928 */
2929 rcu_read_lock();
2930 if (d_ancestor(old_dentry, new_dentry))
2931 result = 1;
2932 else
2933 result = 0;
2934 rcu_read_unlock();
2935 } while (read_seqretry(&rename_lock, seq));
2936
2937 return result;
2938}
2939
2940void d_genocide(struct dentry *root)
2941{
2942 struct dentry *this_parent;
2943 struct list_head *next;
2944 unsigned seq;
2945 int locked = 0;
2946
2947 seq = read_seqbegin(&rename_lock);
2948again:
2949 this_parent = root;
2950 spin_lock(&this_parent->d_lock);
2951repeat:
2952 next = this_parent->d_subdirs.next;
2953resume:
2954 while (next != &this_parent->d_subdirs) {
2955 struct list_head *tmp = next;
2956 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
2957 next = tmp->next;
2958
2959 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2960 if (d_unhashed(dentry) || !dentry->d_inode) {
2961 spin_unlock(&dentry->d_lock);
2962 continue;
2963 }
2964 if (!list_empty(&dentry->d_subdirs)) {
2965 spin_unlock(&this_parent->d_lock);
2966 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
2967 this_parent = dentry;
2968 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
2969 goto repeat;
2970 }
2971 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2972 dentry->d_flags |= DCACHE_GENOCIDE;
2973 dentry->d_count--;
2974 }
2975 spin_unlock(&dentry->d_lock);
2976 }
2977 rcu_read_lock();
2978ascend:
2979 if (this_parent != root) {
2980 struct dentry *child = this_parent;
2981 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2982 this_parent->d_flags |= DCACHE_GENOCIDE;
2983 this_parent->d_count--;
2984 }
2985 this_parent = child->d_parent;
2986
2987 spin_unlock(&child->d_lock);
2988 spin_lock(&this_parent->d_lock);
2989
2990 /* might go back up the wrong parent if we have had a rename */
2991 if (!locked && read_seqretry(&rename_lock, seq))
2992 goto rename_retry;
2993 next = child->d_child.next;
2994 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
2995 if (next == &this_parent->d_subdirs)
2996 goto ascend;
2997 child = list_entry(next, struct dentry, d_child);
2998 next = next->next;
2999 }
3000 rcu_read_unlock();
3001 goto resume;
3002 }
3003 if (!locked && read_seqretry(&rename_lock, seq))
3004 goto rename_retry;
3005 spin_unlock(&this_parent->d_lock);
3006 rcu_read_unlock();
3007 if (locked)
3008 write_sequnlock(&rename_lock);
3009 return;
3010
3011rename_retry:
3012 spin_unlock(&this_parent->d_lock);
3013 rcu_read_unlock();
3014 if (locked)
3015 goto again;
3016 locked = 1;
3017 write_seqlock(&rename_lock);
3018 goto again;
3019}
3020
3021/**
3022 * find_inode_number - check for dentry with name
3023 * @dir: directory to check
3024 * @name: Name to find.
3025 *
3026 * Check whether a dentry already exists for the given name,
3027 * and return the inode number if it has an inode. Otherwise
3028 * 0 is returned.
3029 *
3030 * This routine is used to post-process directory listings for
3031 * filesystems using synthetic inode numbers, and is necessary
3032 * to keep getcwd() working.
3033 */
3034
3035ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3036{
3037 struct dentry * dentry;
3038 ino_t ino = 0;
3039
3040 dentry = d_hash_and_lookup(dir, name);
3041 if (dentry) {
3042 if (dentry->d_inode)
3043 ino = dentry->d_inode->i_ino;
3044 dput(dentry);
3045 }
3046 return ino;
3047}
3048EXPORT_SYMBOL(find_inode_number);
3049
3050static __initdata unsigned long dhash_entries;
3051static int __init set_dhash_entries(char *str)
3052{
3053 if (!str)
3054 return 0;
3055 dhash_entries = simple_strtoul(str, &str, 0);
3056 return 1;
3057}
3058__setup("dhash_entries=", set_dhash_entries);
3059
3060static void __init dcache_init_early(void)
3061{
3062 unsigned int loop;
3063
3064 /* If hashes are distributed across NUMA nodes, defer
3065 * hash allocation until vmalloc space is available.
3066 */
3067 if (hashdist)
3068 return;
3069
3070 dentry_hashtable =
3071 alloc_large_system_hash("Dentry cache",
3072 sizeof(struct hlist_bl_head),
3073 dhash_entries,
3074 13,
3075 HASH_EARLY,
3076 &d_hash_shift,
3077 &d_hash_mask,
3078 0);
3079
3080 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3081 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3082}
3083
3084static void __init dcache_init(void)
3085{
3086 unsigned int loop;
3087
3088 /*
3089 * A constructor could be added for stable state like the lists,
3090 * but it is probably not worth it because of the cache nature
3091 * of the dcache.
3092 */
3093 dentry_cache = KMEM_CACHE(dentry,
3094 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
3095
3096 /* Hash may have been set up in dcache_init_early */
3097 if (!hashdist)
3098 return;
3099
3100 dentry_hashtable =
3101 alloc_large_system_hash("Dentry cache",
3102 sizeof(struct hlist_bl_head),
3103 dhash_entries,
3104 13,
3105 0,
3106 &d_hash_shift,
3107 &d_hash_mask,
3108 0);
3109
3110 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3111 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3112}
3113
3114/* SLAB cache for __getname() consumers */
3115struct kmem_cache *names_cachep __read_mostly;
3116EXPORT_SYMBOL(names_cachep);
3117
3118EXPORT_SYMBOL(d_genocide);
3119
3120void __init vfs_caches_init_early(void)
3121{
3122 dcache_init_early();
3123 inode_init_early();
3124}
3125
3126void __init vfs_caches_init(unsigned long mempages)
3127{
3128 unsigned long reserve;
3129
3130 /* Base hash sizes on available memory, with a reserve equal to
3131 150% of current kernel size */
3132
3133 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3134 mempages -= reserve;
3135
3136 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
3137 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
3138
3139 dcache_init();
3140 inode_init();
3141 files_init(mempages);
3142 mnt_init();
3143 bdev_cache_init();
3144 chrdev_init();
3145}