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",
213 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
258 static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
269 * and a dentry name is guaranteed to be properly
276 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
289 static inline struct external_name *external_name(struct dentry *dentry)
291 return container_of(dentry->d_name.name, struct external_name, name[0]);
296 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
298 kmem_cache_free(dentry_cache, dentry);
303 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
304 kfree(external_name(dentry));
305 kmem_cache_free(dentry_cache, dentry);
308 static inline int dname_external(const struct dentry *dentry)
310 return dentry->d_name.name != dentry->d_iname;
313 void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
315 spin_lock(&dentry->d_lock);
316 name->name = dentry->d_name;
317 if (unlikely(dname_external(dentry))) {
318 atomic_inc(&external_name(dentry)->u.count);
320 memcpy(name->inline_name, dentry->d_iname,
321 dentry->d_name.len + 1);
324 spin_unlock(&dentry->d_lock);
339 static inline void __d_set_inode_and_type(struct dentry *dentry,
345 dentry->d_inode = inode;
346 flags = READ_ONCE(dentry->d_flags);
349 smp_store_release(&dentry->d_flags, flags);
352 static inline void __d_clear_type_and_inode(struct dentry *dentry)
354 unsigned flags = READ_ONCE(dentry->d_flags);
357 WRITE_ONCE(dentry->d_flags, flags);
358 dentry->d_inode = NULL;
359 if (dentry->d_flags & DCACHE_LRU_LIST)
363 static void dentry_free(struct dentry *dentry)
365 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
366 if (unlikely(dname_external(dentry))) {
367 struct external_name *p = external_name(dentry);
369 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
373 /* if dentry was never visible to RCU, immediate free is OK */
374 if (dentry->d_flags & DCACHE_NORCU)
375 __d_free(&dentry->d_u.d_rcu);
377 call_rcu(&dentry->d_u.d_rcu, __d_free);
381 * Release the dentry's inode, using the filesystem
384 static void dentry_unlink_inode(struct dentry * dentry)
385 __releases(dentry->d_lock)
386 __releases(dentry->d_inode->i_lock)
388 struct inode *inode = dentry->d_inode;
390 raw_write_seqcount_begin(&dentry->d_seq);
391 __d_clear_type_and_inode(dentry);
392 hlist_del_init(&dentry->d_u.d_alias);
393 raw_write_seqcount_end(&dentry->d_seq);
394 spin_unlock(&dentry->d_lock);
398 if (dentry->d_op && dentry->d_op->d_iput)
399 dentry->d_op->d_iput(dentry, inode);
409 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
423 #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
424 static void d_lru_add(struct dentry *dentry)
426 D_FLAG_VERIFY(dentry, 0);
427 dentry->d_flags |= DCACHE_LRU_LIST;
429 if (d_is_negative(dentry))
431 WARN_ON_ONCE(!list_lru_add(&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))
441 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
444 static void d_shrink_del(struct dentry *dentry)
446 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
447 list_del_init(&dentry->d_lru);
448 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
452 static void d_shrink_add(struct dentry *dentry, struct list_head *list)
454 D_FLAG_VERIFY(dentry, 0);
455 list_add(&dentry->d_lru, list);
456 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
466 static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
468 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
469 dentry->d_flags &= ~DCACHE_LRU_LIST;
471 if (d_is_negative(dentry))
473 list_lru_isolate(lru, &dentry->d_lru);
476 static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
479 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
480 dentry->d_flags |= DCACHE_SHRINK_LIST;
481 if (d_is_negative(dentry))
483 list_lru_isolate_move(lru, &dentry->d_lru, list);
486 static void ___d_drop(struct dentry *dentry)
490 * Hashed dentries are normally on the dentry hashtable,
494 if (unlikely(IS_ROOT(dentry)))
495 b = &dentry->d_sb->s_roots;
497 b = d_hash(dentry->d_name.hash);
500 __hlist_bl_del(&dentry->d_hash);
504 void __d_drop(struct dentry *dentry)
506 if (!d_unhashed(dentry)) {
507 ___d_drop(dentry);
508 dentry->d_hash.pprev = NULL;
509 write_seqcount_invalidate(&dentry->d_seq);
515 * d_drop - drop a dentry
516 * @dentry: dentry to drop
518 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
520 * deleting the dentry - d_delete will try to mark the dentry negative if
524 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
527 * __d_drop requires dentry->d_lock
529 * ___d_drop doesn't mark dentry as "unhashed"
530 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
532 void d_drop(struct dentry *dentry)
534 spin_lock(&dentry->d_lock);
535 __d_drop(dentry);
536 spin_unlock(&dentry->d_lock);
540 static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
542 struct dentry *next;
545 * attached to the dentry tree
547 dentry->d_flags |= DCACHE_DENTRY_KILLED;
548 if (unlikely(list_empty(&dentry->d_child)))
550 __list_del_entry(&dentry->d_child);
570 while (dentry->d_child.next != &parent->d_subdirs) {
571 next = list_entry(dentry->d_child.next, struct dentry, d_child);
574 dentry->d_child.next = next->d_child.next;
578 static void __dentry_kill(struct dentry *dentry)
580 struct dentry *parent = NULL;
582 if (!IS_ROOT(dentry))
583 parent = dentry->d_parent;
586 * The dentry is now unrecoverably dead to the world.
588 lockref_mark_dead(&dentry->d_lockref);
591 * inform the fs via d_prune that this dentry is about to be
594 if (dentry->d_flags & DCACHE_OP_PRUNE)
595 dentry->d_op->d_prune(dentry);
597 if (dentry->d_flags & DCACHE_LRU_LIST) {
598 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
599 d_lru_del(dentry);
602 __d_drop(dentry);
603 dentry_unlist(dentry, parent);
606 if (dentry->d_inode)
607 dentry_unlink_inode(dentry);
609 spin_unlock(&dentry->d_lock);
611 if (dentry->d_op && dentry->d_op->d_release)
612 dentry->d_op->d_release(dentry);
614 spin_lock(&dentry->d_lock);
615 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
616 dentry->d_flags |= DCACHE_MAY_FREE;
619 spin_unlock(&dentry->d_lock);
621 dentry_free(dentry);
625 static struct dentry *__lock_parent(struct dentry *dentry)
627 struct dentry *parent;
629 spin_unlock(&dentry->d_lock);
631 parent = READ_ONCE(dentry->d_parent);
634 * We can't blindly lock dentry until we are sure
636 * Any changes of dentry->d_parent must have
641 if (unlikely(parent != dentry->d_parent)) {
646 if (parent != dentry)
647 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
653 static inline struct dentry *lock_parent(struct dentry *dentry)
655 struct dentry *parent = dentry->d_parent;
656 if (IS_ROOT(dentry))
660 return __lock_parent(dentry);
663 static inline bool retain_dentry(struct dentry *dentry)
665 WARN_ON(d_in_lookup(dentry));
668 if (unlikely(d_unhashed(dentry)))
671 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
674 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
675 if (dentry->d_op->d_delete(dentry))
679 if (unlikely(dentry->d_flags & DCACHE_DONTCACHE))
683 dentry->d_lockref.count--;
684 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
685 d_lru_add(dentry);
686 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
687 dentry->d_flags |= DCACHE_REFERENCED;
693 struct dentry *de;
707 * Finish off a dentry we've decided to kill.
708 * dentry->d_lock must be held, returns with it unlocked.
709 * Returns dentry requiring refcount drop, or NULL if we're done.
711 static struct dentry *dentry_kill(struct dentry *dentry)
712 __releases(dentry->d_lock)
714 struct inode *inode = dentry->d_inode;
715 struct dentry *parent = NULL;
720 if (!IS_ROOT(dentry)) {
721 parent = dentry->d_parent;
723 parent = __lock_parent(dentry);
724 if (likely(inode || !dentry->d_inode))
729 inode = dentry->d_inode;
733 __dentry_kill(dentry);
737 spin_unlock(&dentry->d_lock);
739 spin_lock(&dentry->d_lock);
740 parent = lock_parent(dentry);
742 if (unlikely(dentry->d_lockref.count != 1)) {
743 dentry->d_lockref.count--;
744 } else if (likely(!retain_dentry(dentry))) {
745 __dentry_kill(dentry);
753 spin_unlock(&dentry->d_lock);
760 * If unsuccessful, we return false, having already taken the dentry lock.
762 * The caller needs to hold the RCU read lock, so that the dentry is
765 static inline bool fast_dput(struct dentry *dentry)
772 * let the dentry count go to zero, so use "put_or_lock".
774 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
775 return lockref_put_or_lock(&dentry->d_lockref);
781 ret = lockref_put_return(&dentry->d_lockref);
789 spin_lock(&dentry->d_lock);
790 if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) {
791 spin_unlock(&dentry->d_lock);
794 dentry->d_lockref.count--;
806 * to zero, but we don't hold the dentry lock, so
812 * do: the dentry is still hashed, it does not have
819 * dentry was hashed and the flags had the proper
820 * value. Other dentry users may have re-gotten
821 * a reference to the dentry and change that, but
822 * our work is done - we can leave the dentry
826 * the dentry anyway.
832 d_flags = READ_ONCE(dentry->d_flags);
837 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
845 spin_lock(&dentry->d_lock);
854 if (dentry->d_lockref.count) {
855 spin_unlock(&dentry->d_lock);
864 dentry->d_lockref.count = 1;
876 * However, that implies that we have to traverse the dentry
887 * dput - release a dentry
888 * @dentry: dentry to release
890 * Release a dentry. This will drop the usage count and if appropriate
891 * call the dentry unlink method as well as removing it from the queues and
895 void dput(struct dentry *dentry)
897 while (dentry) {
901 if (likely(fast_dput(dentry))) {
906 /* Slow case: now with the dentry lock held */
909 if (likely(retain_dentry(dentry))) {
910 spin_unlock(&dentry->d_lock);
914 dentry = dentry_kill(dentry);
919 static void __dput_to_list(struct dentry *dentry, struct list_head *list)
920 __must_hold(&dentry->d_lock)
922 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
924 --dentry->d_lockref.count;
926 if (dentry->d_flags & DCACHE_LRU_LIST)
927 d_lru_del(dentry);
928 if (!--dentry->d_lockref.count)
929 d_shrink_add(dentry, list);
933 void dput_to_list(struct dentry *dentry, struct list_head *list)
936 if (likely(fast_dput(dentry))) {
941 if (!retain_dentry(dentry))
942 __dput_to_list(dentry, list);
943 spin_unlock(&dentry->d_lock);
947 static inline void __dget_dlock(struct dentry *dentry)
949 dentry->d_lockref.count++;
952 static inline void __dget(struct dentry *dentry)
954 lockref_get(&dentry->d_lockref);
957 struct dentry *dget_parent(struct dentry *dentry)
960 struct dentry *ret;
968 seq = raw_seqcount_begin(&dentry->d_seq);
969 ret = READ_ONCE(dentry->d_parent);
973 if (!read_seqcount_retry(&dentry->d_seq, seq))
984 ret = dentry->d_parent;
986 if (unlikely(ret != dentry->d_parent)) {
999 static struct dentry * __d_find_any_alias(struct inode *inode)
1001 struct dentry *alias;
1005 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
1017 struct dentry *d_find_any_alias(struct inode *inode)
1019 struct dentry *de;
1028 static struct dentry *__d_find_alias(struct inode *inode)
1030 struct dentry *alias;
1061 struct dentry *d_find_alias(struct inode *inode)
1063 struct dentry *de = NULL;
1078 struct dentry *d_find_alias_rcu(struct inode *inode)
1081 struct dentry *de = NULL;
1088 de = hlist_entry(l->first, struct dentry, d_u.d_alias);
1105 struct dentry *dentry;
1108 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1109 spin_lock(&dentry->d_lock);
1110 if (!dentry->d_lockref.count) {
1111 struct dentry *parent = lock_parent(dentry);
1112 if (likely(!dentry->d_lockref.count)) {
1113 __dentry_kill(dentry);
1120 spin_unlock(&dentry->d_lock);
1127 * Lock a dentry from shrink list.
1128 * Called under rcu_read_lock() and dentry->d_lock; the former
1130 * Note that dentry is *not* protected from concurrent dentry_kill(),
1133 * Return false if dentry has been disrupted or grabbed, leaving
1135 * that dentry's inode and parent both locked.
1137 static bool shrink_lock_dentry(struct dentry *dentry)
1140 struct dentry *parent;
1142 if (dentry->d_lockref.count)
1145 inode = dentry->d_inode;
1147 spin_unlock(&dentry->d_lock);
1149 spin_lock(&dentry->d_lock);
1150 if (unlikely(dentry->d_lockref.count))
1153 if (unlikely(inode != dentry->d_inode))
1157 parent = dentry->d_parent;
1158 if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1161 spin_unlock(&dentry->d_lock);
1163 if (unlikely(parent != dentry->d_parent)) {
1165 spin_lock(&dentry->d_lock);
1168 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1169 if (likely(!dentry->d_lockref.count))
1181 struct dentry *dentry, *parent;
1183 dentry = list_entry(list->prev, struct dentry, d_lru);
1184 spin_lock(&dentry->d_lock);
1186 if (!shrink_lock_dentry(dentry)) {
1189 d_shrink_del(dentry);
1190 if (dentry->d_lockref.count < 0)
1191 can_free = dentry->d_flags & DCACHE_MAY_FREE;
1192 spin_unlock(&dentry->d_lock);
1194 dentry_free(dentry);
1198 d_shrink_del(dentry);
1199 parent = dentry->d_parent;
1200 if (parent != dentry)
1202 __dentry_kill(dentry);
1210 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1214 * we are inverting the lru lock/dentry->d_lock here,
1218 if (!spin_trylock(&dentry->d_lock))
1226 if (dentry->d_lockref.count) {
1227 d_lru_isolate(lru, dentry);
1228 spin_unlock(&dentry->d_lock);
1232 if (dentry->d_flags & DCACHE_REFERENCED) {
1233 dentry->d_flags &= ~DCACHE_REFERENCED;
1234 spin_unlock(&dentry->d_lock);
1238 * this point, we've dropped the dentry->d_lock but keep the
1258 d_lru_shrink_move(lru, dentry, freeable);
1259 spin_unlock(&dentry->d_lock);
1291 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1294 * we are inverting the lru lock/dentry->d_lock here,
1298 if (!spin_trylock(&dentry->d_lock))
1301 d_lru_shrink_move(lru, dentry, freeable);
1302 spin_unlock(&dentry->d_lock);
1332 * @D_WALK_SKIP: skip this dentry and its children
1342 * d_walk - walk the dentry tree
1345 * @enter: callback when first entering the dentry
1349 static void d_walk(struct dentry *parent, void *data,
1350 enum d_walk_ret (*enter)(void *, struct dentry *))
1352 struct dentry *this_parent;
1379 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1382 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1385 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1387 ret = enter(data, dentry);
1392 spin_unlock(&dentry->d_lock);
1398 spin_unlock(&dentry->d_lock);
1402 if (!list_empty(&dentry->d_subdirs)) {
1404 spin_release(&dentry->d_lock.dep_map, _RET_IP_);
1405 this_parent = dentry;
1409 spin_unlock(&dentry->d_lock);
1417 struct dentry *child = this_parent;
1431 child = list_entry(next, struct dentry, d_child);
1460 static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1463 struct path path = { .mnt = info->mnt, .dentry = dentry };
1465 if (likely(!d_mountpoint(dentry)))
1475 * path_has_submounts - check for mounts over a dentry in the
1487 d_walk(parent->dentry, &data, path_check_mount);
1496 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1500 * this reason take rename_lock and d_lock on dentry and ancestors.
1502 int d_set_mounted(struct dentry *dentry)
1504 struct dentry *p;
1507 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1516 spin_lock(&dentry->d_lock);
1517 if (!d_unlinked(dentry)) {
1519 if (!d_mountpoint(dentry)) {
1520 dentry->d_flags |= DCACHE_MOUNTED;
1524 spin_unlock(&dentry->d_lock);
1531 * Search the dentry child list of the specified parent,
1546 struct dentry *start;
1549 struct dentry *victim;
1554 static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1559 if (data->start == dentry)
1562 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1565 if (dentry->d_flags & DCACHE_LRU_LIST)
1566 d_lru_del(dentry);
1567 if (!dentry->d_lockref.count) {
1568 d_shrink_add(dentry, &data->dispose);
1583 static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1588 if (data->start == dentry)
1591 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1592 if (!dentry->d_lockref.count) {
1594 data->victim = dentry;
1598 if (dentry->d_flags & DCACHE_LRU_LIST)
1599 d_lru_del(dentry);
1600 if (!dentry->d_lockref.count)
1601 d_shrink_add(dentry, &data->dispose);
1618 * Prune the dcache to remove unused children of the parent dentry.
1620 void shrink_dcache_parent(struct dentry *parent)
1639 struct dentry *parent;
1658 static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1661 if (!list_empty(&dentry->d_subdirs))
1665 if (dentry == _data && dentry->d_lockref.count == 1)
1670 dentry,
1671 dentry->d_inode ?
1672 dentry->d_inode->i_ino : 0UL,
1673 dentry,
1674 dentry->d_lockref.count,
1675 dentry->d_sb->s_type->name,
1676 dentry->d_sb->s_id);
1680 static void do_one_tree(struct dentry *dentry)
1682 shrink_dcache_parent(dentry);
1683 d_walk(dentry, dentry, umount_check);
1684 d_drop(dentry);
1685 dput(dentry);
1693 struct dentry *dentry;
1697 dentry = sb->s_root;
1699 do_one_tree(dentry);
1702 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
1703 do_one_tree(dentry);
1707 static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
1709 struct dentry **victim = _data;
1710 if (d_mountpoint(dentry)) {
1711 __dget_dlock(dentry);
1712 *victim = dentry;
1720 * @dentry: dentry to invalidate (aka detach, prune and drop)
1722 void d_invalidate(struct dentry *dentry)
1725 spin_lock(&dentry->d_lock);
1726 if (d_unhashed(dentry)) {
1727 spin_unlock(&dentry->d_lock);
1730 __d_drop(dentry);
1731 spin_unlock(&dentry->d_lock);
1734 if (!dentry->d_inode)
1737 shrink_dcache_parent(dentry);
1739 struct dentry *victim = NULL;
1740 d_walk(dentry, &victim, find_submount);
1743 shrink_dcache_parent(dentry);
1758 * Allocates a dentry. It returns %NULL if there is insufficient memory
1759 * available. On a success the dentry is returned. The name passed in is
1763 static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1765 struct dentry *dentry;
1769 dentry = kmem_cache_alloc_lru(dentry_cache, &sb->s_dentry_lru,
1771 if (!dentry)
1780 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1783 dname = dentry->d_iname;
1790 kmem_cache_free(dentry_cache, dentry);
1796 dname = dentry->d_iname;
1799 dentry->d_name.len = name->len;
1800 dentry->d_name.hash = name->hash;
1805 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
1807 dentry->d_lockref.count = 1;
1808 dentry->d_flags = 0;
1809 spin_lock_init(&dentry->d_lock);
1810 seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
1811 dentry->d_inode = NULL;
1812 dentry->d_parent = dentry;
1813 dentry->d_sb = sb;
1814 dentry->d_op = NULL;
1815 dentry->d_fsdata = NULL;
1816 INIT_HLIST_BL_NODE(&dentry->d_hash);
1817 INIT_LIST_HEAD(&dentry->d_lru);
1818 INIT_LIST_HEAD(&dentry->d_subdirs);
1819 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1820 INIT_LIST_HEAD(&dentry->d_child);
1821 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1823 if (dentry->d_op && dentry->d_op->d_init) {
1824 err = dentry->d_op->d_init(dentry);
1826 if (dname_external(dentry))
1827 kfree(external_name(dentry));
1828 kmem_cache_free(dentry_cache, dentry);
1835 return dentry;
1843 * Allocates a dentry. It returns %NULL if there is insufficient memory
1844 * available. On a success the dentry is returned. The name passed in is
1847 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1849 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1850 if (!dentry)
1858 dentry->d_parent = parent;
1859 list_add(&dentry->d_child, &parent->d_subdirs);
1862 return dentry;
1866 struct dentry *d_alloc_anon(struct super_block *sb)
1872 struct dentry *d_alloc_cursor(struct dentry * parent)
1874 struct dentry *dentry = d_alloc_anon(parent->d_sb);
1875 if (dentry) {
1876 dentry->d_flags |= DCACHE_DENTRY_CURSOR;
1877 dentry->d_parent = dget(parent);
1879 return dentry;
1883 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1888 * performs lookups at all, return an unhashed IS_ROOT dentry.
1897 struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1899 struct dentry *dentry = __d_alloc(sb, name);
1900 if (likely(dentry))
1901 dentry->d_flags |= DCACHE_NORCU;
1902 return dentry;
1905 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1915 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1917 WARN_ON_ONCE(dentry->d_op);
1918 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1924 dentry->d_op = op;
1928 dentry->d_flags |= DCACHE_OP_HASH;
1930 dentry->d_flags |= DCACHE_OP_COMPARE;
1932 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1934 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1936 dentry->d_flags |= DCACHE_OP_DELETE;
1938 dentry->d_flags |= DCACHE_OP_PRUNE;
1940 dentry->d_flags |= DCACHE_OP_REAL;
1947 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1948 * @dentry - The dentry to mark
1950 * Mark a dentry as falling through to the lower layer (as set with
1953 void d_set_fallthru(struct dentry *dentry)
1955 spin_lock(&dentry->d_lock);
1956 dentry->d_flags |= DCACHE_FALLTHRU;
1957 spin_unlock(&dentry->d_lock);
1996 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1999 WARN_ON(d_in_lookup(dentry));
2001 spin_lock(&dentry->d_lock);
2003 * Decrement negative dentry count if it was in the LRU list.
2005 if (dentry->d_flags & DCACHE_LRU_LIST)
2007 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2008 raw_write_seqcount_begin(&dentry->d_seq);
2009 __d_set_inode_and_type(dentry, inode, add_flags);
2010 raw_write_seqcount_end(&dentry->d_seq);
2011 fsnotify_update_flags(dentry);
2012 spin_unlock(&dentry->d_lock);
2016 * d_instantiate - fill in inode information for a dentry
2017 * @entry: dentry to complete
2018 * @inode: inode to attach to this dentry
2030 void d_instantiate(struct dentry *entry, struct inode * inode)
2048 void d_instantiate_new(struct dentry *entry, struct inode *inode)
2064 struct dentry *d_make_root(struct inode *root_inode)
2066 struct dentry *res = NULL;
2079 static struct dentry *__d_instantiate_anon(struct dentry *dentry,
2083 struct dentry *res;
2086 security_d_instantiate(dentry, inode);
2091 dput(dentry);
2095 /* attach a disconnected dentry */
2101 spin_lock(&dentry->d_lock);
2102 __d_set_inode_and_type(dentry, inode, add_flags);
2103 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2105 hlist_bl_lock(&dentry->d_sb->s_roots);
2106 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
2107 hlist_bl_unlock(&dentry->d_sb->s_roots);
2109 spin_unlock(&dentry->d_lock);
2112 return dentry;
2119 struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2121 return __d_instantiate_anon(dentry, inode, true);
2125 static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
2127 struct dentry *tmp;
2128 struct dentry *res;
2153 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2154 * @inode: inode to allocate the dentry for
2156 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2157 * similar open by handle operations. The returned dentry may be anonymous,
2161 * has one dentry. If a dentry is found, that is returned instead of
2165 * to the dentry. In case of an error the reference on the inode is released.
2170 struct dentry *d_obtain_alias(struct inode *inode)
2177 * d_obtain_root - find or allocate a dentry for a given inode
2178 * @inode: inode to allocate the dentry for
2180 * Obtain an IS_ROOT dentry for the root of a filesystem.
2182 * We must ensure that directory inodes only ever have one dentry. If a
2183 * dentry is found, that is returned instead of allocating a new one.
2186 * to the dentry. In case of an error the reference on the inode is
2191 struct dentry *d_obtain_root(struct inode *inode)
2198 * d_add_ci - lookup or allocate new dentry with case-exact name
2200 * @dentry: the negative dentry that was passed to the parent's lookup func
2201 * @name: the case-exact name to be associated with the returned dentry
2207 * For a case-insensitive lookup match and if the case-exact dentry
2210 * If no entry exists with the exact case name, allocate new dentry with
2213 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2216 struct dentry *found, *res;
2219 * First check if a dentry matching the name already exists,
2222 found = d_hash_and_lookup(dentry->d_parent, name);
2227 if (d_in_lookup(dentry)) {
2228 found = d_alloc_parallel(dentry->d_parent, name,
2229 dentry->d_wait);
2235 found = d_alloc(dentry->d_parent, name);
2252 * d_same_name - compare dentry name with case-exact name
2253 * @parent: parent dentry
2254 * @dentry: the negative dentry that was passed to the parent's lookup func
2255 * @name: the case-exact name to be associated with the returned dentry
2259 bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
2263 if (dentry->d_name.len != name->len)
2265 return dentry_cmp(dentry, name->name, name->len) == 0;
2267 return parent->d_op->d_compare(dentry,
2268 dentry->d_name.len, dentry->d_name.name,
2274 * This is __d_lookup_rcu() when the parent dentry has
2277 static noinline struct dentry *__d_lookup_rcu_op_compare(
2278 const struct dentry *parent,
2285 struct dentry *dentry;
2287 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2293 seq = raw_seqcount_begin(&dentry->d_seq);
2294 if (dentry->d_parent != parent)
2296 if (d_unhashed(dentry))
2298 if (dentry->d_name.hash != hashlen_hash(hashlen))
2300 tlen = dentry->d_name.len;
2301 tname = dentry->d_name.name;
2303 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2307 if (parent->d_op->d_compare(dentry, tlen, tname, name) != 0)
2310 return dentry;
2316 * __d_lookup_rcu - search for a dentry (racy, store-free)
2317 * @parent: parent dentry
2319 * @seqp: returns d_seq value at the point where the dentry was found
2320 * Returns: dentry, or NULL
2329 * held, and rcu_read_lock held. The returned dentry must not be stored into
2333 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2337 * the returned dentry, so long as its parent's seqlock is checked after the
2341 * NOTE! The caller *has* to check the resulting dentry against the sequence
2342 * number we've returned before using any of the resulting dentry state!
2344 struct dentry *__d_lookup_rcu(const struct dentry *parent,
2352 struct dentry *dentry;
2367 * Carefully use d_seq when comparing a candidate dentry, to avoid
2371 * walk here and result in missing our dentry, resulting in the
2377 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2381 * The dentry sequence count protects us from concurrent
2385 * to do anything useful with the returned dentry.
2390 * dentry compare, we will do seqretries until it is stable,
2397 seq = raw_seqcount_begin(&dentry->d_seq);
2398 if (dentry->d_parent != parent)
2400 if (d_unhashed(dentry))
2402 if (dentry->d_name.hash_len != hashlen)
2404 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2407 return dentry;
2413 * d_lookup - search for a dentry
2414 * @parent: parent dentry
2416 * Returns: dentry, or NULL
2418 * d_lookup searches the children of the parent dentry for the name in
2419 * question. If the dentry is found its reference count is incremented and the
2420 * dentry is returned. The caller must use dput to free the entry when it has
2421 * finished using it. %NULL is returned if the dentry does not exist.
2423 struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2425 struct dentry *dentry;
2430 dentry = __d_lookup(parent, name);
2431 if (dentry)
2434 return dentry;
2439 * __d_lookup - search for a dentry (racy)
2440 * @parent: parent dentry
2442 * Returns: dentry, or NULL
2453 struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2458 struct dentry *found = NULL;
2459 struct dentry *dentry;
2471 * Take d_lock when comparing a candidate dentry, to avoid races
2475 * walk here and result in missing our dentry, resulting in the
2483 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2485 if (dentry->d_name.hash != hash)
2488 spin_lock(&dentry->d_lock);
2489 if (dentry->d_parent != parent)
2491 if (d_unhashed(dentry))
2494 if (!d_same_name(dentry, parent, name))
2497 dentry->d_lockref.count++;
2498 found = dentry;
2499 spin_unlock(&dentry->d_lock);
2502 spin_unlock(&dentry->d_lock);
2510 * d_hash_and_lookup - hash the qstr then search for a dentry
2516 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2535 * - turn this dentry into a negative dentry
2536 * - unhash this dentry and free it.
2539 * a negative dentry, but if anybody else is
2540 * currently using the dentry or the inode
2547 * d_delete - delete a dentry
2548 * @dentry: The dentry to delete
2550 * Turn the dentry into a negative dentry if possible, otherwise
2554 void d_delete(struct dentry * dentry)
2556 struct inode *inode = dentry->d_inode;
2559 spin_lock(&dentry->d_lock);
2563 if (dentry->d_lockref.count == 1) {
2564 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2565 dentry_unlink_inode(dentry);
2567 __d_drop(dentry);
2568 spin_unlock(&dentry->d_lock);
2574 static void __d_rehash(struct dentry *entry)
2585 * @entry: dentry to add to the hash
2587 * Adds a dentry to the hash according to its name.
2590 void d_rehash(struct dentry * entry)
2617 static void d_wait_lookup(struct dentry *dentry)
2619 if (d_in_lookup(dentry)) {
2621 add_wait_queue(dentry->d_wait, &wait);
2624 spin_unlock(&dentry->d_lock);
2626 spin_lock(&dentry->d_lock);
2627 } while (d_in_lookup(dentry));
2631 struct dentry *d_alloc_parallel(struct dentry *parent,
2638 struct dentry *new = d_alloc(parent, name);
2639 struct dentry *dentry;
2649 dentry = __d_lookup_rcu(parent, name, &d_seq);
2650 if (unlikely(dentry)) {
2651 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2655 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2657 dput(dentry);
2662 return dentry;
2687 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2688 if (dentry->d_name.hash != hash)
2690 if (dentry->d_parent != parent)
2692 if (!d_same_name(dentry, parent, name))
2696 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2706 spin_lock(&dentry->d_lock);
2707 d_wait_lookup(dentry);
2714 if (unlikely(dentry->d_name.hash != hash))
2716 if (unlikely(dentry->d_parent != parent))
2718 if (unlikely(d_unhashed(dentry)))
2720 if (unlikely(!d_same_name(dentry, parent, name)))
2723 spin_unlock(&dentry->d_lock);
2725 return dentry;
2735 spin_unlock(&dentry->d_lock);
2736 dput(dentry);
2742 * - Unhash the dentry
2743 * - Retrieve and clear the waitqueue head in dentry
2746 static wait_queue_head_t *__d_lookup_unhash(struct dentry *dentry)
2751 lockdep_assert_held(&dentry->d_lock);
2753 b = in_lookup_hash(dentry->d_parent, dentry->d_name.hash);
2755 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
2756 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
2757 d_wait = dentry->d_wait;
2758 dentry->d_wait = NULL;
2760 INIT_HLIST_NODE(&dentry->d_u.d_alias);
2761 INIT_LIST_HEAD(&dentry->d_lru);
2765 void __d_lookup_unhash_wake(struct dentry *dentry)
2767 spin_lock(&dentry->d_lock);
2768 wake_up_all(__d_lookup_unhash(dentry));
2769 spin_unlock(&dentry->d_lock);
2775 static inline void __d_add(struct dentry *dentry, struct inode *inode)
2780 spin_lock(&dentry->d_lock);
2781 if (unlikely(d_in_lookup(dentry))) {
2782 dir = dentry->d_parent->d_inode;
2784 d_wait = __d_lookup_unhash(dentry);
2788 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2789 raw_write_seqcount_begin(&dentry->d_seq);
2790 __d_set_inode_and_type(dentry, inode, add_flags);
2791 raw_write_seqcount_end(&dentry->d_seq);
2792 fsnotify_update_flags(dentry);
2794 __d_rehash(dentry);
2797 spin_unlock(&dentry->d_lock);
2803 * d_add - add dentry to hash queues
2804 * @entry: dentry to add
2805 * @inode: The inode to attach to this dentry
2811 void d_add(struct dentry *entry, struct inode *inode)
2823 * @entry: dentry to add
2824 * @inode: The inode to go with this dentry
2826 * If an unhashed dentry with the same name/parent and desired
2832 struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2834 struct dentry *alias;
2867 static void swap_names(struct dentry *dentry, struct dentry *target)
2870 if (unlikely(dname_external(dentry))) {
2874 swap(target->d_name.name, dentry->d_name.name);
2877 * dentry:internal, target:external. Steal target's
2880 memcpy(target->d_iname, dentry->d_name.name,
2881 dentry->d_name.len + 1);
2882 dentry->d_name.name = target->d_name.name;
2886 if (unlikely(dname_external(dentry))) {
2888 * dentry:external, target:internal. Give dentry's
2889 * storage to target and make dentry internal
2891 memcpy(dentry->d_iname, target->d_name.name,
2893 target->d_name.name = dentry->d_name.name;
2894 dentry->d_name.name = dentry->d_iname;
2902 swap(((long *) &dentry->d_iname)[i],
2907 swap(dentry->d_name.hash_len, target->d_name.hash_len);
2910 static void copy_name(struct dentry *dentry, struct dentry *target)
2913 if (unlikely(dname_external(dentry)))
2914 old_name = external_name(dentry);
2917 dentry->d_name = target->d_name;
2919 memcpy(dentry->d_iname, target->d_name.name,
2921 dentry->d_name.name = dentry->d_iname;
2922 dentry->d_name.hash_len = target->d_name.hash_len;
2929 * __d_move - move a dentry
2930 * @dentry: entry to move
2931 * @target: new dentry
2939 static void __d_move(struct dentry *dentry, struct dentry *target,
2942 struct dentry *old_parent, *p;
2947 WARN_ON(!dentry->d_inode);
2948 if (WARN_ON(dentry == target))
2951 BUG_ON(d_ancestor(target, dentry));
2952 old_parent = dentry->d_parent;
2954 if (IS_ROOT(dentry)) {
2958 /* target is not a descendent of dentry->d_parent */
2962 BUG_ON(p == dentry);
2968 spin_lock_nested(&dentry->d_lock, 2);
2977 write_seqcount_begin(&dentry->d_seq);
2981 if (!d_unhashed(dentry))
2982 ___d_drop(dentry);
2987 dentry->d_parent = target->d_parent;
2989 copy_name(dentry, target);
2991 dentry->d_parent->d_lockref.count++;
2992 if (dentry != old_parent) /* wasn't IS_ROOT */
2996 swap_names(dentry, target);
3001 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
3002 __d_rehash(dentry);
3003 fsnotify_update_flags(dentry);
3004 fscrypt_handle_d_move(dentry);
3007 write_seqcount_end(&dentry->d_seq);
3012 if (dentry->d_parent != old_parent)
3013 spin_unlock(&dentry->d_parent->d_lock);
3014 if (dentry != old_parent)
3017 spin_unlock(&dentry->d_lock);
3021 * d_move - move a dentry
3022 * @dentry: entry to move
3023 * @target: new dentry
3029 void d_move(struct dentry *dentry, struct dentry *target)
3032 __d_move(dentry, target, false);
3039 * @dentry1: first dentry
3040 * @dentry2: second dentry
3042 void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
3058 * @p1: ancestor dentry
3059 * @p2: child dentry
3061 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
3064 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
3066 struct dentry *p;
3079 * dentry->d_parent->d_inode->i_mutex, and rename_lock
3085 struct dentry *dentry, struct dentry *alias)
3091 /* If alias and dentry share a parent, then no extra locks required */
3092 if (alias->d_parent == dentry->d_parent)
3096 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
3098 m1 = &dentry->d_sb->s_vfs_rename_mutex;
3103 __d_move(alias, dentry, false);
3114 * d_splice_alias - splice a disconnected dentry into the tree if one exists
3115 * @inode: the inode which may have a disconnected dentry
3116 * @dentry: a negative dentry which we want to point to the inode.
3119 * place of the given dentry and return it, else simply d_add the inode
3120 * to the dentry and return NULL.
3128 * If a dentry was found and moved, then it is returned. Otherwise NULL
3131 * Cluster filesystems may call this function with a negative, hashed dentry.
3133 * will only occur during atomic_open. So we need to check for the dentry
3136 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3141 BUG_ON(!d_unhashed(dentry));
3146 security_d_instantiate(dentry, inode);
3149 struct dentry *new = __d_find_any_alias(inode);
3154 if (unlikely(d_ancestor(new, dentry))) {
3161 dentry->d_name.name,
3165 struct dentry *old_parent = dget(new->d_parent);
3166 int err = __d_unalias(inode, dentry, new);
3174 __d_move(new, dentry, false);
3182 __d_add(dentry, inode);
3194 * is_subdir - is new dentry a subdirectory of old_dentry
3195 * @new_dentry: new dentry
3196 * @old_dentry: old dentry
3203 bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3230 static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3232 struct dentry *root = data;
3233 if (dentry != root) {
3234 if (d_unhashed(dentry) || !dentry->d_inode)
3237 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3238 dentry->d_flags |= DCACHE_GENOCIDE;
3239 dentry->d_lockref.count--;
3245 void d_genocide(struct dentry *parent)
3252 struct dentry *dentry = file->f_path.dentry;
3255 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3256 !hlist_unhashed(&dentry->d_u.d_alias) ||
3257 !d_unlinked(dentry));
3258 spin_lock(&dentry->d_parent->d_lock);
3259 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3260 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3262 spin_unlock(&dentry->d_lock);
3263 spin_unlock(&dentry->d_parent->d_lock);
3264 d_instantiate(dentry, inode);
3306 dentry_cache = KMEM_CACHE_USERCOPY(dentry,