Lines Matching refs:dentry

46  * dentry->d_sb->s_dentry_lru_lock protects:
59 * dentry->d_inode->i_lock
60 * dentry->d_lock
61 * dentry->d_sb->s_dentry_lru_lock
66 * dentry->d_parent->...->d_parent->d_lock
68 * dentry->d_parent->d_lock
69 * dentry->d_lock
109 static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
186 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
231 static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
242 * and a dentry name is guaranteed to be properly
249 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
262 static inline struct external_name *external_name(struct dentry *dentry)
264 return container_of(dentry->d_name.name, struct external_name, name[0]);
269 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
271 kmem_cache_free(dentry_cache, dentry);
276 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
277 kfree(external_name(dentry));
278 kmem_cache_free(dentry_cache, dentry);
281 static inline int dname_external(const struct dentry *dentry)
283 return dentry->d_name.name != dentry->d_iname;
286 void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
288 spin_lock(&dentry->d_lock);
289 name->name = dentry->d_name;
290 if (unlikely(dname_external(dentry))) {
291 atomic_inc(&external_name(dentry)->u.count);
293 memcpy(name->inline_name, dentry->d_iname,
294 dentry->d_name.len + 1);
297 spin_unlock(&dentry->d_lock);
312 static inline void __d_set_inode_and_type(struct dentry *dentry,
318 dentry->d_inode = inode;
319 flags = READ_ONCE(dentry->d_flags);
322 smp_store_release(&dentry->d_flags, flags);
325 static inline void __d_clear_type_and_inode(struct dentry *dentry)
327 unsigned flags = READ_ONCE(dentry->d_flags);
330 WRITE_ONCE(dentry->d_flags, flags);
331 dentry->d_inode = NULL;
332 if (dentry->d_flags & DCACHE_LRU_LIST)
336 static void dentry_free(struct dentry *dentry)
338 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
339 if (unlikely(dname_external(dentry))) {
340 struct external_name *p = external_name(dentry);
342 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
346 /* if dentry was never visible to RCU, immediate free is OK */
347 if (dentry->d_flags & DCACHE_NORCU)
348 __d_free(&dentry->d_u.d_rcu);
350 call_rcu(&dentry->d_u.d_rcu, __d_free);
354 * Release the dentry's inode, using the filesystem
357 static void dentry_unlink_inode(struct dentry * dentry)
358 __releases(dentry->d_lock)
359 __releases(dentry->d_inode->i_lock)
361 struct inode *inode = dentry->d_inode;
363 raw_write_seqcount_begin(&dentry->d_seq);
364 __d_clear_type_and_inode(dentry);
365 hlist_del_init(&dentry->d_u.d_alias);
366 raw_write_seqcount_end(&dentry->d_seq);
367 spin_unlock(&dentry->d_lock);
371 if (dentry->d_op && dentry->d_op->d_iput)
372 dentry->d_op->d_iput(dentry, inode);
382 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
396 #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
397 static void d_lru_add(struct dentry *dentry)
399 D_FLAG_VERIFY(dentry, 0);
400 dentry->d_flags |= DCACHE_LRU_LIST;
402 if (d_is_negative(dentry))
404 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
407 static void d_lru_del(struct dentry *dentry)
409 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
410 dentry->d_flags &= ~DCACHE_LRU_LIST;
412 if (d_is_negative(dentry))
414 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
417 static void d_shrink_del(struct dentry *dentry)
419 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
420 list_del_init(&dentry->d_lru);
421 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
425 static void d_shrink_add(struct dentry *dentry, struct list_head *list)
427 D_FLAG_VERIFY(dentry, 0);
428 list_add(&dentry->d_lru, list);
429 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
439 static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
441 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
442 dentry->d_flags &= ~DCACHE_LRU_LIST;
444 if (d_is_negative(dentry))
446 list_lru_isolate(lru, &dentry->d_lru);
449 static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
452 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
453 dentry->d_flags |= DCACHE_SHRINK_LIST;
454 if (d_is_negative(dentry))
456 list_lru_isolate_move(lru, &dentry->d_lru, list);
460 * d_drop - drop a dentry
461 * @dentry: dentry to drop
463 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
465 * deleting the dentry - d_delete will try to mark the dentry negative if
469 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
472 * __d_drop requires dentry->d_lock
473 * ___d_drop doesn't mark dentry as "unhashed"
474 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
476 static void ___d_drop(struct dentry *dentry)
480 * Hashed dentries are normally on the dentry hashtable,
484 if (unlikely(IS_ROOT(dentry)))
485 b = &dentry->d_sb->s_roots;
487 b = d_hash(dentry->d_name.hash);
490 __hlist_bl_del(&dentry->d_hash);
494 void __d_drop(struct dentry *dentry)
496 if (!d_unhashed(dentry)) {
497 ___d_drop(dentry);
498 dentry->d_hash.pprev = NULL;
499 write_seqcount_invalidate(&dentry->d_seq);
504 void d_drop(struct dentry *dentry)
506 spin_lock(&dentry->d_lock);
507 __d_drop(dentry);
508 spin_unlock(&dentry->d_lock);
512 static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
514 struct dentry *next;
517 * attached to the dentry tree
519 dentry->d_flags |= DCACHE_DENTRY_KILLED;
520 if (unlikely(list_empty(&dentry->d_child)))
522 __list_del_entry(&dentry->d_child);
542 while (dentry->d_child.next != &parent->d_subdirs) {
543 next = list_entry(dentry->d_child.next, struct dentry, d_child);
546 dentry->d_child.next = next->d_child.next;
550 static void __dentry_kill(struct dentry *dentry)
552 struct dentry *parent = NULL;
554 if (!IS_ROOT(dentry))
555 parent = dentry->d_parent;
558 * The dentry is now unrecoverably dead to the world.
560 lockref_mark_dead(&dentry->d_lockref);
563 * inform the fs via d_prune that this dentry is about to be
566 if (dentry->d_flags & DCACHE_OP_PRUNE)
567 dentry->d_op->d_prune(dentry);
569 if (dentry->d_flags & DCACHE_LRU_LIST) {
570 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
571 d_lru_del(dentry);
574 __d_drop(dentry);
575 dentry_unlist(dentry, parent);
578 if (dentry->d_inode)
579 dentry_unlink_inode(dentry);
581 spin_unlock(&dentry->d_lock);
583 if (dentry->d_op && dentry->d_op->d_release)
584 dentry->d_op->d_release(dentry);
586 spin_lock(&dentry->d_lock);
587 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
588 dentry->d_flags |= DCACHE_MAY_FREE;
591 spin_unlock(&dentry->d_lock);
593 dentry_free(dentry);
597 static struct dentry *__lock_parent(struct dentry *dentry)
599 struct dentry *parent;
601 spin_unlock(&dentry->d_lock);
603 parent = READ_ONCE(dentry->d_parent);
606 * We can't blindly lock dentry until we are sure
608 * Any changes of dentry->d_parent must have
613 if (unlikely(parent != dentry->d_parent)) {
618 if (parent != dentry)
619 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
625 static inline struct dentry *lock_parent(struct dentry *dentry)
627 struct dentry *parent = dentry->d_parent;
628 if (IS_ROOT(dentry))
632 return __lock_parent(dentry);
635 static inline bool retain_dentry(struct dentry *dentry)
637 WARN_ON(d_in_lookup(dentry));
640 if (unlikely(d_unhashed(dentry)))
643 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
646 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
647 if (dentry->d_op->d_delete(dentry))
651 if (unlikely(dentry->d_flags & DCACHE_DONTCACHE))
655 dentry->d_lockref.count--;
656 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
657 d_lru_add(dentry);
658 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
659 dentry->d_flags |= DCACHE_REFERENCED;
665 struct dentry *de;
679 * Finish off a dentry we've decided to kill.
680 * dentry->d_lock must be held, returns with it unlocked.
681 * Returns dentry requiring refcount drop, or NULL if we're done.
683 static struct dentry *dentry_kill(struct dentry *dentry)
684 __releases(dentry->d_lock)
686 struct inode *inode = dentry->d_inode;
687 struct dentry *parent = NULL;
692 if (!IS_ROOT(dentry)) {
693 parent = dentry->d_parent;
695 parent = __lock_parent(dentry);
696 if (likely(inode || !dentry->d_inode))
701 inode = dentry->d_inode;
705 __dentry_kill(dentry);
709 spin_unlock(&dentry->d_lock);
711 spin_lock(&dentry->d_lock);
712 parent = lock_parent(dentry);
714 if (unlikely(dentry->d_lockref.count != 1)) {
715 dentry->d_lockref.count--;
716 } else if (likely(!retain_dentry(dentry))) {
717 __dentry_kill(dentry);
725 spin_unlock(&dentry->d_lock);
732 * If unsuccessful, we return false, having already taken the dentry lock.
734 * The caller needs to hold the RCU read lock, so that the dentry is
737 static inline bool fast_dput(struct dentry *dentry)
744 * let the dentry count go to zero, so use "put_or_lock".
746 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
747 return lockref_put_or_lock(&dentry->d_lockref);
753 ret = lockref_put_return(&dentry->d_lockref);
761 spin_lock(&dentry->d_lock);
762 if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) {
763 spin_unlock(&dentry->d_lock);
766 dentry->d_lockref.count--;
778 * to zero, but we don't hold the dentry lock, so
784 * do: the dentry is still hashed, it does not have
791 * dentry was hashed and the flags had the proper
792 * value. Other dentry users may have re-gotten
793 * a reference to the dentry and change that, but
794 * our work is done - we can leave the dentry
798 d_flags = READ_ONCE(dentry->d_flags);
802 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
810 spin_lock(&dentry->d_lock);
819 if (dentry->d_lockref.count) {
820 spin_unlock(&dentry->d_lock);
829 dentry->d_lockref.count = 1;
841 * However, that implies that we have to traverse the dentry
852 * dput - release a dentry
853 * @dentry: dentry to release
855 * Release a dentry. This will drop the usage count and if appropriate
856 * call the dentry unlink method as well as removing it from the queues and
860 void dput(struct dentry *dentry)
862 while (dentry) {
866 if (likely(fast_dput(dentry))) {
871 /* Slow case: now with the dentry lock held */
874 if (likely(retain_dentry(dentry))) {
875 spin_unlock(&dentry->d_lock);
879 dentry = dentry_kill(dentry);
884 static void __dput_to_list(struct dentry *dentry, struct list_head *list)
885 __must_hold(&dentry->d_lock)
887 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
889 --dentry->d_lockref.count;
891 if (dentry->d_flags & DCACHE_LRU_LIST)
892 d_lru_del(dentry);
893 if (!--dentry->d_lockref.count)
894 d_shrink_add(dentry, list);
898 void dput_to_list(struct dentry *dentry, struct list_head *list)
901 if (likely(fast_dput(dentry))) {
906 if (!retain_dentry(dentry))
907 __dput_to_list(dentry, list);
908 spin_unlock(&dentry->d_lock);
912 static inline void __dget_dlock(struct dentry *dentry)
914 dentry->d_lockref.count++;
917 static inline void __dget(struct dentry *dentry)
919 lockref_get(&dentry->d_lockref);
922 struct dentry *dget_parent(struct dentry *dentry)
925 struct dentry *ret;
933 seq = raw_seqcount_begin(&dentry->d_seq);
934 ret = READ_ONCE(dentry->d_parent);
938 if (!read_seqcount_retry(&dentry->d_seq, seq))
949 ret = dentry->d_parent;
951 if (unlikely(ret != dentry->d_parent)) {
964 static struct dentry * __d_find_any_alias(struct inode *inode)
966 struct dentry *alias;
970 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
982 struct dentry *d_find_any_alias(struct inode *inode)
984 struct dentry *de;
1007 static struct dentry *__d_find_alias(struct inode *inode)
1009 struct dentry *alias;
1026 struct dentry *d_find_alias(struct inode *inode)
1028 struct dentry *de = NULL;
1045 struct dentry *dentry;
1048 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1049 spin_lock(&dentry->d_lock);
1050 if (!dentry->d_lockref.count) {
1051 struct dentry *parent = lock_parent(dentry);
1052 if (likely(!dentry->d_lockref.count)) {
1053 __dentry_kill(dentry);
1060 spin_unlock(&dentry->d_lock);
1067 * Lock a dentry from shrink list.
1068 * Called under rcu_read_lock() and dentry->d_lock; the former
1070 * Note that dentry is *not* protected from concurrent dentry_kill(),
1073 * Return false if dentry has been disrupted or grabbed, leaving
1075 * that dentry's inode and parent both locked.
1077 static bool shrink_lock_dentry(struct dentry *dentry)
1080 struct dentry *parent;
1082 if (dentry->d_lockref.count)
1085 inode = dentry->d_inode;
1087 spin_unlock(&dentry->d_lock);
1089 spin_lock(&dentry->d_lock);
1090 if (unlikely(dentry->d_lockref.count))
1093 if (unlikely(inode != dentry->d_inode))
1097 parent = dentry->d_parent;
1098 if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1101 spin_unlock(&dentry->d_lock);
1103 if (unlikely(parent != dentry->d_parent)) {
1105 spin_lock(&dentry->d_lock);
1108 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1109 if (likely(!dentry->d_lockref.count))
1121 struct dentry *dentry, *parent;
1123 dentry = list_entry(list->prev, struct dentry, d_lru);
1124 spin_lock(&dentry->d_lock);
1126 if (!shrink_lock_dentry(dentry)) {
1129 d_shrink_del(dentry);
1130 if (dentry->d_lockref.count < 0)
1131 can_free = dentry->d_flags & DCACHE_MAY_FREE;
1132 spin_unlock(&dentry->d_lock);
1134 dentry_free(dentry);
1138 d_shrink_del(dentry);
1139 parent = dentry->d_parent;
1140 if (parent != dentry)
1142 __dentry_kill(dentry);
1150 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1154 * we are inverting the lru lock/dentry->d_lock here,
1158 if (!spin_trylock(&dentry->d_lock))
1166 if (dentry->d_lockref.count) {
1167 d_lru_isolate(lru, dentry);
1168 spin_unlock(&dentry->d_lock);
1172 if (dentry->d_flags & DCACHE_REFERENCED) {
1173 dentry->d_flags &= ~DCACHE_REFERENCED;
1174 spin_unlock(&dentry->d_lock);
1178 * this point, we've dropped the dentry->d_lock but keep the
1198 d_lru_shrink_move(lru, dentry, freeable);
1199 spin_unlock(&dentry->d_lock);
1231 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1234 * we are inverting the lru lock/dentry->d_lock here,
1238 if (!spin_trylock(&dentry->d_lock))
1241 d_lru_shrink_move(lru, dentry, freeable);
1242 spin_unlock(&dentry->d_lock);
1272 * @D_WALK_SKIP: skip this dentry and its children
1282 * d_walk - walk the dentry tree
1285 * @enter: callback when first entering the dentry
1289 static void d_walk(struct dentry *parent, void *data,
1290 enum d_walk_ret (*enter)(void *, struct dentry *))
1292 struct dentry *this_parent;
1319 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1322 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1325 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1327 ret = enter(data, dentry);
1332 spin_unlock(&dentry->d_lock);
1338 spin_unlock(&dentry->d_lock);
1342 if (!list_empty(&dentry->d_subdirs)) {
1344 spin_release(&dentry->d_lock.dep_map, _RET_IP_);
1345 this_parent = dentry;
1349 spin_unlock(&dentry->d_lock);
1357 struct dentry *child = this_parent;
1371 child = list_entry(next, struct dentry, d_child);
1400 static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1403 struct path path = { .mnt = info->mnt, .dentry = dentry };
1405 if (likely(!d_mountpoint(dentry)))
1415 * path_has_submounts - check for mounts over a dentry in the
1427 d_walk(parent->dentry, &data, path_check_mount);
1436 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1440 * this reason take rename_lock and d_lock on dentry and ancestors.
1442 int d_set_mounted(struct dentry *dentry)
1444 struct dentry *p;
1447 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1456 spin_lock(&dentry->d_lock);
1457 if (!d_unlinked(dentry)) {
1459 if (!d_mountpoint(dentry)) {
1460 dentry->d_flags |= DCACHE_MOUNTED;
1464 spin_unlock(&dentry->d_lock);
1471 * Search the dentry child list of the specified parent,
1486 struct dentry *start;
1489 struct dentry *victim;
1494 static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1499 if (data->start == dentry)
1502 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1505 if (dentry->d_flags & DCACHE_LRU_LIST)
1506 d_lru_del(dentry);
1507 if (!dentry->d_lockref.count) {
1508 d_shrink_add(dentry, &data->dispose);
1523 static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1528 if (data->start == dentry)
1531 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1532 if (!dentry->d_lockref.count) {
1534 data->victim = dentry;
1538 if (dentry->d_flags & DCACHE_LRU_LIST)
1539 d_lru_del(dentry);
1540 if (!dentry->d_lockref.count)
1541 d_shrink_add(dentry, &data->dispose);
1558 * Prune the dcache to remove unused children of the parent dentry.
1560 void shrink_dcache_parent(struct dentry *parent)
1579 struct dentry *parent;
1598 static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1601 if (!list_empty(&dentry->d_subdirs))
1605 if (dentry == _data && dentry->d_lockref.count == 1)
1610 dentry,
1611 dentry->d_inode ?
1612 dentry->d_inode->i_ino : 0UL,
1613 dentry,
1614 dentry->d_lockref.count,
1615 dentry->d_sb->s_type->name,
1616 dentry->d_sb->s_id);
1621 static void do_one_tree(struct dentry *dentry)
1623 shrink_dcache_parent(dentry);
1624 d_walk(dentry, dentry, umount_check);
1625 d_drop(dentry);
1626 dput(dentry);
1634 struct dentry *dentry;
1638 dentry = sb->s_root;
1640 do_one_tree(dentry);
1643 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
1644 do_one_tree(dentry);
1648 static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
1650 struct dentry **victim = _data;
1651 if (d_mountpoint(dentry)) {
1652 __dget_dlock(dentry);
1653 *victim = dentry;
1661 * @dentry: dentry to invalidate (aka detach, prune and drop)
1663 void d_invalidate(struct dentry *dentry)
1666 spin_lock(&dentry->d_lock);
1667 if (d_unhashed(dentry)) {
1668 spin_unlock(&dentry->d_lock);
1671 __d_drop(dentry);
1672 spin_unlock(&dentry->d_lock);
1675 if (!dentry->d_inode)
1678 shrink_dcache_parent(dentry);
1680 struct dentry *victim = NULL;
1681 d_walk(dentry, &victim, find_submount);
1684 shrink_dcache_parent(dentry);
1699 * Allocates a dentry. It returns %NULL if there is insufficient memory
1700 * available. On a success the dentry is returned. The name passed in is
1704 static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1706 struct dentry *dentry;
1710 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1711 if (!dentry)
1720 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1723 dname = dentry->d_iname;
1730 kmem_cache_free(dentry_cache, dentry);
1736 dname = dentry->d_iname;
1739 dentry->d_name.len = name->len;
1740 dentry->d_name.hash = name->hash;
1745 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
1747 dentry->d_lockref.count = 1;
1748 dentry->d_flags = 0;
1749 spin_lock_init(&dentry->d_lock);
1750 seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
1751 dentry->d_inode = NULL;
1752 dentry->d_parent = dentry;
1753 dentry->d_sb = sb;
1754 dentry->d_op = NULL;
1755 dentry->d_fsdata = NULL;
1756 INIT_HLIST_BL_NODE(&dentry->d_hash);
1757 INIT_LIST_HEAD(&dentry->d_lru);
1758 INIT_LIST_HEAD(&dentry->d_subdirs);
1759 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1760 INIT_LIST_HEAD(&dentry->d_child);
1761 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1763 if (dentry->d_op && dentry->d_op->d_init) {
1764 err = dentry->d_op->d_init(dentry);
1766 if (dname_external(dentry))
1767 kfree(external_name(dentry));
1768 kmem_cache_free(dentry_cache, dentry);
1775 return dentry;
1783 * Allocates a dentry. It returns %NULL if there is insufficient memory
1784 * available. On a success the dentry is returned. The name passed in is
1787 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1789 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1790 if (!dentry)
1798 dentry->d_parent = parent;
1799 list_add(&dentry->d_child, &parent->d_subdirs);
1802 return dentry;
1806 struct dentry *d_alloc_anon(struct super_block *sb)
1812 struct dentry *d_alloc_cursor(struct dentry * parent)
1814 struct dentry *dentry = d_alloc_anon(parent->d_sb);
1815 if (dentry) {
1816 dentry->d_flags |= DCACHE_DENTRY_CURSOR;
1817 dentry->d_parent = dget(parent);
1819 return dentry;
1823 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1828 * performs lookups at all, return an unhashed IS_ROOT dentry.
1837 struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1839 struct dentry *dentry = __d_alloc(sb, name);
1840 if (likely(dentry))
1841 dentry->d_flags |= DCACHE_NORCU;
1842 return dentry;
1845 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1855 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1857 WARN_ON_ONCE(dentry->d_op);
1858 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1864 dentry->d_op = op;
1868 dentry->d_flags |= DCACHE_OP_HASH;
1870 dentry->d_flags |= DCACHE_OP_COMPARE;
1872 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1874 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1876 dentry->d_flags |= DCACHE_OP_DELETE;
1878 dentry->d_flags |= DCACHE_OP_PRUNE;
1880 dentry->d_flags |= DCACHE_OP_REAL;
1887 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1888 * @dentry - The dentry to mark
1890 * Mark a dentry as falling through to the lower layer (as set with
1893 void d_set_fallthru(struct dentry *dentry)
1895 spin_lock(&dentry->d_lock);
1896 dentry->d_flags |= DCACHE_FALLTHRU;
1897 spin_unlock(&dentry->d_lock);
1936 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1939 WARN_ON(d_in_lookup(dentry));
1941 spin_lock(&dentry->d_lock);
1943 * Decrement negative dentry count if it was in the LRU list.
1945 if (dentry->d_flags & DCACHE_LRU_LIST)
1947 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1948 raw_write_seqcount_begin(&dentry->d_seq);
1949 __d_set_inode_and_type(dentry, inode, add_flags);
1950 raw_write_seqcount_end(&dentry->d_seq);
1951 fsnotify_update_flags(dentry);
1952 spin_unlock(&dentry->d_lock);
1956 * d_instantiate - fill in inode information for a dentry
1957 * @entry: dentry to complete
1958 * @inode: inode to attach to this dentry
1970 void d_instantiate(struct dentry *entry, struct inode * inode)
1988 void d_instantiate_new(struct dentry *entry, struct inode *inode)
2004 struct dentry *d_make_root(struct inode *root_inode)
2006 struct dentry *res = NULL;
2019 static struct dentry *__d_instantiate_anon(struct dentry *dentry,
2023 struct dentry *res;
2026 security_d_instantiate(dentry, inode);
2031 dput(dentry);
2035 /* attach a disconnected dentry */
2041 spin_lock(&dentry->d_lock);
2042 __d_set_inode_and_type(dentry, inode, add_flags);
2043 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2045 hlist_bl_lock(&dentry->d_sb->s_roots);
2046 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
2047 hlist_bl_unlock(&dentry->d_sb->s_roots);
2049 spin_unlock(&dentry->d_lock);
2052 return dentry;
2059 struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2061 return __d_instantiate_anon(dentry, inode, true);
2065 static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
2067 struct dentry *tmp;
2068 struct dentry *res;
2093 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2094 * @inode: inode to allocate the dentry for
2096 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2097 * similar open by handle operations. The returned dentry may be anonymous,
2101 * has one dentry. If a dentry is found, that is returned instead of
2105 * to the dentry. In case of an error the reference on the inode is released.
2110 struct dentry *d_obtain_alias(struct inode *inode)
2117 * d_obtain_root - find or allocate a dentry for a given inode
2118 * @inode: inode to allocate the dentry for
2120 * Obtain an IS_ROOT dentry for the root of a filesystem.
2122 * We must ensure that directory inodes only ever have one dentry. If a
2123 * dentry is found, that is returned instead of allocating a new one.
2126 * to the dentry. In case of an error the reference on the inode is
2131 struct dentry *d_obtain_root(struct inode *inode)
2138 * d_add_ci - lookup or allocate new dentry with case-exact name
2140 * @dentry: the negative dentry that was passed to the parent's lookup func
2141 * @name: the case-exact name to be associated with the returned dentry
2147 * For a case-insensitive lookup match and if the the case-exact dentry
2150 * If no entry exists with the exact case name, allocate new dentry with
2153 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2156 struct dentry *found, *res;
2159 * First check if a dentry matching the name already exists,
2162 found = d_hash_and_lookup(dentry->d_parent, name);
2167 if (d_in_lookup(dentry)) {
2168 found = d_alloc_parallel(dentry->d_parent, name,
2169 dentry->d_wait);
2175 found = d_alloc(dentry->d_parent, name);
2191 static inline bool d_same_name(const struct dentry *dentry,
2192 const struct dentry *parent,
2196 if (dentry->d_name.len != name->len)
2198 return dentry_cmp(dentry, name->name, name->len) == 0;
2200 return parent->d_op->d_compare(dentry,
2201 dentry->d_name.len, dentry->d_name.name,
2206 * __d_lookup_rcu - search for a dentry (racy, store-free)
2207 * @parent: parent dentry
2209 * @seqp: returns d_seq value at the point where the dentry was found
2210 * Returns: dentry, or NULL
2219 * held, and rcu_read_lock held. The returned dentry must not be stored into
2223 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2227 * the returned dentry, so long as its parent's seqlock is checked after the
2231 * NOTE! The caller *has* to check the resulting dentry against the sequence
2232 * number we've returned before using any of the resulting dentry state!
2234 struct dentry *__d_lookup_rcu(const struct dentry *parent,
2242 struct dentry *dentry;
2254 * Carefully use d_seq when comparing a candidate dentry, to avoid
2258 * walk here and result in missing our dentry, resulting in the
2264 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2269 * The dentry sequence count protects us from concurrent
2273 * to do anything useful with the returned dentry.
2278 * dentry compare, we will do seqretries until it is stable,
2285 seq = raw_seqcount_begin(&dentry->d_seq);
2286 if (dentry->d_parent != parent)
2288 if (d_unhashed(dentry))
2294 if (dentry->d_name.hash != hashlen_hash(hashlen))
2296 tlen = dentry->d_name.len;
2297 tname = dentry->d_name.name;
2299 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2303 if (parent->d_op->d_compare(dentry,
2307 if (dentry->d_name.hash_len != hashlen)
2309 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2313 return dentry;
2319 * d_lookup - search for a dentry
2320 * @parent: parent dentry
2322 * Returns: dentry, or NULL
2324 * d_lookup searches the children of the parent dentry for the name in
2325 * question. If the dentry is found its reference count is incremented and the
2326 * dentry is returned. The caller must use dput to free the entry when it has
2327 * finished using it. %NULL is returned if the dentry does not exist.
2329 struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2331 struct dentry *dentry;
2336 dentry = __d_lookup(parent, name);
2337 if (dentry)
2340 return dentry;
2345 * __d_lookup - search for a dentry (racy)
2346 * @parent: parent dentry
2348 * Returns: dentry, or NULL
2359 struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2364 struct dentry *found = NULL;
2365 struct dentry *dentry;
2377 * Take d_lock when comparing a candidate dentry, to avoid races
2381 * walk here and result in missing our dentry, resulting in the
2389 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2391 if (dentry->d_name.hash != hash)
2394 spin_lock(&dentry->d_lock);
2395 if (dentry->d_parent != parent)
2397 if (d_unhashed(dentry))
2400 if (!d_same_name(dentry, parent, name))
2403 dentry->d_lockref.count++;
2404 found = dentry;
2405 spin_unlock(&dentry->d_lock);
2408 spin_unlock(&dentry->d_lock);
2416 * d_hash_and_lookup - hash the qstr then search for a dentry
2422 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2441 * - turn this dentry into a negative dentry
2442 * - unhash this dentry and free it.
2445 * a negative dentry, but if anybody else is
2446 * currently using the dentry or the inode
2453 * d_delete - delete a dentry
2454 * @dentry: The dentry to delete
2456 * Turn the dentry into a negative dentry if possible, otherwise
2460 void d_delete(struct dentry * dentry)
2462 struct inode *inode = dentry->d_inode;
2465 spin_lock(&dentry->d_lock);
2469 if (dentry->d_lockref.count == 1) {
2470 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2471 dentry_unlink_inode(dentry);
2473 __d_drop(dentry);
2474 spin_unlock(&dentry->d_lock);
2480 static void __d_rehash(struct dentry *entry)
2491 * @entry: dentry to add to the hash
2493 * Adds a dentry to the hash according to its name.
2496 void d_rehash(struct dentry * entry)
2520 static void d_wait_lookup(struct dentry *dentry)
2522 if (d_in_lookup(dentry)) {
2524 add_wait_queue(dentry->d_wait, &wait);
2527 spin_unlock(&dentry->d_lock);
2529 spin_lock(&dentry->d_lock);
2530 } while (d_in_lookup(dentry));
2534 struct dentry *d_alloc_parallel(struct dentry *parent,
2541 struct dentry *new = d_alloc(parent, name);
2542 struct dentry *dentry;
2552 dentry = __d_lookup_rcu(parent, name, &d_seq);
2553 if (unlikely(dentry)) {
2554 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2558 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2560 dput(dentry);
2565 return dentry;
2590 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2591 if (dentry->d_name.hash != hash)
2593 if (dentry->d_parent != parent)
2595 if (!d_same_name(dentry, parent, name))
2599 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2609 spin_lock(&dentry->d_lock);
2610 d_wait_lookup(dentry);
2617 if (unlikely(dentry->d_name.hash != hash))
2619 if (unlikely(dentry->d_parent != parent))
2621 if (unlikely(d_unhashed(dentry)))
2623 if (unlikely(!d_same_name(dentry, parent, name)))
2626 spin_unlock(&dentry->d_lock);
2628 return dentry;
2638 spin_unlock(&dentry->d_lock);
2639 dput(dentry);
2644 void __d_lookup_done(struct dentry *dentry)
2646 struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
2647 dentry->d_name.hash);
2649 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
2650 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
2651 wake_up_all(dentry->d_wait);
2652 dentry->d_wait = NULL;
2654 INIT_HLIST_NODE(&dentry->d_u.d_alias);
2655 INIT_LIST_HEAD(&dentry->d_lru);
2661 static inline void __d_add(struct dentry *dentry, struct inode *inode)
2665 spin_lock(&dentry->d_lock);
2666 if (unlikely(d_in_lookup(dentry))) {
2667 dir = dentry->d_parent->d_inode;
2669 __d_lookup_done(dentry);
2673 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2674 raw_write_seqcount_begin(&dentry->d_seq);
2675 __d_set_inode_and_type(dentry, inode, add_flags);
2676 raw_write_seqcount_end(&dentry->d_seq);
2677 fsnotify_update_flags(dentry);
2679 __d_rehash(dentry);
2682 spin_unlock(&dentry->d_lock);
2688 * d_add - add dentry to hash queues
2689 * @entry: dentry to add
2690 * @inode: The inode to attach to this dentry
2696 void d_add(struct dentry *entry, struct inode *inode)
2708 * @entry: dentry to add
2709 * @inode: The inode to go with this dentry
2711 * If an unhashed dentry with the same name/parent and desired
2717 struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2719 struct dentry *alias;
2752 static void swap_names(struct dentry *dentry, struct dentry *target)
2755 if (unlikely(dname_external(dentry))) {
2759 swap(target->d_name.name, dentry->d_name.name);
2762 * dentry:internal, target:external. Steal target's
2765 memcpy(target->d_iname, dentry->d_name.name,
2766 dentry->d_name.len + 1);
2767 dentry->d_name.name = target->d_name.name;
2771 if (unlikely(dname_external(dentry))) {
2773 * dentry:external, target:internal. Give dentry's
2774 * storage to target and make dentry internal
2776 memcpy(dentry->d_iname, target->d_name.name,
2778 target->d_name.name = dentry->d_name.name;
2779 dentry->d_name.name = dentry->d_iname;
2787 swap(((long *) &dentry->d_iname)[i],
2792 swap(dentry->d_name.hash_len, target->d_name.hash_len);
2795 static void copy_name(struct dentry *dentry, struct dentry *target)
2798 if (unlikely(dname_external(dentry)))
2799 old_name = external_name(dentry);
2802 dentry->d_name = target->d_name;
2804 memcpy(dentry->d_iname, target->d_name.name,
2806 dentry->d_name.name = dentry->d_iname;
2807 dentry->d_name.hash_len = target->d_name.hash_len;
2814 * __d_move - move a dentry
2815 * @dentry: entry to move
2816 * @target: new dentry
2824 static void __d_move(struct dentry *dentry, struct dentry *target,
2827 struct dentry *old_parent, *p;
2831 WARN_ON(!dentry->d_inode);
2832 if (WARN_ON(dentry == target))
2835 BUG_ON(d_ancestor(target, dentry));
2836 old_parent = dentry->d_parent;
2838 if (IS_ROOT(dentry)) {
2842 /* target is not a descendent of dentry->d_parent */
2846 BUG_ON(p == dentry);
2852 spin_lock_nested(&dentry->d_lock, 2);
2861 write_seqcount_begin(&dentry->d_seq);
2865 if (!d_unhashed(dentry))
2866 ___d_drop(dentry);
2871 dentry->d_parent = target->d_parent;
2873 copy_name(dentry, target);
2875 dentry->d_parent->d_lockref.count++;
2876 if (dentry != old_parent) /* wasn't IS_ROOT */
2880 swap_names(dentry, target);
2885 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
2886 __d_rehash(dentry);
2887 fsnotify_update_flags(dentry);
2888 fscrypt_handle_d_move(dentry);
2891 write_seqcount_end(&dentry->d_seq);
2896 if (dentry->d_parent != old_parent)
2897 spin_unlock(&dentry->d_parent->d_lock);
2898 if (dentry != old_parent)
2901 spin_unlock(&dentry->d_lock);
2905 * d_move - move a dentry
2906 * @dentry: entry to move
2907 * @target: new dentry
2913 void d_move(struct dentry *dentry, struct dentry *target)
2916 __d_move(dentry, target, false);
2923 * @dentry1: first dentry
2924 * @dentry2: second dentry
2926 void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2942 * @p1: ancestor dentry
2943 * @p2: child dentry
2945 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2948 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2950 struct dentry *p;
2963 * dentry->d_parent->d_inode->i_mutex, and rename_lock
2969 struct dentry *dentry, struct dentry *alias)
2975 /* If alias and dentry share a parent, then no extra locks required */
2976 if (alias->d_parent == dentry->d_parent)
2980 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2982 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2987 __d_move(alias, dentry, false);
2998 * d_splice_alias - splice a disconnected dentry into the tree if one exists
2999 * @inode: the inode which may have a disconnected dentry
3000 * @dentry: a negative dentry which we want to point to the inode.
3003 * place of the given dentry and return it, else simply d_add the inode
3004 * to the dentry and return NULL.
3012 * If a dentry was found and moved, then it is returned. Otherwise NULL
3015 * Cluster filesystems may call this function with a negative, hashed dentry.
3017 * will only occur during atomic_open. So we need to check for the dentry
3020 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3025 BUG_ON(!d_unhashed(dentry));
3030 security_d_instantiate(dentry, inode);
3033 struct dentry *new = __d_find_any_alias(inode);
3038 if (unlikely(d_ancestor(new, dentry))) {
3045 dentry->d_name.name,
3049 struct dentry *old_parent = dget(new->d_parent);
3050 int err = __d_unalias(inode, dentry, new);
3058 __d_move(new, dentry, false);
3066 __d_add(dentry, inode);
3078 * is_subdir - is new dentry a subdirectory of old_dentry
3079 * @new_dentry: new dentry
3080 * @old_dentry: old dentry
3087 bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3114 static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3116 struct dentry *root = data;
3117 if (dentry != root) {
3118 if (d_unhashed(dentry) || !dentry->d_inode)
3121 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3122 dentry->d_flags |= DCACHE_GENOCIDE;
3123 dentry->d_lockref.count--;
3129 void d_genocide(struct dentry *parent)
3136 void d_tmpfile(struct dentry *dentry, struct inode *inode)
3139 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3140 !hlist_unhashed(&dentry->d_u.d_alias) ||
3141 !d_unlinked(dentry));
3142 spin_lock(&dentry->d_parent->d_lock);
3143 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3144 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3146 spin_unlock(&dentry->d_lock);
3147 spin_unlock(&dentry->d_parent->d_lock);
3148 d_instantiate(dentry, inode);
3190 dentry_cache = KMEM_CACHE_USERCOPY(dentry,