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
111 static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
188 .procname = "dentry-state",
212 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
257 static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
268 * and a dentry name is guaranteed to be properly
275 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
288 static inline struct external_name *external_name(struct dentry *dentry)
290 return container_of(dentry->d_name.name, struct external_name, name[0]);
295 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
297 kmem_cache_free(dentry_cache, dentry);
302 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
303 kfree(external_name(dentry));
304 kmem_cache_free(dentry_cache, dentry);
307 static inline int dname_external(const struct dentry *dentry)
309 return dentry->d_name.name != dentry->d_iname;
312 void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
314 spin_lock(&dentry->d_lock);
315 name->name = dentry->d_name;
316 if (unlikely(dname_external(dentry))) {
317 atomic_inc(&external_name(dentry)->u.count);
319 memcpy(name->inline_name, dentry->d_iname,
320 dentry->d_name.len + 1);
323 spin_unlock(&dentry->d_lock);
338 static inline void __d_set_inode_and_type(struct dentry *dentry,
344 dentry->d_inode = inode;
345 flags = READ_ONCE(dentry->d_flags);
348 smp_store_release(&dentry->d_flags, flags);
351 static inline void __d_clear_type_and_inode(struct dentry *dentry)
353 unsigned flags = READ_ONCE(dentry->d_flags);
356 WRITE_ONCE(dentry->d_flags, flags);
357 dentry->d_inode = NULL;
358 if (dentry->d_flags & DCACHE_LRU_LIST)
362 static void dentry_free(struct dentry *dentry)
364 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
365 if (unlikely(dname_external(dentry))) {
366 struct external_name *p = external_name(dentry);
368 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
372 /* if dentry was never visible to RCU, immediate free is OK */
373 if (dentry->d_flags & DCACHE_NORCU)
374 __d_free(&dentry->d_u.d_rcu);
376 call_rcu(&dentry->d_u.d_rcu, __d_free);
380 * Release the dentry's inode, using the filesystem
383 static void dentry_unlink_inode(struct dentry * dentry)
384 __releases(dentry->d_lock)
385 __releases(dentry->d_inode->i_lock)
387 struct inode *inode = dentry->d_inode;
389 raw_write_seqcount_begin(&dentry->d_seq);
390 __d_clear_type_and_inode(dentry);
391 hlist_del_init(&dentry->d_u.d_alias);
392 raw_write_seqcount_end(&dentry->d_seq);
393 spin_unlock(&dentry->d_lock);
397 if (dentry->d_op && dentry->d_op->d_iput)
398 dentry->d_op->d_iput(dentry, inode);
408 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
422 #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
423 static void d_lru_add(struct dentry *dentry)
425 D_FLAG_VERIFY(dentry, 0);
426 dentry->d_flags |= DCACHE_LRU_LIST;
428 if (d_is_negative(dentry))
431 &dentry->d_sb->s_dentry_lru, &dentry->d_lru));
434 static void d_lru_del(struct dentry *dentry)
436 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
437 dentry->d_flags &= ~DCACHE_LRU_LIST;
439 if (d_is_negative(dentry))
442 &dentry->d_sb->s_dentry_lru, &dentry->d_lru));
445 static void d_shrink_del(struct dentry *dentry)
447 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
448 list_del_init(&dentry->d_lru);
449 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
453 static void d_shrink_add(struct dentry *dentry, struct list_head *list)
455 D_FLAG_VERIFY(dentry, 0);
456 list_add(&dentry->d_lru, list);
457 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
467 static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
469 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
470 dentry->d_flags &= ~DCACHE_LRU_LIST;
472 if (d_is_negative(dentry))
474 list_lru_isolate(lru, &dentry->d_lru);
477 static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
480 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
481 dentry->d_flags |= DCACHE_SHRINK_LIST;
482 if (d_is_negative(dentry))
484 list_lru_isolate_move(lru, &dentry->d_lru, list);
487 static void ___d_drop(struct dentry *dentry)
491 * Hashed dentries are normally on the dentry hashtable,
495 if (unlikely(IS_ROOT(dentry)))
496 b = &dentry->d_sb->s_roots;
498 b = d_hash(dentry->d_name.hash);
501 __hlist_bl_del(&dentry->d_hash);
505 void __d_drop(struct dentry *dentry)
507 if (!d_unhashed(dentry)) {
508 ___d_drop(dentry);
509 dentry->d_hash.pprev = NULL;
510 write_seqcount_invalidate(&dentry->d_seq);
516 * d_drop - drop a dentry
517 * @dentry: dentry to drop
519 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
521 * deleting the dentry - d_delete will try to mark the dentry negative if
525 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
528 * __d_drop requires dentry->d_lock
530 * ___d_drop doesn't mark dentry as "unhashed"
531 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
533 void d_drop(struct dentry *dentry)
535 spin_lock(&dentry->d_lock);
536 __d_drop(dentry);
537 spin_unlock(&dentry->d_lock);
541 static inline void dentry_unlist(struct dentry *dentry)
543 struct dentry *next;
546 * attached to the dentry tree
548 dentry->d_flags |= DCACHE_DENTRY_KILLED;
549 if (unlikely(hlist_unhashed(&dentry->d_sib)))
551 __hlist_del(&dentry->d_sib);
571 while (dentry->d_sib.next) {
572 next = hlist_entry(dentry->d_sib.next, struct dentry, d_sib);
575 dentry->d_sib.next = next->d_sib.next;
579 static struct dentry *__dentry_kill(struct dentry *dentry)
581 struct dentry *parent = NULL;
585 * The dentry is now unrecoverably dead to the world.
587 lockref_mark_dead(&dentry->d_lockref);
590 * inform the fs via d_prune that this dentry is about to be
593 if (dentry->d_flags & DCACHE_OP_PRUNE)
594 dentry->d_op->d_prune(dentry);
596 if (dentry->d_flags & DCACHE_LRU_LIST) {
597 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
598 d_lru_del(dentry);
601 __d_drop(dentry);
602 if (dentry->d_inode)
603 dentry_unlink_inode(dentry);
605 spin_unlock(&dentry->d_lock);
607 if (dentry->d_op && dentry->d_op->d_release)
608 dentry->d_op->d_release(dentry);
612 if (!IS_ROOT(dentry)) {
613 parent = dentry->d_parent;
616 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
617 dentry_unlist(dentry);
618 if (dentry->d_flags & DCACHE_SHRINK_LIST)
620 spin_unlock(&dentry->d_lock);
622 dentry_free(dentry);
631 * Lock a dentry for feeding it to __dentry_kill().
632 * Called under rcu_read_lock() and dentry->d_lock; the former
634 * Note that dentry is *not* protected from concurrent dentry_kill(),
637 * Return false if dentry is busy. Otherwise, return true and have
638 * that dentry's inode locked.
641 static bool lock_for_kill(struct dentry *dentry)
643 struct inode *inode = dentry->d_inode;
645 if (unlikely(dentry->d_lockref.count))
652 spin_unlock(&dentry->d_lock);
654 spin_lock(&dentry->d_lock);
655 if (likely(inode == dentry->d_inode))
658 inode = dentry->d_inode;
660 if (likely(!dentry->d_lockref.count))
668 * Decide if dentry is worth retaining. Usually this is called with dentry
673 * sufficient that at some point after we dropped the reference the dentry was
674 * hashed and the flags had the proper value. Other dentry users may have
675 * re-gotten a reference to the dentry and change that, but our work is done -
676 * we can leave the dentry around with a zero refcount.
678 static inline bool retain_dentry(struct dentry *dentry, bool locked)
683 d_flags = READ_ONCE(dentry->d_flags);
686 if (unlikely(d_unhashed(dentry)))
696 if (!locked || dentry->d_op->d_delete(dentry))
712 d_lru_add(dentry);
716 dentry->d_flags |= DCACHE_REFERENCED;
723 struct dentry *de;
739 * If unsuccessful, we return false, having already taken the dentry lock.
743 * The caller needs to hold the RCU read lock, so that the dentry is
746 static inline bool fast_dput(struct dentry *dentry)
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--;
779 * dentry looks like it ought to be retained and there's nothing else
782 if (retain_dentry(dentry, false))
790 spin_lock(&dentry->d_lock);
799 if (dentry->d_lockref.count || retain_dentry(dentry, true)) {
800 spin_unlock(&dentry->d_lock);
814 * However, that implies that we have to traverse the dentry
825 * dput - release a dentry
826 * @dentry: dentry to release
828 * Release a dentry. This will drop the usage count and if appropriate
829 * call the dentry unlink method as well as removing it from the queues and
833 void dput(struct dentry *dentry)
835 if (!dentry)
839 if (likely(fast_dput(dentry))) {
843 while (lock_for_kill(dentry)) {
845 dentry = __dentry_kill(dentry);
846 if (!dentry)
848 if (retain_dentry(dentry, true)) {
849 spin_unlock(&dentry->d_lock);
855 spin_unlock(&dentry->d_lock);
859 static void to_shrink_list(struct dentry *dentry, struct list_head *list)
860 __must_hold(&dentry->d_lock)
862 if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
863 if (dentry->d_flags & DCACHE_LRU_LIST)
864 d_lru_del(dentry);
865 d_shrink_add(dentry, list);
869 void dput_to_list(struct dentry *dentry, struct list_head *list)
872 if (likely(fast_dput(dentry))) {
877 to_shrink_list(dentry, list);
878 spin_unlock(&dentry->d_lock);
881 struct dentry *dget_parent(struct dentry *dentry)
884 struct dentry *ret;
892 seq = raw_seqcount_begin(&dentry->d_seq);
893 ret = READ_ONCE(dentry->d_parent);
897 if (!read_seqcount_retry(&dentry->d_seq, seq))
908 ret = dentry->d_parent;
910 if (unlikely(ret != dentry->d_parent)) {
923 static struct dentry * __d_find_any_alias(struct inode *inode)
925 struct dentry *alias;
929 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
941 struct dentry *d_find_any_alias(struct inode *inode)
943 struct dentry *de;
952 static struct dentry *__d_find_alias(struct inode *inode)
954 struct dentry *alias;
985 struct dentry *d_find_alias(struct inode *inode)
987 struct dentry *de = NULL;
1002 struct dentry *d_find_alias_rcu(struct inode *inode)
1005 struct dentry *de = NULL;
1012 de = hlist_entry(l->first, struct dentry, d_u.d_alias);
1030 struct dentry *dentry;
1033 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1034 spin_lock(&dentry->d_lock);
1035 if (!dentry->d_lockref.count)
1036 to_shrink_list(dentry, &dispose);
1037 spin_unlock(&dentry->d_lock);
1044 static inline void shrink_kill(struct dentry *victim)
1059 struct dentry *dentry;
1061 dentry = list_entry(list->prev, struct dentry, d_lru);
1062 spin_lock(&dentry->d_lock);
1064 if (!lock_for_kill(dentry)) {
1067 d_shrink_del(dentry);
1068 can_free = dentry->d_flags & DCACHE_DENTRY_KILLED;
1069 spin_unlock(&dentry->d_lock);
1071 dentry_free(dentry);
1074 d_shrink_del(dentry);
1075 shrink_kill(dentry);
1083 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1087 * we are inverting the lru lock/dentry->d_lock here,
1091 if (!spin_trylock(&dentry->d_lock))
1099 if (dentry->d_lockref.count) {
1100 d_lru_isolate(lru, dentry);
1101 spin_unlock(&dentry->d_lock);
1105 if (dentry->d_flags & DCACHE_REFERENCED) {
1106 dentry->d_flags &= ~DCACHE_REFERENCED;
1107 spin_unlock(&dentry->d_lock);
1111 * this point, we've dropped the dentry->d_lock but keep the
1131 d_lru_shrink_move(lru, dentry, freeable);
1132 spin_unlock(&dentry->d_lock);
1164 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1167 * we are inverting the lru lock/dentry->d_lock here,
1171 if (!spin_trylock(&dentry->d_lock))
1174 d_lru_shrink_move(lru, dentry, freeable);
1175 spin_unlock(&dentry->d_lock);
1205 * @D_WALK_SKIP: skip this dentry and its children
1215 * d_walk - walk the dentry tree
1218 * @enter: callback when first entering the dentry
1222 static void d_walk(struct dentry *parent, void *data,
1223 enum d_walk_ret (*enter)(void *, struct dentry *))
1225 struct dentry *this_parent, *dentry;
1247 dentry = d_first_child(this_parent);
1249 hlist_for_each_entry_from(dentry, d_sib) {
1250 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1253 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1255 ret = enter(data, dentry);
1260 spin_unlock(&dentry->d_lock);
1266 spin_unlock(&dentry->d_lock);
1270 if (!hlist_empty(&dentry->d_children)) {
1272 spin_release(&dentry->d_lock.dep_map, _RET_IP_);
1273 this_parent = dentry;
1277 spin_unlock(&dentry->d_lock);
1285 dentry = this_parent;
1286 this_parent = dentry->d_parent;
1288 spin_unlock(&dentry->d_lock);
1295 hlist_for_each_entry_continue(dentry, d_sib) {
1296 if (likely(!(dentry->d_flags & DCACHE_DENTRY_KILLED))) {
1327 static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1330 struct path path = { .mnt = info->mnt, .dentry = dentry };
1332 if (likely(!d_mountpoint(dentry)))
1342 * path_has_submounts - check for mounts over a dentry in the
1354 d_walk(parent->dentry, &data, path_check_mount);
1363 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1367 * this reason take rename_lock and d_lock on dentry and ancestors.
1369 int d_set_mounted(struct dentry *dentry)
1371 struct dentry *p;
1374 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1383 spin_lock(&dentry->d_lock);
1384 if (!d_unlinked(dentry)) {
1386 if (!d_mountpoint(dentry)) {
1387 dentry->d_flags |= DCACHE_MOUNTED;
1391 spin_unlock(&dentry->d_lock);
1398 * Search the dentry child list of the specified parent,
1413 struct dentry *start;
1416 struct dentry *victim;
1421 static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1426 if (data->start == dentry)
1429 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1431 } else if (!dentry->d_lockref.count) {
1432 to_shrink_list(dentry, &data->dispose);
1434 } else if (dentry->d_lockref.count < 0) {
1448 static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1453 if (data->start == dentry)
1456 if (!dentry->d_lockref.count) {
1457 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1459 data->victim = dentry;
1462 to_shrink_list(dentry, &data->dispose);
1479 * Prune the dcache to remove unused children of the parent dentry.
1481 void shrink_dcache_parent(struct dentry *parent)
1514 static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1517 if (!hlist_empty(&dentry->d_children))
1521 if (dentry == _data && dentry->d_lockref.count == 1)
1526 dentry,
1527 dentry->d_inode ?
1528 dentry->d_inode->i_ino : 0UL,
1529 dentry,
1530 dentry->d_lockref.count,
1531 dentry->d_sb->s_type->name,
1532 dentry->d_sb->s_id);
1536 static void do_one_tree(struct dentry *dentry)
1538 shrink_dcache_parent(dentry);
1539 d_walk(dentry, dentry, umount_check);
1540 d_drop(dentry);
1541 dput(dentry);
1549 struct dentry *dentry;
1553 dentry = sb->s_root;
1555 do_one_tree(dentry);
1558 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
1559 do_one_tree(dentry);
1563 static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
1565 struct dentry **victim = _data;
1566 if (d_mountpoint(dentry)) {
1567 *victim = dget_dlock(dentry);
1575 * @dentry: dentry to invalidate (aka detach, prune and drop)
1577 void d_invalidate(struct dentry *dentry)
1580 spin_lock(&dentry->d_lock);
1581 if (d_unhashed(dentry)) {
1582 spin_unlock(&dentry->d_lock);
1585 __d_drop(dentry);
1586 spin_unlock(&dentry->d_lock);
1589 if (!dentry->d_inode)
1592 shrink_dcache_parent(dentry);
1594 struct dentry *victim = NULL;
1595 d_walk(dentry, &victim, find_submount);
1598 shrink_dcache_parent(dentry);
1613 * Allocates a dentry. It returns %NULL if there is insufficient memory
1614 * available. On a success the dentry is returned. The name passed in is
1618 static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1620 struct dentry *dentry;
1624 dentry = kmem_cache_alloc_lru(dentry_cache, &sb->s_dentry_lru,
1626 if (!dentry)
1635 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1638 dname = dentry->d_iname;
1645 kmem_cache_free(dentry_cache, dentry);
1651 dname = dentry->d_iname;
1654 dentry->d_name.len = name->len;
1655 dentry->d_name.hash = name->hash;
1660 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
1662 dentry->d_lockref.count = 1;
1663 dentry->d_flags = 0;
1664 spin_lock_init(&dentry->d_lock);
1665 seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
1666 dentry->d_inode = NULL;
1667 dentry->d_parent = dentry;
1668 dentry->d_sb = sb;
1669 dentry->d_op = NULL;
1670 dentry->d_fsdata = NULL;
1671 INIT_HLIST_BL_NODE(&dentry->d_hash);
1672 INIT_LIST_HEAD(&dentry->d_lru);
1673 INIT_HLIST_HEAD(&dentry->d_children);
1674 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1675 INIT_HLIST_NODE(&dentry->d_sib);
1676 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1678 if (dentry->d_op && dentry->d_op->d_init) {
1679 err = dentry->d_op->d_init(dentry);
1681 if (dname_external(dentry))
1682 kfree(external_name(dentry));
1683 kmem_cache_free(dentry_cache, dentry);
1690 return dentry;
1698 * Allocates a dentry. It returns %NULL if there is insufficient memory
1699 * available. On a success the dentry is returned. The name passed in is
1702 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1704 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1705 if (!dentry)
1712 dentry->d_parent = dget_dlock(parent);
1713 hlist_add_head(&dentry->d_sib, &parent->d_children);
1716 return dentry;
1720 struct dentry *d_alloc_anon(struct super_block *sb)
1726 struct dentry *d_alloc_cursor(struct dentry * parent)
1728 struct dentry *dentry = d_alloc_anon(parent->d_sb);
1729 if (dentry) {
1730 dentry->d_flags |= DCACHE_DENTRY_CURSOR;
1731 dentry->d_parent = dget(parent);
1733 return dentry;
1737 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1742 * performs lookups at all, return an unhashed IS_ROOT dentry.
1751 struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1756 struct dentry *dentry = __d_alloc(sb, name);
1757 if (likely(dentry)) {
1758 dentry->d_flags |= DCACHE_NORCU;
1760 d_set_d_op(dentry, &anon_ops);
1762 return dentry;
1765 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1775 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1777 WARN_ON_ONCE(dentry->d_op);
1778 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1784 dentry->d_op = op;
1788 dentry->d_flags |= DCACHE_OP_HASH;
1790 dentry->d_flags |= DCACHE_OP_COMPARE;
1792 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1794 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1796 dentry->d_flags |= DCACHE_OP_DELETE;
1798 dentry->d_flags |= DCACHE_OP_PRUNE;
1800 dentry->d_flags |= DCACHE_OP_REAL;
1840 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1843 WARN_ON(d_in_lookup(dentry));
1845 spin_lock(&dentry->d_lock);
1847 * Decrement negative dentry count if it was in the LRU list.
1849 if (dentry->d_flags & DCACHE_LRU_LIST)
1851 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1852 raw_write_seqcount_begin(&dentry->d_seq);
1853 __d_set_inode_and_type(dentry, inode, add_flags);
1854 raw_write_seqcount_end(&dentry->d_seq);
1855 fsnotify_update_flags(dentry);
1856 spin_unlock(&dentry->d_lock);
1860 * d_instantiate - fill in inode information for a dentry
1861 * @entry: dentry to complete
1862 * @inode: inode to attach to this dentry
1874 void d_instantiate(struct dentry *entry, struct inode * inode)
1892 void d_instantiate_new(struct dentry *entry, struct inode *inode)
1908 struct dentry *d_make_root(struct inode *root_inode)
1910 struct dentry *res = NULL;
1923 static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
1926 struct dentry *new, *res;
1948 if (likely(!res)) { /* still no alias, attach a disconnected dentry */
1977 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
1978 * @inode: inode to allocate the dentry for
1980 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1981 * similar open by handle operations. The returned dentry may be anonymous,
1985 * has one dentry. If a dentry is found, that is returned instead of
1989 * to the dentry. In case of an error the reference on the inode is released.
1994 struct dentry *d_obtain_alias(struct inode *inode)
2001 * d_obtain_root - find or allocate a dentry for a given inode
2002 * @inode: inode to allocate the dentry for
2004 * Obtain an IS_ROOT dentry for the root of a filesystem.
2006 * We must ensure that directory inodes only ever have one dentry. If a
2007 * dentry is found, that is returned instead of allocating a new one.
2010 * to the dentry. In case of an error the reference on the inode is
2015 struct dentry *d_obtain_root(struct inode *inode)
2022 * d_add_ci - lookup or allocate new dentry with case-exact name
2024 * @dentry: the negative dentry that was passed to the parent's lookup func
2025 * @name: the case-exact name to be associated with the returned dentry
2031 * For a case-insensitive lookup match and if the case-exact dentry
2034 * If no entry exists with the exact case name, allocate new dentry with
2037 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2040 struct dentry *found, *res;
2043 * First check if a dentry matching the name already exists,
2046 found = d_hash_and_lookup(dentry->d_parent, name);
2051 if (d_in_lookup(dentry)) {
2052 found = d_alloc_parallel(dentry->d_parent, name,
2053 dentry->d_wait);
2059 found = d_alloc(dentry->d_parent, name);
2076 * d_same_name - compare dentry name with case-exact name
2077 * @parent: parent dentry
2078 * @dentry: the negative dentry that was passed to the parent's lookup func
2079 * @name: the case-exact name to be associated with the returned dentry
2083 bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
2087 if (dentry->d_name.len != name->len)
2089 return dentry_cmp(dentry, name->name, name->len) == 0;
2091 return parent->d_op->d_compare(dentry,
2092 dentry->d_name.len, dentry->d_name.name,
2098 * This is __d_lookup_rcu() when the parent dentry has
2101 static noinline struct dentry *__d_lookup_rcu_op_compare(
2102 const struct dentry *parent,
2109 struct dentry *dentry;
2111 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2117 seq = raw_seqcount_begin(&dentry->d_seq);
2118 if (dentry->d_parent != parent)
2120 if (d_unhashed(dentry))
2122 if (dentry->d_name.hash != hashlen_hash(hashlen))
2124 tlen = dentry->d_name.len;
2125 tname = dentry->d_name.name;
2127 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2131 if (parent->d_op->d_compare(dentry, tlen, tname, name) != 0)
2134 return dentry;
2140 * __d_lookup_rcu - search for a dentry (racy, store-free)
2141 * @parent: parent dentry
2143 * @seqp: returns d_seq value at the point where the dentry was found
2144 * Returns: dentry, or NULL
2153 * held, and rcu_read_lock held. The returned dentry must not be stored into
2157 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2161 * the returned dentry, so long as its parent's seqlock is checked after the
2165 * NOTE! The caller *has* to check the resulting dentry against the sequence
2166 * number we've returned before using any of the resulting dentry state!
2168 struct dentry *__d_lookup_rcu(const struct dentry *parent,
2176 struct dentry *dentry;
2191 * Carefully use d_seq when comparing a candidate dentry, to avoid
2195 * walk here and result in missing our dentry, resulting in the
2201 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2205 * The dentry sequence count protects us from concurrent
2209 * to do anything useful with the returned dentry.
2214 * dentry compare, we will do seqretries until it is stable,
2221 seq = raw_seqcount_begin(&dentry->d_seq);
2222 if (dentry->d_parent != parent)
2224 if (d_unhashed(dentry))
2226 if (dentry->d_name.hash_len != hashlen)
2228 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2231 return dentry;
2237 * d_lookup - search for a dentry
2238 * @parent: parent dentry
2240 * Returns: dentry, or NULL
2242 * d_lookup searches the children of the parent dentry for the name in
2243 * question. If the dentry is found its reference count is incremented and the
2244 * dentry is returned. The caller must use dput to free the entry when it has
2245 * finished using it. %NULL is returned if the dentry does not exist.
2247 struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2249 struct dentry *dentry;
2254 dentry = __d_lookup(parent, name);
2255 if (dentry)
2258 return dentry;
2263 * __d_lookup - search for a dentry (racy)
2264 * @parent: parent dentry
2266 * Returns: dentry, or NULL
2277 struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2282 struct dentry *found = NULL;
2283 struct dentry *dentry;
2295 * Take d_lock when comparing a candidate dentry, to avoid races
2299 * walk here and result in missing our dentry, resulting in the
2307 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2309 if (dentry->d_name.hash != hash)
2312 spin_lock(&dentry->d_lock);
2313 if (dentry->d_parent != parent)
2315 if (d_unhashed(dentry))
2318 if (!d_same_name(dentry, parent, name))
2321 dentry->d_lockref.count++;
2322 found = dentry;
2323 spin_unlock(&dentry->d_lock);
2326 spin_unlock(&dentry->d_lock);
2334 * d_hash_and_lookup - hash the qstr then search for a dentry
2340 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2359 * - turn this dentry into a negative dentry
2360 * - unhash this dentry and free it.
2363 * a negative dentry, but if anybody else is
2364 * currently using the dentry or the inode
2371 * d_delete - delete a dentry
2372 * @dentry: The dentry to delete
2374 * Turn the dentry into a negative dentry if possible, otherwise
2378 void d_delete(struct dentry * dentry)
2380 struct inode *inode = dentry->d_inode;
2383 spin_lock(&dentry->d_lock);
2387 if (dentry->d_lockref.count == 1) {
2388 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2389 dentry_unlink_inode(dentry);
2391 __d_drop(dentry);
2392 spin_unlock(&dentry->d_lock);
2398 static void __d_rehash(struct dentry *entry)
2409 * @entry: dentry to add to the hash
2411 * Adds a dentry to the hash according to its name.
2414 void d_rehash(struct dentry * entry)
2441 static void d_wait_lookup(struct dentry *dentry)
2443 if (d_in_lookup(dentry)) {
2445 add_wait_queue(dentry->d_wait, &wait);
2448 spin_unlock(&dentry->d_lock);
2450 spin_lock(&dentry->d_lock);
2451 } while (d_in_lookup(dentry));
2455 struct dentry *d_alloc_parallel(struct dentry *parent,
2462 struct dentry *new = d_alloc(parent, name);
2463 struct dentry *dentry;
2473 dentry = __d_lookup_rcu(parent, name, &d_seq);
2474 if (unlikely(dentry)) {
2475 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2479 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2481 dput(dentry);
2486 return dentry;
2511 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2512 if (dentry->d_name.hash != hash)
2514 if (dentry->d_parent != parent)
2516 if (!d_same_name(dentry, parent, name))
2520 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2530 spin_lock(&dentry->d_lock);
2531 d_wait_lookup(dentry);
2538 if (unlikely(dentry->d_name.hash != hash))
2540 if (unlikely(dentry->d_parent != parent))
2542 if (unlikely(d_unhashed(dentry)))
2544 if (unlikely(!d_same_name(dentry, parent, name)))
2547 spin_unlock(&dentry->d_lock);
2549 return dentry;
2559 spin_unlock(&dentry->d_lock);
2560 dput(dentry);
2566 * - Unhash the dentry
2567 * - Retrieve and clear the waitqueue head in dentry
2570 static wait_queue_head_t *__d_lookup_unhash(struct dentry *dentry)
2575 lockdep_assert_held(&dentry->d_lock);
2577 b = in_lookup_hash(dentry->d_parent, dentry->d_name.hash);
2579 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
2580 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
2581 d_wait = dentry->d_wait;
2582 dentry->d_wait = NULL;
2584 INIT_HLIST_NODE(&dentry->d_u.d_alias);
2585 INIT_LIST_HEAD(&dentry->d_lru);
2589 void __d_lookup_unhash_wake(struct dentry *dentry)
2591 spin_lock(&dentry->d_lock);
2592 wake_up_all(__d_lookup_unhash(dentry));
2593 spin_unlock(&dentry->d_lock);
2599 static inline void __d_add(struct dentry *dentry, struct inode *inode)
2604 spin_lock(&dentry->d_lock);
2605 if (unlikely(d_in_lookup(dentry))) {
2606 dir = dentry->d_parent->d_inode;
2608 d_wait = __d_lookup_unhash(dentry);
2612 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2613 raw_write_seqcount_begin(&dentry->d_seq);
2614 __d_set_inode_and_type(dentry, inode, add_flags);
2615 raw_write_seqcount_end(&dentry->d_seq);
2616 fsnotify_update_flags(dentry);
2618 __d_rehash(dentry);
2621 spin_unlock(&dentry->d_lock);
2627 * d_add - add dentry to hash queues
2628 * @entry: dentry to add
2629 * @inode: The inode to attach to this dentry
2635 void d_add(struct dentry *entry, struct inode *inode)
2647 * @entry: dentry to add
2648 * @inode: The inode to go with this dentry
2650 * If an unhashed dentry with the same name/parent and desired
2656 struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2658 struct dentry *alias;
2691 static void swap_names(struct dentry *dentry, struct dentry *target)
2694 if (unlikely(dname_external(dentry))) {
2698 swap(target->d_name.name, dentry->d_name.name);
2701 * dentry:internal, target:external. Steal target's
2704 memcpy(target->d_iname, dentry->d_name.name,
2705 dentry->d_name.len + 1);
2706 dentry->d_name.name = target->d_name.name;
2710 if (unlikely(dname_external(dentry))) {
2712 * dentry:external, target:internal. Give dentry's
2713 * storage to target and make dentry internal
2715 memcpy(dentry->d_iname, target->d_name.name,
2717 target->d_name.name = dentry->d_name.name;
2718 dentry->d_name.name = dentry->d_iname;
2726 swap(((long *) &dentry->d_iname)[i],
2731 swap(dentry->d_name.hash_len, target->d_name.hash_len);
2734 static void copy_name(struct dentry *dentry, struct dentry *target)
2737 if (unlikely(dname_external(dentry)))
2738 old_name = external_name(dentry);
2741 dentry->d_name = target->d_name;
2743 memcpy(dentry->d_iname, target->d_name.name,
2745 dentry->d_name.name = dentry->d_iname;
2746 dentry->d_name.hash_len = target->d_name.hash_len;
2753 * __d_move - move a dentry
2754 * @dentry: entry to move
2755 * @target: new dentry
2763 static void __d_move(struct dentry *dentry, struct dentry *target,
2766 struct dentry *old_parent, *p;
2771 WARN_ON(!dentry->d_inode);
2772 if (WARN_ON(dentry == target))
2775 BUG_ON(d_ancestor(target, dentry));
2776 old_parent = dentry->d_parent;
2778 if (IS_ROOT(dentry)) {
2782 /* target is not a descendent of dentry->d_parent */
2786 BUG_ON(p == dentry);
2792 spin_lock_nested(&dentry->d_lock, 2);
2801 write_seqcount_begin(&dentry->d_seq);
2805 if (!d_unhashed(dentry))
2806 ___d_drop(dentry);
2811 dentry->d_parent = target->d_parent;
2813 copy_name(dentry, target);
2815 dentry->d_parent->d_lockref.count++;
2816 if (dentry != old_parent) /* wasn't IS_ROOT */
2820 swap_names(dentry, target);
2827 if (!hlist_unhashed(&dentry->d_sib))
2828 __hlist_del(&dentry->d_sib);
2829 hlist_add_head(&dentry->d_sib, &dentry->d_parent->d_children);
2830 __d_rehash(dentry);
2831 fsnotify_update_flags(dentry);
2832 fscrypt_handle_d_move(dentry);
2835 write_seqcount_end(&dentry->d_seq);
2840 if (dentry->d_parent != old_parent)
2841 spin_unlock(&dentry->d_parent->d_lock);
2842 if (dentry != old_parent)
2845 spin_unlock(&dentry->d_lock);
2849 * d_move - move a dentry
2850 * @dentry: entry to move
2851 * @target: new dentry
2857 void d_move(struct dentry *dentry, struct dentry *target)
2860 __d_move(dentry, target, false);
2867 * @dentry1: first dentry
2868 * @dentry2: second dentry
2870 void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2886 * @p1: ancestor dentry
2887 * @p2: child dentry
2889 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2892 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2894 struct dentry *p;
2907 * dentry->d_parent->d_inode->i_mutex, and rename_lock
2912 static int __d_unalias(struct dentry *dentry, struct dentry *alias)
2918 /* If alias and dentry share a parent, then no extra locks required */
2919 if (alias->d_parent == dentry->d_parent)
2923 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2925 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2930 __d_move(alias, dentry, false);
2941 * d_splice_alias - splice a disconnected dentry into the tree if one exists
2942 * @inode: the inode which may have a disconnected dentry
2943 * @dentry: a negative dentry which we want to point to the inode.
2946 * place of the given dentry and return it, else simply d_add the inode
2947 * to the dentry and return NULL.
2955 * If a dentry was found and moved, then it is returned. Otherwise NULL
2958 * Cluster filesystems may call this function with a negative, hashed dentry.
2960 * will only occur during atomic_open. So we need to check for the dentry
2963 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
2968 BUG_ON(!d_unhashed(dentry));
2973 security_d_instantiate(dentry, inode);
2976 struct dentry *new = __d_find_any_alias(inode);
2981 if (unlikely(d_ancestor(new, dentry))) {
2988 dentry->d_name.name,
2992 struct dentry *old_parent = dget(new->d_parent);
2993 int err = __d_unalias(dentry, new);
3001 __d_move(new, dentry, false);
3009 __d_add(dentry, inode);
3021 * is_subdir - is new dentry a subdirectory of old_dentry
3022 * @new_dentry: new dentry
3023 * @old_dentry: old dentry
3030 bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3057 static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3059 struct dentry *root = data;
3060 if (dentry != root) {
3061 if (d_unhashed(dentry) || !dentry->d_inode)
3064 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3065 dentry->d_flags |= DCACHE_GENOCIDE;
3066 dentry->d_lockref.count--;
3072 void d_genocide(struct dentry *parent)
3079 struct dentry *dentry = file->f_path.dentry;
3081 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3082 !hlist_unhashed(&dentry->d_u.d_alias) ||
3083 !d_unlinked(dentry));
3084 spin_lock(&dentry->d_parent->d_lock);
3085 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3086 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3088 spin_unlock(&dentry->d_lock);
3089 spin_unlock(&dentry->d_parent->d_lock);
3095 struct dentry *dentry = file->f_path.dentry;
3099 d_instantiate(dentry, inode);
3141 dentry_cache = KMEM_CACHE_USERCOPY(dentry,