457c899653991 (Thomas Gleixner 2019-05-19 13:08:55 +0100 1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * fs/dcache.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * Complete reimplementation
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) * (C) 1997 Thomas Schoebel-Theuer,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7) * with heavy changes by Linus Torvalds
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) * Notes on the allocation strategy:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) * The dcache is a master of the icache - whenever a dcache entry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) * exists, the inode will always exist. "iput()" is done either when
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) * the dcache entry is deleted or garbage collected.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17)
7a5cf791a7476 (Al Viro 2018-03-05 19:15:50 -0500 18) #include <linux/ratelimit.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) #include <linux/string.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) #include <linux/fs.h>
0bf3d5c1604ec (Eric Biggers 2019-03-20 11:39:11 -0700 22) #include <linux/fscrypt.h>
7a91bf7f5c22c (John McCutchan 2005-08-08 13:52:16 -0400 23) #include <linux/fsnotify.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) #include <linux/init.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 26) #include <linux/hash.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) #include <linux/cache.h>
630d9c47274aa (Paul Gortmaker 2011-11-16 23:57:37 -0500 28) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29) #include <linux/security.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30) #include <linux/seqlock.h>
57c8a661d95df (Mike Rapoport 2018-10-30 15:09:49 -0700 31) #include <linux/memblock.h>
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 32) #include <linux/bit_spinlock.h>
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 33) #include <linux/rculist_bl.h>
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 34) #include <linux/list_lru.h>
07f3f05c1e305 (David Howells 2006-09-30 20:52:18 +0200 35) #include "internal.h"
b2dba1af3c415 (Al Viro 2011-11-23 19:26:23 -0500 36) #include "mount.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37)
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 38) /*
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 39) * Usage:
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 40) * dcache->d_inode->i_lock protects:
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 41) * - i_dentry, d_u.d_alias, d_inode of aliases
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 42) * dcache_hash_bucket lock protects:
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 43) * - the dcache hash table
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 44) * s_roots bl list spinlock protects:
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 45) * - the s_roots list (see __d_drop)
19156840e33a2 (Dave Chinner 2013-08-28 10:17:55 +1000 46) * dentry->d_sb->s_dentry_lru_lock protects:
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 47) * - the dcache lru lists and counters
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 48) * d_lock protects:
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 49) * - d_flags
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 50) * - d_name
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 51) * - d_lru
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 52) * - d_count
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 53) * - d_unhashed()
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 54) * - d_parent and d_subdirs
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 55) * - childrens' d_child and d_parent
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 56) * - d_u.d_alias, d_inode
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 57) *
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 58) * Ordering:
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 59) * dentry->d_inode->i_lock
b5c84bf6f6fa3 (Nicholas Piggin 2011-01-07 17:49:38 +1100 60) * dentry->d_lock
19156840e33a2 (Dave Chinner 2013-08-28 10:17:55 +1000 61) * dentry->d_sb->s_dentry_lru_lock
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 62) * dcache_hash_bucket lock
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 63) * s_roots lock
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 64) *
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 65) * If there is an ancestor relationship:
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 66) * dentry->d_parent->...->d_parent->d_lock
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 67) * ...
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 68) * dentry->d_parent->d_lock
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 69) * dentry->d_lock
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 70) *
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 71) * If no ancestor relationship:
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 72) * arbitrary, since it's serialized on rename_lock
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 73) */
fa3536cc144c1 (Eric Dumazet 2006-03-26 01:37:24 -0800 74) int sysctl_vfs_cache_pressure __read_mostly = 100;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76)
74c3cbe33bc07 (Al Viro 2007-07-22 08:04:18 -0400 77) __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78)
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 79) EXPORT_SYMBOL(rename_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 80)
e18b890bb0881 (Christoph Lameter 2006-12-06 20:33:20 -0800 81) static struct kmem_cache *dentry_cache __read_mostly;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 82)
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 83) const struct qstr empty_name = QSTR_INIT("", 0);
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 84) EXPORT_SYMBOL(empty_name);
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 85) const struct qstr slash_name = QSTR_INIT("/", 1);
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 86) EXPORT_SYMBOL(slash_name);
80e5d1ff5d5f1 (Al Viro 2021-04-15 19:46:50 -0400 87) const struct qstr dotdot_name = QSTR_INIT("..", 2);
80e5d1ff5d5f1 (Al Viro 2021-04-15 19:46:50 -0400 88) EXPORT_SYMBOL(dotdot_name);
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 89)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 90) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 91) * This is the single most critical data structure when it comes
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 92) * to the dcache: the hashtable for lookups. Somebody should try
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 93) * to make this good - I've just made it work.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 94) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 95) * This hash-function tries to avoid losing too many bits of hash
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 96) * information, yet avoid using a prime hash-size or similar.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 97) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 98)
fa3536cc144c1 (Eric Dumazet 2006-03-26 01:37:24 -0800 99) static unsigned int d_hash_shift __read_mostly;
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 100)
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 101) static struct hlist_bl_head *dentry_hashtable __read_mostly;
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 102)
8387ff2577eb9 (Linus Torvalds 2016-06-10 07:51:30 -0700 103) static inline struct hlist_bl_head *d_hash(unsigned int hash)
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 104) {
854d3e63438d7 (Alexey Dobriyan 2017-11-20 18:05:07 +0300 105) return dentry_hashtable + (hash >> d_hash_shift);
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 106) }
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 107)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 108) #define IN_LOOKUP_SHIFT 10
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 109) static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 110)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 111) static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 112) unsigned int hash)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 113) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 114) hash += (unsigned long) parent / L1_CACHE_BYTES;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 115) return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 116) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 117)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 118)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 119) /* Statistics gathering. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 120) struct dentry_stat_t dentry_stat = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121) .age_limit = 45,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 122) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123)
3942c07ccf98e (Glauber Costa 2013-08-28 10:17:53 +1000 124) static DEFINE_PER_CPU(long, nr_dentry);
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 125) static DEFINE_PER_CPU(long, nr_dentry_unused);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 126) static DEFINE_PER_CPU(long, nr_dentry_negative);
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 127)
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 128) #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 129)
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 130) /*
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 131) * Here we resort to our own counters instead of using generic per-cpu counters
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 132) * for consistency with what the vfs inode code does. We are expected to harvest
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 133) * better code and performance by having our own specialized counters.
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 134) *
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 135) * Please note that the loop is done over all possible CPUs, not over all online
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 136) * CPUs. The reason for this is that we don't want to play games with CPUs going
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 137) * on and off. If one of them goes off, we will just keep their counters.
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 138) *
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 139) * glommer: See cffbc8a for details, and if you ever intend to change this,
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 140) * please update all vfs counters to match.
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 141) */
3942c07ccf98e (Glauber Costa 2013-08-28 10:17:53 +1000 142) static long get_nr_dentry(void)
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 143) {
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 144) int i;
3942c07ccf98e (Glauber Costa 2013-08-28 10:17:53 +1000 145) long sum = 0;
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 146) for_each_possible_cpu(i)
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 147) sum += per_cpu(nr_dentry, i);
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 148) return sum < 0 ? 0 : sum;
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 149) }
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 150)
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 151) static long get_nr_dentry_unused(void)
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 152) {
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 153) int i;
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 154) long sum = 0;
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 155) for_each_possible_cpu(i)
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 156) sum += per_cpu(nr_dentry_unused, i);
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 157) return sum < 0 ? 0 : sum;
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 158) }
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 159)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 160) static long get_nr_dentry_negative(void)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 161) {
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 162) int i;
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 163) long sum = 0;
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 164)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 165) for_each_possible_cpu(i)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 166) sum += per_cpu(nr_dentry_negative, i);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 167) return sum < 0 ? 0 : sum;
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 168) }
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 169)
32927393dc1cc (Christoph Hellwig 2020-04-24 08:43:38 +0200 170) int proc_nr_dentry(struct ctl_table *table, int write, void *buffer,
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 171) size_t *lenp, loff_t *ppos)
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 172) {
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 173) dentry_stat.nr_dentry = get_nr_dentry();
62d36c7703521 (Dave Chinner 2013-08-28 10:17:54 +1000 174) dentry_stat.nr_unused = get_nr_dentry_unused();
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 175) dentry_stat.nr_negative = get_nr_dentry_negative();
3942c07ccf98e (Glauber Costa 2013-08-28 10:17:53 +1000 176) return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 177) }
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 178) #endif
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 179)
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 180) /*
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 181) * Compare 2 name strings, return 0 if they match, otherwise non-zero.
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 182) * The strings are both count bytes long, and count is non-zero.
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 183) */
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 184) #ifdef CONFIG_DCACHE_WORD_ACCESS
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 185)
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 186) #include <asm/word-at-a-time.h>
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 187) /*
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 188) * NOTE! 'cs' and 'scount' come from a dentry, so it has a
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 189) * aligned allocation for this particular component. We don't
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 190) * strictly need the load_unaligned_zeropad() safety, but it
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 191) * doesn't hurt either.
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 192) *
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 193) * In contrast, 'ct' and 'tcount' can be from a pathname, and do
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 194) * need the careful unaligned handling.
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 195) */
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 196) static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 197) {
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 198) unsigned long a,b,mask;
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 199)
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 200) for (;;) {
bfe7aa6c39b12 (Andrey Ryabinin 2018-02-01 21:00:51 +0300 201) a = read_word_at_a_time(cs);
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 202) b = load_unaligned_zeropad(ct);
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 203) if (tcount < sizeof(unsigned long))
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 204) break;
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 205) if (unlikely(a != b))
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 206) return 1;
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 207) cs += sizeof(unsigned long);
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 208) ct += sizeof(unsigned long);
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 209) tcount -= sizeof(unsigned long);
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 210) if (!tcount)
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 211) return 0;
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 212) }
a5c21dcefa1c3 (Will Deacon 2013-12-12 17:40:21 +0000 213) mask = bytemask_from_count(tcount);
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 214) return unlikely(!!((a ^ b) & mask));
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 215) }
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 216)
bfcfaa77bdf0f (Linus Torvalds 2012-03-06 11:16:17 -0800 217) #else
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 218)
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 219) static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 220) {
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 221) do {
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 222) if (*cs != *ct)
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 223) return 1;
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 224) cs++;
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 225) ct++;
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 226) tcount--;
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 227) } while (tcount);
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 228) return 0;
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 229) }
5483f18e986ed (Linus Torvalds 2012-03-04 15:51:42 -0800 230)
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 231) #endif
e419b4cc58568 (Linus Torvalds 2012-05-03 10:16:43 -0700 232)
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 233) static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 234) {
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 235) /*
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 236) * Be careful about RCU walk racing with rename:
506458efaf153 (Will Deacon 2017-10-24 11:22:48 +0100 237) * use 'READ_ONCE' to fetch the name pointer.
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 238) *
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 239) * NOTE! Even if a rename will mean that the length
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 240) * was not loaded atomically, we don't care. The
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 241) * RCU walk will check the sequence count eventually,
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 242) * and catch it. And we won't overrun the buffer,
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 243) * because we're reading the name pointer atomically,
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 244) * and a dentry name is guaranteed to be properly
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 245) * terminated with a NUL byte.
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 246) *
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 247) * End result: even if 'len' is wrong, we'll exit
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 248) * early because the data cannot match (there can
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 249) * be no NUL in the ct/tcount data)
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 250) */
506458efaf153 (Will Deacon 2017-10-24 11:22:48 +0100 251) const unsigned char *cs = READ_ONCE(dentry->d_name.name);
ae0a843c740b4 (He Kuang 2016-03-26 09:12:10 +0000 252)
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 253) return dentry_string_cmp(cs, ct, tcount);
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 254) }
94753db5ed9ad (Linus Torvalds 2012-05-10 12:19:19 -0700 255)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 256) struct external_name {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 257) union {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 258) atomic_t count;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 259) struct rcu_head head;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 260) } u;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 261) unsigned char name[];
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 262) };
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 263)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 264) static inline struct external_name *external_name(struct dentry *dentry)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 265) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 266) return container_of(dentry->d_name.name, struct external_name, name[0]);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 267) }
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 268)
9c82ab9c9e16c (Christoph Hellwig 2010-10-10 05:36:22 -0400 269) static void __d_free(struct rcu_head *head)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 270) {
9c82ab9c9e16c (Christoph Hellwig 2010-10-10 05:36:22 -0400 271) struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
9c82ab9c9e16c (Christoph Hellwig 2010-10-10 05:36:22 -0400 272)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 273) kmem_cache_free(dentry_cache, dentry);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 274) }
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 275)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 276) static void __d_free_external(struct rcu_head *head)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 277) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 278) struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 279) kfree(external_name(dentry));
f1782c9bc5477 (Roman Gushchin 2018-04-10 16:27:44 -0700 280) kmem_cache_free(dentry_cache, dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 281) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 282)
810bb172671ae (Al Viro 2014-10-12 12:45:37 -0400 283) static inline int dname_external(const struct dentry *dentry)
810bb172671ae (Al Viro 2014-10-12 12:45:37 -0400 284) {
810bb172671ae (Al Viro 2014-10-12 12:45:37 -0400 285) return dentry->d_name.name != dentry->d_iname;
810bb172671ae (Al Viro 2014-10-12 12:45:37 -0400 286) }
810bb172671ae (Al Viro 2014-10-12 12:45:37 -0400 287)
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 288) void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 289) {
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 290) spin_lock(&dentry->d_lock);
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 291) name->name = dentry->d_name;
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 292) if (unlikely(dname_external(dentry))) {
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 293) atomic_inc(&external_name(dentry)->u.count);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 294) } else {
6cd00a01f0c1a (Tetsuo Handa 2018-08-17 15:44:34 -0700 295) memcpy(name->inline_name, dentry->d_iname,
6cd00a01f0c1a (Tetsuo Handa 2018-08-17 15:44:34 -0700 296) dentry->d_name.len + 1);
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 297) name->name.name = name->inline_name;
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 298) }
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 299) spin_unlock(&dentry->d_lock);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 300) }
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 301) EXPORT_SYMBOL(take_dentry_name_snapshot);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 302)
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 303) void release_dentry_name_snapshot(struct name_snapshot *name)
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 304) {
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 305) if (unlikely(name->name.name != name->inline_name)) {
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 306) struct external_name *p;
230c6402b1b30 (Al Viro 2019-04-26 13:07:27 -0400 307) p = container_of(name->name.name, struct external_name, name[0]);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 308) if (unlikely(atomic_dec_and_test(&p->u.count)))
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 309) kfree_rcu(p, u.head);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 310) }
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 311) }
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 312) EXPORT_SYMBOL(release_dentry_name_snapshot);
49d31c2f389ac (Al Viro 2017-07-07 14:51:19 -0400 313)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 314) static inline void __d_set_inode_and_type(struct dentry *dentry,
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 315) struct inode *inode,
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 316) unsigned type_flags)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 317) {
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 318) unsigned flags;
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 319)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 320) dentry->d_inode = inode;
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 321) flags = READ_ONCE(dentry->d_flags);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 322) flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 323) flags |= type_flags;
2fa6b1e01a9b1 (Al Viro 2019-11-12 16:13:06 -0500 324) smp_store_release(&dentry->d_flags, flags);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 325) }
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 326)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 327) static inline void __d_clear_type_and_inode(struct dentry *dentry)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 328) {
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 329) unsigned flags = READ_ONCE(dentry->d_flags);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 330)
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 331) flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 332) WRITE_ONCE(dentry->d_flags, flags);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 333) dentry->d_inode = NULL;
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 334) if (dentry->d_flags & DCACHE_LRU_LIST)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 335) this_cpu_inc(nr_dentry_negative);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 336) }
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 337)
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 338) static void dentry_free(struct dentry *dentry)
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 339) {
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 340) WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 341) if (unlikely(dname_external(dentry))) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 342) struct external_name *p = external_name(dentry);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 343) if (likely(atomic_dec_and_test(&p->u.count))) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 344) call_rcu(&dentry->d_u.d_rcu, __d_free_external);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 345) return;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 346) }
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 347) }
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 348) /* if dentry was never visible to RCU, immediate free is OK */
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 349) if (dentry->d_flags & DCACHE_NORCU)
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 350) __d_free(&dentry->d_u.d_rcu);
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 351) else
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 352) call_rcu(&dentry->d_u.d_rcu, __d_free);
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 353) }
b4f0354e968f5 (Al Viro 2014-04-29 23:40:14 -0400 354)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 355) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 356) * Release the dentry's inode, using the filesystem
550dce01dd606 (Al Viro 2016-05-29 20:13:30 -0400 357) * d_iput() operation if defined.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 358) */
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 359) static void dentry_unlink_inode(struct dentry * dentry)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 360) __releases(dentry->d_lock)
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 361) __releases(dentry->d_inode->i_lock)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 362) {
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 363) struct inode *inode = dentry->d_inode;
a528aca7f359f (Al Viro 2016-02-29 12:12:46 -0500 364)
4c0d7cd5c8416 (Al Viro 2018-08-09 10:15:54 -0400 365) raw_write_seqcount_begin(&dentry->d_seq);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 366) __d_clear_type_and_inode(dentry);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 367) hlist_del_init(&dentry->d_u.d_alias);
4c0d7cd5c8416 (Al Viro 2018-08-09 10:15:54 -0400 368) raw_write_seqcount_end(&dentry->d_seq);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 369) spin_unlock(&dentry->d_lock);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 370) spin_unlock(&inode->i_lock);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 371) if (!inode->i_nlink)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 372) fsnotify_inoderemove(inode);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 373) if (dentry->d_op && dentry->d_op->d_iput)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 374) dentry->d_op->d_iput(dentry, inode);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 375) else
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 376) iput(inode);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 377) }
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 378)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 379) /*
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 380) * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 381) * is in use - which includes both the "real" per-superblock
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 382) * LRU list _and_ the DCACHE_SHRINK_LIST use.
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 383) *
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 384) * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 385) * on the shrink list (ie not on the superblock LRU list).
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 386) *
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 387) * The per-cpu "nr_dentry_unused" counters are updated with
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 388) * the DCACHE_LRU_LIST bit.
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 389) *
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 390) * The per-cpu "nr_dentry_negative" counters are only updated
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 391) * when deleted from or added to the per-superblock LRU list, not
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 392) * from/to the shrink list. That is to avoid an unneeded dec/inc
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 393) * pair when moving from LRU to shrink list in select_collect().
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 394) *
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 395) * These helper functions make sure we always follow the
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 396) * rules. d_lock must be held by the caller.
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 397) */
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 398) #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 399) static void d_lru_add(struct dentry *dentry)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 400) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 401) D_FLAG_VERIFY(dentry, 0);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 402) dentry->d_flags |= DCACHE_LRU_LIST;
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 403) this_cpu_inc(nr_dentry_unused);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 404) if (d_is_negative(dentry))
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 405) this_cpu_inc(nr_dentry_negative);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 406) WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 407) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 408)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 409) static void d_lru_del(struct dentry *dentry)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 410) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 411) D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 412) dentry->d_flags &= ~DCACHE_LRU_LIST;
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 413) this_cpu_dec(nr_dentry_unused);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 414) if (d_is_negative(dentry))
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 415) this_cpu_dec(nr_dentry_negative);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 416) WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 417) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 418)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 419) static void d_shrink_del(struct dentry *dentry)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 420) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 421) D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 422) list_del_init(&dentry->d_lru);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 423) dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 424) this_cpu_dec(nr_dentry_unused);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 425) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 426)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 427) static void d_shrink_add(struct dentry *dentry, struct list_head *list)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 428) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 429) D_FLAG_VERIFY(dentry, 0);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 430) list_add(&dentry->d_lru, list);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 431) dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 432) this_cpu_inc(nr_dentry_unused);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 433) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 434)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 435) /*
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 436) * These can only be called under the global LRU lock, ie during the
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 437) * callback for freeing the LRU list. "isolate" removes it from the
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 438) * LRU lists entirely, while shrink_move moves it to the indicated
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 439) * private list.
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 440) */
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 441) static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 442) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 443) D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 444) dentry->d_flags &= ~DCACHE_LRU_LIST;
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 445) this_cpu_dec(nr_dentry_unused);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 446) if (d_is_negative(dentry))
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 447) this_cpu_dec(nr_dentry_negative);
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 448) list_lru_isolate(lru, &dentry->d_lru);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 449) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 450)
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 451) static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 452) struct list_head *list)
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 453) {
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 454) D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 455) dentry->d_flags |= DCACHE_SHRINK_LIST;
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 456) if (d_is_negative(dentry))
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 457) this_cpu_dec(nr_dentry_negative);
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 458) list_lru_isolate_move(lru, &dentry->d_lru, list);
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 459) }
89dc77bcdabf4 (Linus Torvalds 2013-09-13 22:55:10 -0400 460)
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 461) static void ___d_drop(struct dentry *dentry)
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 462) {
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 463) struct hlist_bl_head *b;
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 464) /*
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 465) * Hashed dentries are normally on the dentry hashtable,
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 466) * with the exception of those newly allocated by
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 467) * d_obtain_root, which are always IS_ROOT:
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 468) */
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 469) if (unlikely(IS_ROOT(dentry)))
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 470) b = &dentry->d_sb->s_roots;
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 471) else
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 472) b = d_hash(dentry->d_name.hash);
b61625d24596e (Al Viro 2013-10-04 11:09:01 -0400 473)
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 474) hlist_bl_lock(b);
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 475) __hlist_bl_del(&dentry->d_hash);
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 476) hlist_bl_unlock(b);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 477) }
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 478)
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 479) void __d_drop(struct dentry *dentry)
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 480) {
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 481) if (!d_unhashed(dentry)) {
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 482) ___d_drop(dentry);
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 483) dentry->d_hash.pprev = NULL;
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 484) write_seqcount_invalidate(&dentry->d_seq);
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 485) }
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 486) }
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 487) EXPORT_SYMBOL(__d_drop);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 488)
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 489) /**
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 490) * d_drop - drop a dentry
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 491) * @dentry: dentry to drop
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 492) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 493) * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 494) * be found through a VFS lookup any more. Note that this is different from
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 495) * deleting the dentry - d_delete will try to mark the dentry negative if
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 496) * possible, giving a successful _negative_ lookup, while d_drop will
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 497) * just make the cache lookup fail.
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 498) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 499) * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 500) * reason (NFS timeouts or autofs deletes).
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 501) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 502) * __d_drop requires dentry->d_lock
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 503) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 504) * ___d_drop doesn't mark dentry as "unhashed"
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 505) * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 506) */
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 507) void d_drop(struct dentry *dentry)
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 508) {
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 509) spin_lock(&dentry->d_lock);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 510) __d_drop(dentry);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 511) spin_unlock(&dentry->d_lock);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 512) }
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 513) EXPORT_SYMBOL(d_drop);
789680d1ee931 (Nicholas Piggin 2011-01-07 17:49:30 +1100 514)
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 515) static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 516) {
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 517) struct dentry *next;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 518) /*
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 519) * Inform d_walk() and shrink_dentry_list() that we are no longer
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 520) * attached to the dentry tree
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 521) */
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 522) dentry->d_flags |= DCACHE_DENTRY_KILLED;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 523) if (unlikely(list_empty(&dentry->d_child)))
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 524) return;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 525) __list_del_entry(&dentry->d_child);
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 526) /*
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 527) * Cursors can move around the list of children. While we'd been
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 528) * a normal list member, it didn't matter - ->d_child.next would've
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 529) * been updated. However, from now on it won't be and for the
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 530) * things like d_walk() it might end up with a nasty surprise.
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 531) * Normally d_walk() doesn't care about cursors moving around -
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 532) * ->d_lock on parent prevents that and since a cursor has no children
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 533) * of its own, we get through it without ever unlocking the parent.
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 534) * There is one exception, though - if we ascend from a child that
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 535) * gets killed as soon as we unlock it, the next sibling is found
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 536) * using the value left in its ->d_child.next. And if _that_
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 537) * pointed to a cursor, and cursor got moved (e.g. by lseek())
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 538) * before d_walk() regains parent->d_lock, we'll end up skipping
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 539) * everything the cursor had been moved past.
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 540) *
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 541) * Solution: make sure that the pointer left behind in ->d_child.next
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 542) * points to something that won't be moving around. I.e. skip the
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 543) * cursors.
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 544) */
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 545) while (dentry->d_child.next != &parent->d_subdirs) {
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 546) next = list_entry(dentry->d_child.next, struct dentry, d_child);
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 547) if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 548) break;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 549) dentry->d_child.next = next->d_child.next;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 550) }
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 551) }
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 552)
e55fd011549ea (Al Viro 2014-05-28 13:51:12 -0400 553) static void __dentry_kill(struct dentry *dentry)
77812a1ef139d (Nicholas Piggin 2011-01-07 17:49:48 +1100 554) {
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 555) struct dentry *parent = NULL;
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 556) bool can_free = true;
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 557) if (!IS_ROOT(dentry))
77812a1ef139d (Nicholas Piggin 2011-01-07 17:49:48 +1100 558) parent = dentry->d_parent;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 559)
0d98439ea3c6f (Linus Torvalds 2013-09-08 13:46:52 -0700 560) /*
0d98439ea3c6f (Linus Torvalds 2013-09-08 13:46:52 -0700 561) * The dentry is now unrecoverably dead to the world.
0d98439ea3c6f (Linus Torvalds 2013-09-08 13:46:52 -0700 562) */
0d98439ea3c6f (Linus Torvalds 2013-09-08 13:46:52 -0700 563) lockref_mark_dead(&dentry->d_lockref);
0d98439ea3c6f (Linus Torvalds 2013-09-08 13:46:52 -0700 564)
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 565) /*
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 566) * inform the fs via d_prune that this dentry is about to be
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 567) * unhashed and destroyed.
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 568) */
2926620145095 (Al Viro 2014-05-30 11:39:02 -0400 569) if (dentry->d_flags & DCACHE_OP_PRUNE)
61572bb1f40b9 (Yan, Zheng 2013-04-15 14:13:21 +0800 570) dentry->d_op->d_prune(dentry);
61572bb1f40b9 (Yan, Zheng 2013-04-15 14:13:21 +0800 571)
01b6035190b02 (Al Viro 2014-04-29 23:42:52 -0400 572) if (dentry->d_flags & DCACHE_LRU_LIST) {
01b6035190b02 (Al Viro 2014-04-29 23:42:52 -0400 573) if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
01b6035190b02 (Al Viro 2014-04-29 23:42:52 -0400 574) d_lru_del(dentry);
01b6035190b02 (Al Viro 2014-04-29 23:42:52 -0400 575) }
77812a1ef139d (Nicholas Piggin 2011-01-07 17:49:48 +1100 576) /* if it was on the hash then remove it */
77812a1ef139d (Nicholas Piggin 2011-01-07 17:49:48 +1100 577) __d_drop(dentry);
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 578) dentry_unlist(dentry, parent);
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 579) if (parent)
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 580) spin_unlock(&parent->d_lock);
550dce01dd606 (Al Viro 2016-05-29 20:13:30 -0400 581) if (dentry->d_inode)
550dce01dd606 (Al Viro 2016-05-29 20:13:30 -0400 582) dentry_unlink_inode(dentry);
550dce01dd606 (Al Viro 2016-05-29 20:13:30 -0400 583) else
550dce01dd606 (Al Viro 2016-05-29 20:13:30 -0400 584) spin_unlock(&dentry->d_lock);
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 585) this_cpu_dec(nr_dentry);
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 586) if (dentry->d_op && dentry->d_op->d_release)
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 587) dentry->d_op->d_release(dentry);
03b3b889e79cd (Al Viro 2014-04-29 15:45:28 -0400 588)
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 589) spin_lock(&dentry->d_lock);
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 590) if (dentry->d_flags & DCACHE_SHRINK_LIST) {
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 591) dentry->d_flags |= DCACHE_MAY_FREE;
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 592) can_free = false;
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 593) }
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 594) spin_unlock(&dentry->d_lock);
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 595) if (likely(can_free))
41edf278fc2f0 (Al Viro 2014-05-01 10:30:00 -0400 596) dentry_free(dentry);
9c5f1d30199d0 (Al Viro 2018-04-15 18:28:48 -0400 597) cond_resched();
e55fd011549ea (Al Viro 2014-05-28 13:51:12 -0400 598) }
e55fd011549ea (Al Viro 2014-05-28 13:51:12 -0400 599)
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 600) static struct dentry *__lock_parent(struct dentry *dentry)
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 601) {
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 602) struct dentry *parent;
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 603) rcu_read_lock();
c2338f2dc7c1e (Al Viro 2014-06-12 00:29:13 -0400 604) spin_unlock(&dentry->d_lock);
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 605) again:
66702eb59064f (Mark Rutland 2017-10-23 14:07:14 -0700 606) parent = READ_ONCE(dentry->d_parent);
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 607) spin_lock(&parent->d_lock);
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 608) /*
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 609) * We can't blindly lock dentry until we are sure
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 610) * that we won't violate the locking order.
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 611) * Any changes of dentry->d_parent must have
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 612) * been done with parent->d_lock held, so
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 613) * spin_lock() above is enough of a barrier
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 614) * for checking if it's still our child.
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 615) */
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 616) if (unlikely(parent != dentry->d_parent)) {
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 617) spin_unlock(&parent->d_lock);
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 618) goto again;
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 619) }
65d8eb5a8f548 (Al Viro 2018-02-23 22:07:35 -0500 620) rcu_read_unlock();
65d8eb5a8f548 (Al Viro 2018-02-23 22:07:35 -0500 621) if (parent != dentry)
9f12600fe425b (Linus Torvalds 2014-05-31 09:13:21 -0700 622) spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
65d8eb5a8f548 (Al Viro 2018-02-23 22:07:35 -0500 623) else
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 624) parent = NULL;
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 625) return parent;
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 626) }
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 627)
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 628) static inline struct dentry *lock_parent(struct dentry *dentry)
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 629) {
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 630) struct dentry *parent = dentry->d_parent;
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 631) if (IS_ROOT(dentry))
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 632) return NULL;
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 633) if (likely(spin_trylock(&parent->d_lock)))
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 634) return parent;
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 635) return __lock_parent(dentry);
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 636) }
8b987a46a1e0e (Al Viro 2018-02-23 22:11:34 -0500 637)
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 638) static inline bool retain_dentry(struct dentry *dentry)
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 639) {
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 640) WARN_ON(d_in_lookup(dentry));
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 641)
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 642) /* Unreachable? Get rid of it */
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 643) if (unlikely(d_unhashed(dentry)))
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 644) return false;
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 645)
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 646) if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 647) return false;
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 648)
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 649) if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 650) if (dentry->d_op->d_delete(dentry))
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 651) return false;
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 652) }
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 653)
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 654) if (unlikely(dentry->d_flags & DCACHE_DONTCACHE))
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 655) return false;
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 656)
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 657) /* retain; LRU fodder */
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 658) dentry->d_lockref.count--;
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 659) if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 660) d_lru_add(dentry);
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 661) else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
62d9956cefe6e (Al Viro 2018-03-06 21:37:31 -0500 662) dentry->d_flags |= DCACHE_REFERENCED;
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 663) return true;
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 664) }
a338579f2f3d6 (Al Viro 2018-02-23 21:07:24 -0500 665)
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 666) void d_mark_dontcache(struct inode *inode)
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 667) {
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 668) struct dentry *de;
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 669)
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 670) spin_lock(&inode->i_lock);
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 671) hlist_for_each_entry(de, &inode->i_dentry, d_u.d_alias) {
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 672) spin_lock(&de->d_lock);
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 673) de->d_flags |= DCACHE_DONTCACHE;
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 674) spin_unlock(&de->d_lock);
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 675) }
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 676) inode->i_state |= I_DONTCACHE;
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 677) spin_unlock(&inode->i_lock);
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 678) }
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 679) EXPORT_SYMBOL(d_mark_dontcache);
2c567af418e3f (Ira Weiny 2020-04-30 07:41:37 -0700 680)
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 681) /*
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 682) * Finish off a dentry we've decided to kill.
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 683) * dentry->d_lock must be held, returns with it unlocked.
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 684) * Returns dentry requiring refcount drop, or NULL if we're done.
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 685) */
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 686) static struct dentry *dentry_kill(struct dentry *dentry)
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 687) __releases(dentry->d_lock)
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 688) {
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 689) struct inode *inode = dentry->d_inode;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 690) struct dentry *parent = NULL;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 691)
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 692) if (inode && unlikely(!spin_trylock(&inode->i_lock)))
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 693) goto slow_positive;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 694)
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 695) if (!IS_ROOT(dentry)) {
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 696) parent = dentry->d_parent;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 697) if (unlikely(!spin_trylock(&parent->d_lock))) {
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 698) parent = __lock_parent(dentry);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 699) if (likely(inode || !dentry->d_inode))
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 700) goto got_locks;
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 701) /* negative that became positive */
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 702) if (parent)
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 703) spin_unlock(&parent->d_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 704) inode = dentry->d_inode;
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 705) goto slow_positive;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 706) }
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 707) }
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 708) __dentry_kill(dentry);
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 709) return parent;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 710)
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 711) slow_positive:
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 712) spin_unlock(&dentry->d_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 713) spin_lock(&inode->i_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 714) spin_lock(&dentry->d_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 715) parent = lock_parent(dentry);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 716) got_locks:
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 717) if (unlikely(dentry->d_lockref.count != 1)) {
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 718) dentry->d_lockref.count--;
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 719) } else if (likely(!retain_dentry(dentry))) {
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 720) __dentry_kill(dentry);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 721) return parent;
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 722) }
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 723) /* we are keeping it, after all */
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 724) if (inode)
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 725) spin_unlock(&inode->i_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 726) if (parent)
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 727) spin_unlock(&parent->d_lock);
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 728) spin_unlock(&dentry->d_lock);
f657a666fd1b1 (Al Viro 2018-02-23 21:25:42 -0500 729) return NULL;
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 730) }
c1d0c1a2b51e8 (John Ogness 2018-02-23 00:50:21 +0100 731)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 732) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 733) * Try to do a lockless dput(), and return whether that was successful.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 734) *
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 735) * If unsuccessful, we return false, having already taken the dentry lock.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 736) *
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 737) * The caller needs to hold the RCU read lock, so that the dentry is
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 738) * guaranteed to stay around even if the refcount goes down to zero!
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 739) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 740) static inline bool fast_dput(struct dentry *dentry)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 741) {
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 742) int ret;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 743) unsigned int d_flags;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 744)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 745) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 746) * If we have a d_op->d_delete() operation, we sould not
75a6f82a0d10e (Al Viro 2015-07-08 02:42:38 +0100 747) * let the dentry count go to zero, so use "put_or_lock".
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 748) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 749) if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 750) return lockref_put_or_lock(&dentry->d_lockref);
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 751)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 752) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 753) * .. otherwise, we can try to just decrement the
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 754) * lockref optimistically.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 755) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 756) ret = lockref_put_return(&dentry->d_lockref);
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 757)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 758) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 759) * If the lockref_put_return() failed due to the lock being held
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 760) * by somebody else, the fast path has failed. We will need to
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 761) * get the lock, and then check the count again.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 762) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 763) if (unlikely(ret < 0)) {
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 764) spin_lock(&dentry->d_lock);
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 765) if (dentry->d_lockref.count > 1) {
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 766) dentry->d_lockref.count--;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 767) spin_unlock(&dentry->d_lock);
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 768) return true;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 769) }
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 770) return false;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 771) }
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 772)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 773) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 774) * If we weren't the last ref, we're done.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 775) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 776) if (ret)
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 777) return true;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 778)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 779) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 780) * Careful, careful. The reference count went down
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 781) * to zero, but we don't hold the dentry lock, so
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 782) * somebody else could get it again, and do another
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 783) * dput(), and we need to not race with that.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 784) *
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 785) * However, there is a very special and common case
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 786) * where we don't care, because there is nothing to
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 787) * do: the dentry is still hashed, it does not have
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 788) * a 'delete' op, and it's referenced and already on
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 789) * the LRU list.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 790) *
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 791) * NOTE! Since we aren't locked, these values are
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 792) * not "stable". However, it is sufficient that at
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 793) * some point after we dropped the reference the
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 794) * dentry was hashed and the flags had the proper
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 795) * value. Other dentry users may have re-gotten
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 796) * a reference to the dentry and change that, but
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 797) * our work is done - we can leave the dentry
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 798) * around with a zero refcount.
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 799) *
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 800) * Nevertheless, there are two cases that we should kill
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 801) * the dentry anyway.
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 802) * 1. free disconnected dentries as soon as their refcount
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 803) * reached zero.
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 804) * 2. free dentries if they should not be cached.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 805) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 806) smp_rmb();
66702eb59064f (Mark Rutland 2017-10-23 14:07:14 -0700 807) d_flags = READ_ONCE(dentry->d_flags);
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 808) d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST |
77573fa310d95 (Hao Li 2020-12-08 10:10:50 +0800 809) DCACHE_DISCONNECTED | DCACHE_DONTCACHE;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 810)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 811) /* Nothing to do? Dropping the reference was all we needed? */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 812) if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 813) return true;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 814)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 815) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 816) * Not the fast normal case? Get the lock. We've already decremented
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 817) * the refcount, but we'll need to re-check the situation after
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 818) * getting the lock.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 819) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 820) spin_lock(&dentry->d_lock);
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 821)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 822) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 823) * Did somebody else grab a reference to it in the meantime, and
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 824) * we're no longer the last user after all? Alternatively, somebody
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 825) * else could have killed it and marked it dead. Either way, we
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 826) * don't need to do anything else.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 827) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 828) if (dentry->d_lockref.count) {
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 829) spin_unlock(&dentry->d_lock);
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 830) return true;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 831) }
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 832)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 833) /*
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 834) * Re-get the reference we optimistically dropped. We hold the
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 835) * lock, and we just tested that it was zero, so we can just
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 836) * set it to 1.
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 837) */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 838) dentry->d_lockref.count = 1;
7964410fcf135 (Gustavo A. R. Silva 2018-08-01 19:39:05 -0500 839) return false;
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 840) }
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 841)
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 842)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 843) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 844) * This is dput
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 845) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 846) * This is complicated by the fact that we do not want to put
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 847) * dentries that are no longer on any hash chain on the unused
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 848) * list: we'd much rather just get rid of them immediately.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 849) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 850) * However, that implies that we have to traverse the dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 851) * tree upwards to the parents which might _also_ now be
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 852) * scheduled for deletion (it may have been only waiting for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 853) * its last child to go away).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 854) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 855) * This tail recursion is done by hand as we don't want to depend
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 856) * on the compiler to always get this right (gcc generally doesn't).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 857) * Real recursion would eat up our stack space.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 858) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 859)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 860) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 861) * dput - release a dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 862) * @dentry: dentry to release
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 863) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 864) * Release a dentry. This will drop the usage count and if appropriate
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 865) * call the dentry unlink method as well as removing it from the queues and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 866) * releasing its resources. If the parent dentries were scheduled for release
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 867) * they too may now get deleted.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 868) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 869) void dput(struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 870) {
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 871) while (dentry) {
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 872) might_sleep();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 873)
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 874) rcu_read_lock();
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 875) if (likely(fast_dput(dentry))) {
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 876) rcu_read_unlock();
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 877) return;
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 878) }
47be61845c775 (Wei Fang 2016-07-06 11:32:20 +0800 879)
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 880) /* Slow case: now with the dentry lock held */
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 881) rcu_read_unlock();
360f54796ed65 (Linus Torvalds 2015-01-09 15:19:03 -0800 882)
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 883) if (likely(retain_dentry(dentry))) {
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 884) spin_unlock(&dentry->d_lock);
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 885) return;
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 886) }
265ac90230257 (Nicholas Piggin 2010-10-10 05:36:24 -0400 887)
1088a6408ce19 (Al Viro 2018-04-15 18:31:03 -0400 888) dentry = dentry_kill(dentry);
47be61845c775 (Wei Fang 2016-07-06 11:32:20 +0800 889) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 890) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 891) EXPORT_SYMBOL(dput);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 892)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 893) static void __dput_to_list(struct dentry *dentry, struct list_head *list)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 894) __must_hold(&dentry->d_lock)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 895) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 896) if (dentry->d_flags & DCACHE_SHRINK_LIST) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 897) /* let the owner of the list it's on deal with it */
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 898) --dentry->d_lockref.count;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 899) } else {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 900) if (dentry->d_flags & DCACHE_LRU_LIST)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 901) d_lru_del(dentry);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 902) if (!--dentry->d_lockref.count)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 903) d_shrink_add(dentry, list);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 904) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 905) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 906)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 907) void dput_to_list(struct dentry *dentry, struct list_head *list)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 908) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 909) rcu_read_lock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 910) if (likely(fast_dput(dentry))) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 911) rcu_read_unlock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 912) return;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 913) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 914) rcu_read_unlock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 915) if (!retain_dentry(dentry))
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 916) __dput_to_list(dentry, list);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 917) spin_unlock(&dentry->d_lock);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 918) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 919)
b5c84bf6f6fa3 (Nicholas Piggin 2011-01-07 17:49:38 +1100 920) /* This must be called with d_lock held */
dc0474be3e274 (Nicholas Piggin 2011-01-07 17:49:43 +1100 921) static inline void __dget_dlock(struct dentry *dentry)
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 922) {
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 923) dentry->d_lockref.count++;
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 924) }
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 925)
dc0474be3e274 (Nicholas Piggin 2011-01-07 17:49:43 +1100 926) static inline void __dget(struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 927) {
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 928) lockref_get(&dentry->d_lockref);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 929) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 930)
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 931) struct dentry *dget_parent(struct dentry *dentry)
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 932) {
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 933) int gotref;
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 934) struct dentry *ret;
e84009336711d (Al Viro 2019-10-31 01:43:31 -0400 935) unsigned seq;
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 936)
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 937) /*
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 938) * Do optimistic parent lookup without any
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 939) * locking.
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 940) */
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 941) rcu_read_lock();
e84009336711d (Al Viro 2019-10-31 01:43:31 -0400 942) seq = raw_seqcount_begin(&dentry->d_seq);
66702eb59064f (Mark Rutland 2017-10-23 14:07:14 -0700 943) ret = READ_ONCE(dentry->d_parent);
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 944) gotref = lockref_get_not_zero(&ret->d_lockref);
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 945) rcu_read_unlock();
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 946) if (likely(gotref)) {
e84009336711d (Al Viro 2019-10-31 01:43:31 -0400 947) if (!read_seqcount_retry(&dentry->d_seq, seq))
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 948) return ret;
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 949) dput(ret);
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 950) }
df3d0bbcdb2ca (Waiman Long 2013-09-02 11:29:22 -0700 951)
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 952) repeat:
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 953) /*
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 954) * Don't need rcu_dereference because we re-check it was correct under
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 955) * the lock.
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 956) */
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 957) rcu_read_lock();
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 958) ret = dentry->d_parent;
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 959) spin_lock(&ret->d_lock);
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 960) if (unlikely(ret != dentry->d_parent)) {
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 961) spin_unlock(&ret->d_lock);
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 962) rcu_read_unlock();
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 963) goto repeat;
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 964) }
a734eb458ab2b (Nicholas Piggin 2011-01-07 17:49:44 +1100 965) rcu_read_unlock();
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 966) BUG_ON(!ret->d_lockref.count);
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 967) ret->d_lockref.count++;
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 968) spin_unlock(&ret->d_lock);
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 969) return ret;
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 970) }
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 971) EXPORT_SYMBOL(dget_parent);
b7ab39f631f50 (Nicholas Piggin 2011-01-07 17:49:32 +1100 972)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 973) static struct dentry * __d_find_any_alias(struct inode *inode)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 974) {
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 975) struct dentry *alias;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 976)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 977) if (hlist_empty(&inode->i_dentry))
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 978) return NULL;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 979) alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 980) __dget(alias);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 981) return alias;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 982) }
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 983)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 984) /**
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 985) * d_find_any_alias - find any alias for a given inode
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 986) * @inode: inode to find an alias for
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 987) *
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 988) * If any aliases exist for the given inode, take and return a
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 989) * reference for one of them. If no aliases exist, return %NULL.
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 990) */
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 991) struct dentry *d_find_any_alias(struct inode *inode)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 992) {
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 993) struct dentry *de;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 994)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 995) spin_lock(&inode->i_lock);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 996) de = __d_find_any_alias(inode);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 997) spin_unlock(&inode->i_lock);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 998) return de;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 999) }
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1000) EXPORT_SYMBOL(d_find_any_alias);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1001)
52ed46f0fa882 (J. Bruce Fields 2014-01-16 11:15:51 -0500 1002) static struct dentry *__d_find_alias(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1003) {
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1004) struct dentry *alias;
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1005)
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1006) if (S_ISDIR(inode->i_mode))
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1007) return __d_find_any_alias(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1008)
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1009) hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 1010) spin_lock(&alias->d_lock);
61fec493c9df7 (Al Viro 2018-04-25 10:52:25 -0400 1011) if (!d_unhashed(alias)) {
8d80d7dabe966 (J. Bruce Fields 2014-01-16 17:17:31 -0500 1012) __dget_dlock(alias);
8d80d7dabe966 (J. Bruce Fields 2014-01-16 17:17:31 -0500 1013) spin_unlock(&alias->d_lock);
8d80d7dabe966 (J. Bruce Fields 2014-01-16 17:17:31 -0500 1014) return alias;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1015) }
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 1016) spin_unlock(&alias->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1017) }
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 1018) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1019) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1020)
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1021) /**
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1022) * d_find_alias - grab a hashed alias of inode
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1023) * @inode: inode in question
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1024) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1025) * If inode has a hashed alias, or is a directory and has any alias,
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1026) * acquire the reference to alias and return it. Otherwise return NULL.
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1027) * Notice that if inode is a directory there can be only one alias and
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1028) * it can be unhashed only if it has no children, or if it is the root
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1029) * of a filesystem, or if the directory was renamed and d_revalidate
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1030) * was the first vfs operation to notice.
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1031) *
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1032) * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1033) * any other hashed alias over that one.
961f3c898e86d (Mauro Carvalho Chehab 2021-01-14 09:04:39 +0100 1034) */
da5029563a0a0 (Nicholas Piggin 2011-01-07 17:49:33 +1100 1035) struct dentry *d_find_alias(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1036) {
214fda1f6e1b8 (David Howells 2006-03-25 03:06:36 -0800 1037) struct dentry *de = NULL;
214fda1f6e1b8 (David Howells 2006-03-25 03:06:36 -0800 1038)
b3d9b7a3c752d (Al Viro 2012-06-09 13:51:19 -0400 1039) if (!hlist_empty(&inode->i_dentry)) {
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 1040) spin_lock(&inode->i_lock);
52ed46f0fa882 (J. Bruce Fields 2014-01-16 11:15:51 -0500 1041) de = __d_find_alias(inode);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 1042) spin_unlock(&inode->i_lock);
214fda1f6e1b8 (David Howells 2006-03-25 03:06:36 -0800 1043) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1044) return de;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1045) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 1046) EXPORT_SYMBOL(d_find_alias);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1047)
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1048) /*
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1049) * Caller MUST be holding rcu_read_lock() and be guaranteed
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1050) * that inode won't get freed until rcu_read_unlock().
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1051) */
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1052) struct dentry *d_find_alias_rcu(struct inode *inode)
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1053) {
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1054) struct hlist_head *l = &inode->i_dentry;
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1055) struct dentry *de = NULL;
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1056)
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1057) spin_lock(&inode->i_lock);
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1058) // ->i_dentry and ->i_rcu are colocated, but the latter won't be
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1059) // used without having I_FREEING set, which means no aliases left
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1060) if (likely(!(inode->i_state & I_FREEING) && !hlist_empty(l))) {
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1061) if (S_ISDIR(inode->i_mode)) {
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1062) de = hlist_entry(l->first, struct dentry, d_u.d_alias);
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1063) } else {
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1064) hlist_for_each_entry(de, l, d_u.d_alias)
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1065) if (!d_unhashed(de))
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1066) break;
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1067) }
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1068) }
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1069) spin_unlock(&inode->i_lock);
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1070) return de;
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1071) }
bca585d24a171 (Al Viro 2021-01-05 14:13:52 -0500 1072)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1073) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1074) * Try to kill dentries associated with this inode.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1075) * WARNING: you must own a reference to inode.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1076) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1077) void d_prune_aliases(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1078) {
0cdca3f9806a3 (Domen Puncer 2005-09-10 00:27:07 -0700 1079) struct dentry *dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1080) restart:
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 1081) spin_lock(&inode->i_lock);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1082) hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1083) spin_lock(&dentry->d_lock);
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 1084) if (!dentry->d_lockref.count) {
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1085) struct dentry *parent = lock_parent(dentry);
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1086) if (likely(!dentry->d_lockref.count)) {
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1087) __dentry_kill(dentry);
4a7795d35e252 (Yan, Zheng 2014-11-19 15:50:34 +0800 1088) dput(parent);
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1089) goto restart;
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1090) }
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1091) if (parent)
29355c3904e17 (Al Viro 2014-05-30 11:25:30 -0400 1092) spin_unlock(&parent->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1093) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1094) spin_unlock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1095) }
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 1096) spin_unlock(&inode->i_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1097) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 1098) EXPORT_SYMBOL(d_prune_aliases);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1099)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1100) /*
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1101) * Lock a dentry from shrink list.
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1102) * Called under rcu_read_lock() and dentry->d_lock; the former
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1103) * guarantees that nothing we access will be freed under us.
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1104) * Note that dentry is *not* protected from concurrent dentry_kill(),
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1105) * d_delete(), etc.
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1106) *
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1107) * Return false if dentry has been disrupted or grabbed, leaving
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1108) * the caller to kick it off-list. Otherwise, return true and have
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1109) * that dentry's inode and parent both locked.
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1110) */
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1111) static bool shrink_lock_dentry(struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1112) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1113) struct inode *inode;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1114) struct dentry *parent;
da3bbdd4632c0 (Kentaro Makita 2008-07-23 21:27:13 -0700 1115)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1116) if (dentry->d_lockref.count)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1117) return false;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1118)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1119) inode = dentry->d_inode;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1120) if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1121) spin_unlock(&dentry->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1122) spin_lock(&inode->i_lock);
ec33679d78f9d (Nicholas Piggin 2011-01-07 17:49:47 +1100 1123) spin_lock(&dentry->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1124) if (unlikely(dentry->d_lockref.count))
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1125) goto out;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1126) /* changed inode means that somebody had grabbed it */
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1127) if (unlikely(inode != dentry->d_inode))
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1128) goto out;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1129) }
046b961b45f93 (Al Viro 2014-05-29 08:54:52 -0400 1130)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1131) parent = dentry->d_parent;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1132) if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1133) return true;
dd1f6b2e43a53 (Dave Chinner 2013-08-28 10:17:55 +1000 1134)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1135) spin_unlock(&dentry->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1136) spin_lock(&parent->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1137) if (unlikely(parent != dentry->d_parent)) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1138) spin_unlock(&parent->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1139) spin_lock(&dentry->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1140) goto out;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1141) }
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1142) spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1143) if (likely(!dentry->d_lockref.count))
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1144) return true;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1145) spin_unlock(&parent->d_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1146) out:
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1147) if (inode)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1148) spin_unlock(&inode->i_lock);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1149) return false;
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1150) }
77812a1ef139d (Nicholas Piggin 2011-01-07 17:49:48 +1100 1151)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1152) void shrink_dentry_list(struct list_head *list)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1153) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1154) while (!list_empty(list)) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1155) struct dentry *dentry, *parent;
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1156)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1157) dentry = list_entry(list->prev, struct dentry, d_lru);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1158) spin_lock(&dentry->d_lock);
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1159) rcu_read_lock();
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1160) if (!shrink_lock_dentry(dentry)) {
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1161) bool can_free = false;
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1162) rcu_read_unlock();
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1163) d_shrink_del(dentry);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1164) if (dentry->d_lockref.count < 0)
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1165) can_free = dentry->d_flags & DCACHE_MAY_FREE;
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1166) spin_unlock(&dentry->d_lock);
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1167) if (can_free)
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1168) dentry_free(dentry);
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1169) continue;
64fd72e0a44bd (Al Viro 2014-05-28 09:48:44 -0400 1170) }
8f04da2adbdff (John Ogness 2018-02-23 00:50:24 +0100 1171) rcu_read_unlock();
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1172) d_shrink_del(dentry);
3b3f09f48ba78 (Al Viro 2018-02-23 21:54:18 -0500 1173) parent = dentry->d_parent;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1174) if (parent != dentry)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1175) __dput_to_list(parent, list);
ff2fde9929feb (Al Viro 2014-05-28 13:59:13 -0400 1176) __dentry_kill(dentry);
da3bbdd4632c0 (Kentaro Makita 2008-07-23 21:27:13 -0700 1177) }
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1178) }
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1179)
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1180) static enum lru_status dentry_lru_isolate(struct list_head *item,
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1181) struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1182) {
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1183) struct list_head *freeable = arg;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1184) struct dentry *dentry = container_of(item, struct dentry, d_lru);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1185)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1186)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1187) /*
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1188) * we are inverting the lru lock/dentry->d_lock here,
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1189) * so use a trylock. If we fail to get the lock, just skip
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1190) * it
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1191) */
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1192) if (!spin_trylock(&dentry->d_lock))
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1193) return LRU_SKIP;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1194)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1195) /*
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1196) * Referenced dentries are still in use. If they have active
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1197) * counts, just remove them from the LRU. Otherwise give them
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1198) * another pass through the LRU.
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1199) */
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1200) if (dentry->d_lockref.count) {
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1201) d_lru_isolate(lru, dentry);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1202) spin_unlock(&dentry->d_lock);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1203) return LRU_REMOVED;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1204) }
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1205)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1206) if (dentry->d_flags & DCACHE_REFERENCED) {
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1207) dentry->d_flags &= ~DCACHE_REFERENCED;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1208) spin_unlock(&dentry->d_lock);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1209)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1210) /*
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1211) * The list move itself will be made by the common LRU code. At
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1212) * this point, we've dropped the dentry->d_lock but keep the
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1213) * lru lock. This is safe to do, since every list movement is
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1214) * protected by the lru lock even if both locks are held.
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1215) *
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1216) * This is guaranteed by the fact that all LRU management
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1217) * functions are intermediated by the LRU API calls like
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1218) * list_lru_add and list_lru_del. List movement in this file
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1219) * only ever occur through this functions or through callbacks
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1220) * like this one, that are called from the LRU API.
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1221) *
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1222) * The only exceptions to this are functions like
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1223) * shrink_dentry_list, and code that first checks for the
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1224) * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1225) * operating only with stack provided lists after they are
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1226) * properly isolated from the main list. It is thus, always a
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1227) * local access.
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1228) */
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1229) return LRU_ROTATE;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1230) }
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1231)
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1232) d_lru_shrink_move(lru, dentry, freeable);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1233) spin_unlock(&dentry->d_lock);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1234)
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1235) return LRU_REMOVED;
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1236) }
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1237)
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1238) /**
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1239) * prune_dcache_sb - shrink the dcache
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1240) * @sb: superblock
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1241) * @sc: shrink control, passed to list_lru_shrink_walk()
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1242) *
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1243) * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1244) * is done when we need more memory and called from the superblock shrinker
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1245) * function.
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1246) *
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1247) * This function may fail to free any resources if all the dentries are in
b48f03b319ba7 (Dave Chinner 2011-08-23 18:56:24 +1000 1248) * use.
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1249) */
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1250) long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1251) {
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1252) LIST_HEAD(dispose);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1253) long freed;
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1254)
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1255) freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
503c358cf1925 (Vladimir Davydov 2015-02-12 14:58:47 -0800 1256) dentry_lru_isolate, &dispose);
f604156751db7 (Dave Chinner 2013-08-28 10:18:00 +1000 1257) shrink_dentry_list(&dispose);
0a234c6dcb79a (Dave Chinner 2013-08-28 10:17:57 +1000 1258) return freed;
da3bbdd4632c0 (Kentaro Makita 2008-07-23 21:27:13 -0700 1259) }
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 1260)
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1261) static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1262) struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
dd1f6b2e43a53 (Dave Chinner 2013-08-28 10:17:55 +1000 1263) {
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1264) struct list_head *freeable = arg;
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1265) struct dentry *dentry = container_of(item, struct dentry, d_lru);
dd1f6b2e43a53 (Dave Chinner 2013-08-28 10:17:55 +1000 1266)
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1267) /*
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1268) * we are inverting the lru lock/dentry->d_lock here,
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1269) * so use a trylock. If we fail to get the lock, just skip
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1270) * it
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1271) */
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1272) if (!spin_trylock(&dentry->d_lock))
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1273) return LRU_SKIP;
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1274)
3f97b163207c6 (Vladimir Davydov 2015-02-12 14:59:35 -0800 1275) d_lru_shrink_move(lru, dentry, freeable);
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1276) spin_unlock(&dentry->d_lock);
ec33679d78f9d (Nicholas Piggin 2011-01-07 17:49:47 +1100 1277)
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1278) return LRU_REMOVED;
da3bbdd4632c0 (Kentaro Makita 2008-07-23 21:27:13 -0700 1279) }
da3bbdd4632c0 (Kentaro Makita 2008-07-23 21:27:13 -0700 1280)
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1281)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1282) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1283) * shrink_dcache_sb - shrink dcache for a superblock
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1284) * @sb: superblock
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1285) *
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1286) * Shrink the dcache for the specified super block. This is used to free
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1287) * the dcache before unmounting a file system.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1288) */
3049cfe24ef38 (Christoph Hellwig 2010-10-10 05:36:25 -0400 1289) void shrink_dcache_sb(struct super_block *sb)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1290) {
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1291) do {
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1292) LIST_HEAD(dispose);
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1293)
1dbd449c9943e (Waiman Long 2019-01-30 13:52:36 -0500 1294) list_lru_walk(&sb->s_dentry_lru,
b17c070fb624c (Sahitya Tummala 2017-07-10 15:50:00 -0700 1295) dentry_lru_isolate_shrink, &dispose, 1024);
4e717f5c10839 (Glauber Costa 2013-08-28 10:18:03 +1000 1296) shrink_dentry_list(&dispose);
b17c070fb624c (Sahitya Tummala 2017-07-10 15:50:00 -0700 1297) } while (list_lru_count(&sb->s_dentry_lru) > 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1298) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 1299) EXPORT_SYMBOL(shrink_dcache_sb);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1300)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1301) /**
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1302) * enum d_walk_ret - action to talke during tree walk
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1303) * @D_WALK_CONTINUE: contrinue walk
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1304) * @D_WALK_QUIT: quit walk
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1305) * @D_WALK_NORETRY: quit when retry is needed
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1306) * @D_WALK_SKIP: skip this dentry and its children
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1307) */
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1308) enum d_walk_ret {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1309) D_WALK_CONTINUE,
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1310) D_WALK_QUIT,
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1311) D_WALK_NORETRY,
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1312) D_WALK_SKIP,
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1313) };
c826cb7dfce80 (Linus Torvalds 2011-03-15 15:29:21 -0700 1314)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1315) /**
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1316) * d_walk - walk the dentry tree
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1317) * @parent: start of walk
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1318) * @data: data passed to @enter() and @finish()
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1319) * @enter: callback when first entering the dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1320) *
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1321) * The @enter() callbacks are called with d_lock held.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1322) */
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1323) static void d_walk(struct dentry *parent, void *data,
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1324) enum d_walk_ret (*enter)(void *, struct dentry *))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1325) {
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 1326) struct dentry *this_parent;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1327) struct list_head *next;
48f5ec21d9c67 (Al Viro 2013-09-09 15:22:25 -0400 1328) unsigned seq = 0;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1329) enum d_walk_ret ret;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1330) bool retry = true;
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 1331)
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 1332) again:
48f5ec21d9c67 (Al Viro 2013-09-09 15:22:25 -0400 1333) read_seqbegin_or_lock(&rename_lock, &seq);
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 1334) this_parent = parent;
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1335) spin_lock(&this_parent->d_lock);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1336)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1337) ret = enter(data, this_parent);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1338) switch (ret) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1339) case D_WALK_CONTINUE:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1340) break;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1341) case D_WALK_QUIT:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1342) case D_WALK_SKIP:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1343) goto out_unlock;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1344) case D_WALK_NORETRY:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1345) retry = false;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1346) break;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1347) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1348) repeat:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1349) next = this_parent->d_subdirs.next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1350) resume:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1351) while (next != &this_parent->d_subdirs) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1352) struct list_head *tmp = next;
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1353) struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1354) next = tmp->next;
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1355)
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1356) if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1357) continue;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1358)
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1359) spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1360)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1361) ret = enter(data, dentry);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1362) switch (ret) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1363) case D_WALK_CONTINUE:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1364) break;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1365) case D_WALK_QUIT:
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1366) spin_unlock(&dentry->d_lock);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1367) goto out_unlock;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1368) case D_WALK_NORETRY:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1369) retry = false;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1370) break;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1371) case D_WALK_SKIP:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1372) spin_unlock(&dentry->d_lock);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1373) continue;
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1374) }
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1375)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1376) if (!list_empty(&dentry->d_subdirs)) {
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1377) spin_unlock(&this_parent->d_lock);
5facae4f3549b (Qian Cai 2019-09-19 12:09:40 -0400 1378) spin_release(&dentry->d_lock.dep_map, _RET_IP_);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1379) this_parent = dentry;
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1380) spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1381) goto repeat;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1382) }
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1383) spin_unlock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1384) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1385) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1386) * All done at this level ... ascend and resume the search.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1387) */
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1388) rcu_read_lock();
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1389) ascend:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1390) if (this_parent != parent) {
c826cb7dfce80 (Linus Torvalds 2011-03-15 15:29:21 -0700 1391) struct dentry *child = this_parent;
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1392) this_parent = child->d_parent;
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1393)
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1394) spin_unlock(&child->d_lock);
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1395) spin_lock(&this_parent->d_lock);
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1396)
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1397) /* might go back up the wrong parent if we have had a rename. */
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1398) if (need_seqretry(&rename_lock, seq))
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 1399) goto rename_retry;
2159184ea01e4 (Al Viro 2015-05-28 23:09:19 -0400 1400) /* go into the first sibling still alive */
2159184ea01e4 (Al Viro 2015-05-28 23:09:19 -0400 1401) do {
2159184ea01e4 (Al Viro 2015-05-28 23:09:19 -0400 1402) next = child->d_child.next;
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1403) if (next == &this_parent->d_subdirs)
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1404) goto ascend;
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1405) child = list_entry(next, struct dentry, d_child);
2159184ea01e4 (Al Viro 2015-05-28 23:09:19 -0400 1406) } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
31dec1327e377 (Al Viro 2013-10-25 17:04:27 -0400 1407) rcu_read_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1408) goto resume;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1409) }
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1410) if (need_seqretry(&rename_lock, seq))
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 1411) goto rename_retry;
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1412) rcu_read_unlock();
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1413)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1414) out_unlock:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1415) spin_unlock(&this_parent->d_lock);
48f5ec21d9c67 (Al Viro 2013-09-09 15:22:25 -0400 1416) done_seqretry(&rename_lock, seq);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1417) return;
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 1418)
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 1419) rename_retry:
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1420) spin_unlock(&this_parent->d_lock);
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1421) rcu_read_unlock();
ca5358ef75fc6 (Al Viro 2014-10-26 19:31:10 -0400 1422) BUG_ON(seq & 1);
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1423) if (!retry)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1424) return;
48f5ec21d9c67 (Al Viro 2013-09-09 15:22:25 -0400 1425) seq = 1;
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 1426) goto again;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1427) }
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1428)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1429) struct check_mount {
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1430) struct vfsmount *mnt;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1431) unsigned int mounted;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1432) };
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1433)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1434) static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1435) {
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1436) struct check_mount *info = data;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1437) struct path path = { .mnt = info->mnt, .dentry = dentry };
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1438)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1439) if (likely(!d_mountpoint(dentry)))
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1440) return D_WALK_CONTINUE;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1441) if (__path_is_mountpoint(&path)) {
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1442) info->mounted = 1;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1443) return D_WALK_QUIT;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1444) }
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1445) return D_WALK_CONTINUE;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1446) }
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1447)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1448) /**
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1449) * path_has_submounts - check for mounts over a dentry in the
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1450) * current namespace.
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1451) * @parent: path to check.
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1452) *
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1453) * Return true if the parent or its subdirectories contain
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1454) * a mount point in the current namespace.
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1455) */
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1456) int path_has_submounts(const struct path *parent)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1457) {
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1458) struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1459)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1460) read_seqlock_excl(&mount_lock);
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1461) d_walk(parent->dentry, &data, path_check_mount);
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1462) read_sequnlock_excl(&mount_lock);
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1463)
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1464) return data.mounted;
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1465) }
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1466) EXPORT_SYMBOL(path_has_submounts);
01619491a5f07 (Ian Kent 2016-11-24 08:03:41 +1100 1467)
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1468) /*
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1469) * Called by mount code to set a mountpoint and check if the mountpoint is
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1470) * reachable (e.g. NFS can unhash a directory dentry and then the complete
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1471) * subtree can become unreachable).
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1472) *
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1473) * Only one of d_invalidate() and d_set_mounted() must succeed. For
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1474) * this reason take rename_lock and d_lock on dentry and ancestors.
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1475) */
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1476) int d_set_mounted(struct dentry *dentry)
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1477) {
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1478) struct dentry *p;
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1479) int ret = -ENOENT;
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1480) write_seqlock(&rename_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1481) for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1482) /* Need exclusion wrt. d_invalidate() */
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1483) spin_lock(&p->d_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1484) if (unlikely(d_unhashed(p))) {
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1485) spin_unlock(&p->d_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1486) goto out;
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1487) }
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1488) spin_unlock(&p->d_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1489) }
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1490) spin_lock(&dentry->d_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1491) if (!d_unlinked(dentry)) {
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1492) ret = -EBUSY;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1493) if (!d_mountpoint(dentry)) {
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1494) dentry->d_flags |= DCACHE_MOUNTED;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1495) ret = 0;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1496) }
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1497) }
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1498) spin_unlock(&dentry->d_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1499) out:
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1500) write_sequnlock(&rename_lock);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1501) return ret;
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1502) }
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 1503)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1504) /*
fd51790949edb (J. Bruce Fields 2012-09-18 16:35:51 -0400 1505) * Search the dentry child list of the specified parent,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1506) * and move any unused dentries to the end of the unused
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1507) * list for prune_dcache(). We descend to the next level
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1508) * whenever the d_subdirs list is non-empty and continue
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1509) * searching.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1510) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1511) * It returns zero iff there are no unused children,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1512) * otherwise it returns the number of children moved to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1513) * the end of the unused list. This may not be the total
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1514) * number of unused children, because select_parent can
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1515) * drop the lock and return early due to latency
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1516) * constraints.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1517) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1518)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1519) struct select_data {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1520) struct dentry *start;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1521) union {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1522) long found;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1523) struct dentry *victim;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1524) };
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1525) struct list_head dispose;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1526) };
2304450783dfd (Nicholas Piggin 2011-01-07 17:49:31 +1100 1527)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1528) static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1529) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1530) struct select_data *data = _data;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1531) enum d_walk_ret ret = D_WALK_CONTINUE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1532)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1533) if (data->start == dentry)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1534) goto out;
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 1535)
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1536) if (dentry->d_flags & DCACHE_SHRINK_LIST) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1537) data->found++;
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1538) } else {
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1539) if (dentry->d_flags & DCACHE_LRU_LIST)
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1540) d_lru_del(dentry);
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1541) if (!dentry->d_lockref.count) {
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1542) d_shrink_add(dentry, &data->dispose);
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1543) data->found++;
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1544) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1545) }
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1546) /*
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1547) * We can return to the caller if we have found some (this
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1548) * ensures forward progress). We'll be coming back to find
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1549) * the rest.
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1550) */
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1551) if (!list_empty(&data->dispose))
fe91522a7ba82 (Al Viro 2014-05-03 00:02:25 -0400 1552) ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1553) out:
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1554) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1555) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1556)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1557) static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1558) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1559) struct select_data *data = _data;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1560) enum d_walk_ret ret = D_WALK_CONTINUE;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1561)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1562) if (data->start == dentry)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1563) goto out;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1564)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1565) if (dentry->d_flags & DCACHE_SHRINK_LIST) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1566) if (!dentry->d_lockref.count) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1567) rcu_read_lock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1568) data->victim = dentry;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1569) return D_WALK_QUIT;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1570) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1571) } else {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1572) if (dentry->d_flags & DCACHE_LRU_LIST)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1573) d_lru_del(dentry);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1574) if (!dentry->d_lockref.count)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1575) d_shrink_add(dentry, &data->dispose);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1576) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1577) /*
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1578) * We can return to the caller if we have found some (this
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1579) * ensures forward progress). We'll be coming back to find
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1580) * the rest.
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1581) */
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1582) if (!list_empty(&data->dispose))
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1583) ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1584) out:
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1585) return ret;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1586) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1587)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1588) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1589) * shrink_dcache_parent - prune dcache
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1590) * @parent: parent of entries to prune
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1591) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1592) * Prune the dcache to remove unused children of the parent dentry.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1593) */
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1594) void shrink_dcache_parent(struct dentry *parent)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1595) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1596) for (;;) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1597) struct select_data data = {.start = parent};
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1598)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1599) INIT_LIST_HEAD(&data.dispose);
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1600) d_walk(parent, &data, select_collect);
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1601)
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1602) if (!list_empty(&data.dispose)) {
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1603) shrink_dentry_list(&data.dispose);
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1604) continue;
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1605) }
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1606)
4fb48871409e2 (Al Viro 2018-04-19 23:58:48 -0400 1607) cond_resched();
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1608) if (!data.found)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 1609) break;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1610) data.victim = NULL;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1611) d_walk(parent, &data, select_collect2);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1612) if (data.victim) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1613) struct dentry *parent;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1614) spin_lock(&data.victim->d_lock);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1615) if (!shrink_lock_dentry(data.victim)) {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1616) spin_unlock(&data.victim->d_lock);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1617) rcu_read_unlock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1618) } else {
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1619) rcu_read_unlock();
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1620) parent = data.victim->d_parent;
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1621) if (parent != data.victim)
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1622) __dput_to_list(parent, &data.dispose);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1623) __dentry_kill(data.victim);
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1624) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1625) }
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1626) if (!list_empty(&data.dispose))
9bdebc2bd1c4a (Al Viro 2019-06-29 18:31:24 -0400 1627) shrink_dentry_list(&data.dispose);
421348f1ca0bf (Greg Thelen 2013-04-30 15:26:48 -0700 1628) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1629) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 1630) EXPORT_SYMBOL(shrink_dcache_parent);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1631)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1632) static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1633) {
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1634) /* it has busy descendents; complain about those instead */
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1635) if (!list_empty(&dentry->d_subdirs))
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1636) return D_WALK_CONTINUE;
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1637)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1638) /* root with refcount 1 is fine */
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1639) if (dentry == _data && dentry->d_lockref.count == 1)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1640) return D_WALK_CONTINUE;
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1641)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1642) printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1643) " still in use (%d) [unmount of %s %s]\n",
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1644) dentry,
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1645) dentry->d_inode ?
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1646) dentry->d_inode->i_ino : 0UL,
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1647) dentry,
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1648) dentry->d_lockref.count,
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1649) dentry->d_sb->s_type->name,
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1650) dentry->d_sb->s_id);
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1651) WARN_ON(1);
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1652) return D_WALK_CONTINUE;
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1653) }
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1654)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1655) static void do_one_tree(struct dentry *dentry)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1656) {
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1657) shrink_dcache_parent(dentry);
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1658) d_walk(dentry, dentry, umount_check);
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1659) d_drop(dentry);
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1660) dput(dentry);
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1661) }
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1662)
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1663) /*
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1664) * destroy the dentries attached to a superblock on unmounting
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1665) */
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1666) void shrink_dcache_for_umount(struct super_block *sb)
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1667) {
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1668) struct dentry *dentry;
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1669)
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1670) WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1671)
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1672) dentry = sb->s_root;
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1673) sb->s_root = NULL;
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1674) do_one_tree(dentry);
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1675)
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 1676) while (!hlist_bl_empty(&sb->s_roots)) {
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 1677) dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
9c8c10e262e0f (Al Viro 2014-05-02 20:36:10 -0400 1678) do_one_tree(dentry);
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1679) }
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1680) }
42c326082d8a2 (Al Viro 2013-11-08 12:31:16 -0500 1681)
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1682) static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1683) {
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1684) struct dentry **victim = _data;
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1685) if (d_mountpoint(dentry)) {
8ed936b5671bf (Eric W. Biederman 2013-10-01 18:33:48 -0700 1686) __dget_dlock(dentry);
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1687) *victim = dentry;
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1688) return D_WALK_QUIT;
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1689) }
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1690) return D_WALK_CONTINUE;
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1691) }
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1692)
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1693) /**
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1694) * d_invalidate - detach submounts, prune dcache, and drop
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1695) * @dentry: dentry to invalidate (aka detach, prune and drop)
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1696) */
5542aa2fa7f6c (Eric W. Biederman 2014-02-13 09:46:25 -0800 1697) void d_invalidate(struct dentry *dentry)
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1698) {
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1699) bool had_submounts = false;
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1700) spin_lock(&dentry->d_lock);
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1701) if (d_unhashed(dentry)) {
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1702) spin_unlock(&dentry->d_lock);
5542aa2fa7f6c (Eric W. Biederman 2014-02-13 09:46:25 -0800 1703) return;
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1704) }
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1705) __d_drop(dentry);
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1706) spin_unlock(&dentry->d_lock);
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1707)
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1708) /* Negative dentries can be dropped without further checks */
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1709) if (!dentry->d_inode)
5542aa2fa7f6c (Eric W. Biederman 2014-02-13 09:46:25 -0800 1710) return;
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1711)
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1712) shrink_dcache_parent(dentry);
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1713) for (;;) {
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1714) struct dentry *victim = NULL;
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 1715) d_walk(dentry, &victim, find_submount);
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1716) if (!victim) {
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1717) if (had_submounts)
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1718) shrink_dcache_parent(dentry);
81be24d263dbe (Al Viro 2017-06-03 07:20:09 +0100 1719) return;
8ed936b5671bf (Eric W. Biederman 2013-10-01 18:33:48 -0700 1720) }
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1721) had_submounts = true;
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1722) detach_mounts(victim);
ff17fa561a04b (Al Viro 2018-04-15 18:21:47 -0400 1723) dput(victim);
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1724) }
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1725) }
1ffe46d11cc88 (Eric W. Biederman 2014-02-13 09:39:37 -0800 1726) EXPORT_SYMBOL(d_invalidate);
848ac114e847a (Miklos Szeredi 2013-09-05 11:44:36 +0200 1727)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1728) /**
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1729) * __d_alloc - allocate a dcache entry
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1730) * @sb: filesystem it will belong to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1731) * @name: qstr of the name
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1732) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1733) * Allocates a dentry. It returns %NULL if there is insufficient memory
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1734) * available. On a success the dentry is returned. The name passed in is
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1735) * copied and the copy passed in may be reused after this call.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1736) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1737)
5c8b0dfc6f4a5 (Al Viro 2019-10-25 14:08:24 -0400 1738) static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1739) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1740) struct dentry *dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1741) char *dname;
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1742) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1743)
e12ba74d8ff3e (Mel Gorman 2007-10-16 01:25:52 -0700 1744) dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1745) if (!dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1746) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1747)
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1748) /*
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1749) * We guarantee that the inline name is always NUL-terminated.
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1750) * This way the memcpy() done by the name switching in rename
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1751) * will still always have a NUL at the end, even if we might
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1752) * be overwriting an internal NUL character
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1753) */
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1754) dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
798434bda36e3 (Al Viro 2016-03-24 20:38:43 -0400 1755) if (unlikely(!name)) {
cdf01226b26e9 (David Howells 2017-07-04 17:25:22 +0100 1756) name = &slash_name;
798434bda36e3 (Al Viro 2016-03-24 20:38:43 -0400 1757) dname = dentry->d_iname;
798434bda36e3 (Al Viro 2016-03-24 20:38:43 -0400 1758) } else if (name->len > DNAME_INLINE_LEN-1) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 1759) size_t size = offsetof(struct external_name, name[1]);
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1760) struct external_name *p = kmalloc(size + name->len,
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1761) GFP_KERNEL_ACCOUNT |
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1762) __GFP_RECLAIMABLE);
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1763) if (!p) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1764) kmem_cache_free(dentry_cache, dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1765) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1766) }
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1767) atomic_set(&p->u.count, 1);
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 1768) dname = p->name;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1769) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1770) dname = dentry->d_iname;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1771) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1772)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1773) dentry->d_name.len = name->len;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1774) dentry->d_name.hash = name->hash;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1775) memcpy(dname, name->name, name->len);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1776) dname[name->len] = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1777)
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1778) /* Make sure we always see the terminating NUL character */
7088efa9137a1 (Paul E. McKenney 2017-10-09 10:04:27 -0700 1779) smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
6326c71fd2fb3 (Linus Torvalds 2012-05-21 16:14:04 -0700 1780)
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 1781) dentry->d_lockref.count = 1;
dea3667bc3c2a (Linus Torvalds 2011-04-24 07:58:46 -0700 1782) dentry->d_flags = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1783) spin_lock_init(&dentry->d_lock);
26475371976c6 (Ahmed S. Darwish 2020-07-20 17:55:24 +0200 1784) seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1785) dentry->d_inode = NULL;
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1786) dentry->d_parent = dentry;
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1787) dentry->d_sb = sb;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1788) dentry->d_op = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1789) dentry->d_fsdata = NULL;
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 1790) INIT_HLIST_BL_NODE(&dentry->d_hash);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1791) INIT_LIST_HEAD(&dentry->d_lru);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1792) INIT_LIST_HEAD(&dentry->d_subdirs);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1793) INIT_HLIST_NODE(&dentry->d_u.d_alias);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1794) INIT_LIST_HEAD(&dentry->d_child);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1795) d_set_d_op(dentry, dentry->d_sb->s_d_op);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1796)
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1797) if (dentry->d_op && dentry->d_op->d_init) {
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1798) err = dentry->d_op->d_init(dentry);
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1799) if (err) {
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1800) if (dname_external(dentry))
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1801) kfree(external_name(dentry));
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1802) kmem_cache_free(dentry_cache, dentry);
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1803) return NULL;
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1804) }
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1805) }
285b102d3b745 (Miklos Szeredi 2016-06-28 11:47:32 +0200 1806)
3e880fb5e4bb6 (Nicholas Piggin 2011-01-07 17:49:19 +1100 1807) this_cpu_inc(nr_dentry);
312d3ca856d36 (Christoph Hellwig 2010-10-10 05:36:23 -0400 1808)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1809) return dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1810) }
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1811)
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1812) /**
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1813) * d_alloc - allocate a dcache entry
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1814) * @parent: parent of entry to allocate
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1815) * @name: qstr of the name
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1816) *
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1817) * Allocates a dentry. It returns %NULL if there is insufficient memory
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1818) * available. On a success the dentry is returned. The name passed in is
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1819) * copied and the copy passed in may be reused after this call.
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1820) */
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1821) struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1822) {
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1823) struct dentry *dentry = __d_alloc(parent->d_sb, name);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1824) if (!dentry)
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1825) return NULL;
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1826) spin_lock(&parent->d_lock);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1827) /*
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1828) * don't need child lock because it is not subject
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1829) * to concurrency here
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1830) */
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1831) __dget_dlock(parent);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1832) dentry->d_parent = parent;
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 1833) list_add(&dentry->d_child, &parent->d_subdirs);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1834) spin_unlock(&parent->d_lock);
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1835)
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1836) return dentry;
a4464dbc0ca6a (Al Viro 2011-07-07 15:03:58 -0400 1837) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 1838) EXPORT_SYMBOL(d_alloc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1839)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1840) struct dentry *d_alloc_anon(struct super_block *sb)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1841) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1842) return __d_alloc(sb, NULL);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1843) }
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1844) EXPORT_SYMBOL(d_alloc_anon);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1845)
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1846) struct dentry *d_alloc_cursor(struct dentry * parent)
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1847) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 1848) struct dentry *dentry = d_alloc_anon(parent->d_sb);
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1849) if (dentry) {
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1850) dentry->d_flags |= DCACHE_DENTRY_CURSOR;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1851) dentry->d_parent = dget(parent);
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1852) }
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1853) return dentry;
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1854) }
ba65dc5ef16f8 (Al Viro 2016-06-10 11:32:47 -0400 1855)
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1856) /**
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1857) * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1858) * @sb: the superblock
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1859) * @name: qstr of the name
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1860) *
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1861) * For a filesystem that just pins its dentries in memory and never
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1862) * performs lookups at all, return an unhashed IS_ROOT dentry.
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1863) * This is used for pipes, sockets et.al. - the stuff that should
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1864) * never be anyone's children or parents. Unlike all other
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1865) * dentries, these will not have RCU delay between dropping the
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1866) * last reference and freeing them.
ab1152dd5650d (Al Viro 2019-03-15 22:58:11 -0400 1867) *
ab1152dd5650d (Al Viro 2019-03-15 22:58:11 -0400 1868) * The only user is alloc_file_pseudo() and that's what should
ab1152dd5650d (Al Viro 2019-03-15 22:58:11 -0400 1869) * be considered a public interface. Don't use directly.
e1a24bb0aa6ab (J. Bruce Fields 2012-06-29 16:20:47 -0400 1870) */
4b936885ab04d (Nicholas Piggin 2011-01-07 17:50:07 +1100 1871) struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
4b936885ab04d (Nicholas Piggin 2011-01-07 17:50:07 +1100 1872) {
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1873) struct dentry *dentry = __d_alloc(sb, name);
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1874) if (likely(dentry))
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1875) dentry->d_flags |= DCACHE_NORCU;
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 1876) return dentry;
4b936885ab04d (Nicholas Piggin 2011-01-07 17:50:07 +1100 1877) }
4b936885ab04d (Nicholas Piggin 2011-01-07 17:50:07 +1100 1878)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1879) struct dentry *d_alloc_name(struct dentry *parent, const char *name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1880) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1881) struct qstr q;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1882)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1883) q.name = name;
8387ff2577eb9 (Linus Torvalds 2016-06-10 07:51:30 -0700 1884) q.hash_len = hashlen_string(parent, name);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1885) return d_alloc(parent, &q);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1886) }
ef26ca97e8305 (H Hartley Sweeten 2009-09-29 20:09:42 -0400 1887) EXPORT_SYMBOL(d_alloc_name);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1888)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1889) void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1890) {
6f7f7caab2590 (Linus Torvalds 2011-01-14 13:26:18 -0800 1891) WARN_ON_ONCE(dentry->d_op);
6f7f7caab2590 (Linus Torvalds 2011-01-14 13:26:18 -0800 1892) WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1893) DCACHE_OP_COMPARE |
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1894) DCACHE_OP_REVALIDATE |
ecf3d1f1aa74d (Jeff Layton 2013-02-20 11:19:05 -0500 1895) DCACHE_OP_WEAK_REVALIDATE |
4bacc9c9234c7 (David Howells 2015-06-18 14:32:31 +0100 1896) DCACHE_OP_DELETE |
d101a125954ea (Miklos Szeredi 2016-03-26 16:14:37 -0400 1897) DCACHE_OP_REAL));
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1898) dentry->d_op = op;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1899) if (!op)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1900) return;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1901) if (op->d_hash)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1902) dentry->d_flags |= DCACHE_OP_HASH;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1903) if (op->d_compare)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1904) dentry->d_flags |= DCACHE_OP_COMPARE;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1905) if (op->d_revalidate)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1906) dentry->d_flags |= DCACHE_OP_REVALIDATE;
ecf3d1f1aa74d (Jeff Layton 2013-02-20 11:19:05 -0500 1907) if (op->d_weak_revalidate)
ecf3d1f1aa74d (Jeff Layton 2013-02-20 11:19:05 -0500 1908) dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1909) if (op->d_delete)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1910) dentry->d_flags |= DCACHE_OP_DELETE;
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 1911) if (op->d_prune)
f0023bc617ba6 (Sage Weil 2011-10-28 10:02:42 -0700 1912) dentry->d_flags |= DCACHE_OP_PRUNE;
d101a125954ea (Miklos Szeredi 2016-03-26 16:14:37 -0400 1913) if (op->d_real)
d101a125954ea (Miklos Szeredi 2016-03-26 16:14:37 -0400 1914) dentry->d_flags |= DCACHE_OP_REAL;
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1915)
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1916) }
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1917) EXPORT_SYMBOL(d_set_d_op);
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 1918)
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1919)
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1920) /*
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1921) * d_set_fallthru - Mark a dentry as falling through to a lower layer
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1922) * @dentry - The dentry to mark
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1923) *
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1924) * Mark a dentry as falling through to the lower layer (as set with
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1925) * d_pin_lower()). This flag may be recorded on the medium.
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1926) */
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1927) void d_set_fallthru(struct dentry *dentry)
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1928) {
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1929) spin_lock(&dentry->d_lock);
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1930) dentry->d_flags |= DCACHE_FALLTHRU;
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1931) spin_unlock(&dentry->d_lock);
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1932) }
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1933) EXPORT_SYMBOL(d_set_fallthru);
df1a085af1f65 (David Howells 2015-01-29 12:02:28 +0000 1934)
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1935) static unsigned d_flags_for_inode(struct inode *inode)
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1936) {
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1937) unsigned add_flags = DCACHE_REGULAR_TYPE;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1938)
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1939) if (!inode)
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1940) return DCACHE_MISS_TYPE;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1941)
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1942) if (S_ISDIR(inode->i_mode)) {
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1943) add_flags = DCACHE_DIRECTORY_TYPE;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1944) if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1945) if (unlikely(!inode->i_op->lookup))
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1946) add_flags = DCACHE_AUTODIR_TYPE;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1947) else
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1948) inode->i_opflags |= IOP_LOOKUP;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1949) }
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1950) goto type_determined;
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1951) }
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1952)
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1953) if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
6b2553918d8b4 (Al Viro 2015-11-17 10:20:54 -0500 1954) if (unlikely(inode->i_op->get_link)) {
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1955) add_flags = DCACHE_SYMLINK_TYPE;
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1956) goto type_determined;
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1957) }
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1958) inode->i_opflags |= IOP_NOFOLLOW;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1959) }
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1960)
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1961) if (unlikely(!S_ISREG(inode->i_mode)))
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1962) add_flags = DCACHE_SPECIAL_TYPE;
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1963)
44bdb5e5f6382 (David Howells 2015-01-29 12:02:29 +0000 1964) type_determined:
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1965) if (unlikely(IS_AUTOMOUNT(inode)))
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1966) add_flags |= DCACHE_NEED_AUTOMOUNT;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1967) return add_flags;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1968) }
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1969)
360da90029196 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 1970) static void __d_instantiate(struct dentry *dentry, struct inode *inode)
360da90029196 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 1971) {
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1972) unsigned add_flags = d_flags_for_inode(inode);
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 1973) WARN_ON(d_in_lookup(dentry));
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 1974)
b23fb0a60379a (Nicholas Piggin 2011-01-07 17:49:35 +1100 1975) spin_lock(&dentry->d_lock);
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 1976) /*
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 1977) * Decrement negative dentry count if it was in the LRU list.
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 1978) */
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 1979) if (dentry->d_flags & DCACHE_LRU_LIST)
af0c9af1b3f66 (Waiman Long 2019-01-30 13:52:38 -0500 1980) this_cpu_dec(nr_dentry_negative);
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 1981) hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
a528aca7f359f (Al Viro 2016-02-29 12:12:46 -0500 1982) raw_write_seqcount_begin(&dentry->d_seq);
4bf46a272647d (David Howells 2015-03-05 14:09:22 +0000 1983) __d_set_inode_and_type(dentry, inode, add_flags);
a528aca7f359f (Al Viro 2016-02-29 12:12:46 -0500 1984) raw_write_seqcount_end(&dentry->d_seq);
affda48410a5b (Al Viro 2016-05-29 18:35:12 -0400 1985) fsnotify_update_flags(dentry);
b23fb0a60379a (Nicholas Piggin 2011-01-07 17:49:35 +1100 1986) spin_unlock(&dentry->d_lock);
360da90029196 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 1987) }
360da90029196 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 1988)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1989) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1990) * d_instantiate - fill in inode information for a dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1991) * @entry: dentry to complete
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1992) * @inode: inode to attach to this dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1993) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1994) * Fill in inode information in the entry.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1995) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1996) * This turns negative dentries into productive full members
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1997) * of society.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1998) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1999) * NOTE! This assumes that the inode count has been incremented
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2000) * (or otherwise set) by the caller to indicate that it is now
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2001) * in use by the dcache.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2002) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2003)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2004) void d_instantiate(struct dentry *entry, struct inode * inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2005) {
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 2006) BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 2007) if (inode) {
b96809173e94e (Al Viro 2016-04-11 00:53:26 -0400 2008) security_d_instantiate(entry, inode);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 2009) spin_lock(&inode->i_lock);
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 2010) __d_instantiate(entry, inode);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 2011) spin_unlock(&inode->i_lock);
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 2012) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2013) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2014) EXPORT_SYMBOL(d_instantiate);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2015)
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2016) /*
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2017) * This should be equivalent to d_instantiate() + unlock_new_inode(),
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2018) * with lockdep-related part of unlock_new_inode() done before
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2019) * anything else. Use that instead of open-coding d_instantiate()/
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2020) * unlock_new_inode() combinations.
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2021) */
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2022) void d_instantiate_new(struct dentry *entry, struct inode *inode)
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2023) {
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2024) BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2025) BUG_ON(!inode);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2026) lockdep_annotate_inode_mutex_key(inode);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2027) security_d_instantiate(entry, inode);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2028) spin_lock(&inode->i_lock);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2029) __d_instantiate(entry, inode);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2030) WARN_ON(!(inode->i_state & I_NEW));
c2b6d621c4ffe (Al Viro 2018-06-28 15:53:17 -0400 2031) inode->i_state &= ~I_NEW & ~I_CREATING;
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2032) smp_mb();
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2033) wake_up_bit(&inode->i_state, __I_NEW);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2034) spin_unlock(&inode->i_lock);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2035) }
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2036) EXPORT_SYMBOL(d_instantiate_new);
1e2e547a93a00 (Al Viro 2018-05-04 08:23:01 -0400 2037)
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2038) struct dentry *d_make_root(struct inode *root_inode)
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2039) {
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2040) struct dentry *res = NULL;
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2041)
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2042) if (root_inode) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2043) res = d_alloc_anon(root_inode->i_sb);
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 2044) if (res)
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2045) d_instantiate(res, root_inode);
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 2046) else
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2047) iput(root_inode);
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2048) }
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2049) return res;
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2050) }
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2051) EXPORT_SYMBOL(d_make_root);
adc0e91ab142a (Al Viro 2012-01-08 16:49:21 -0500 2052)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2053) static struct dentry *__d_instantiate_anon(struct dentry *dentry,
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2054) struct inode *inode,
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2055) bool disconnected)
4ea3ada2955e4 (Christoph Hellwig 2008-08-11 15:48:57 +0200 2056) {
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2057) struct dentry *res;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 2058) unsigned add_flags;
4ea3ada2955e4 (Christoph Hellwig 2008-08-11 15:48:57 +0200 2059)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2060) security_d_instantiate(dentry, inode);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 2061) spin_lock(&inode->i_lock);
d891eedbc3b1b (J. Bruce Fields 2011-01-18 15:45:09 -0500 2062) res = __d_find_any_alias(inode);
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2063) if (res) {
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 2064) spin_unlock(&inode->i_lock);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2065) dput(dentry);
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2066) goto out_iput;
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2067) }
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2068)
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2069) /* attach a disconnected dentry */
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2070) add_flags = d_flags_for_inode(inode);
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2071)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2072) if (disconnected)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2073) add_flags |= DCACHE_DISCONNECTED;
b18825a7c8e37 (David Howells 2013-09-12 19:22:53 +0100 2074)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2075) spin_lock(&dentry->d_lock);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2076) __d_set_inode_and_type(dentry, inode, add_flags);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2077) hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 2078) if (!disconnected) {
139351f1f9854 (Linus Torvalds 2018-02-05 13:05:20 -0800 2079) hlist_bl_lock(&dentry->d_sb->s_roots);
139351f1f9854 (Linus Torvalds 2018-02-05 13:05:20 -0800 2080) hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
139351f1f9854 (Linus Torvalds 2018-02-05 13:05:20 -0800 2081) hlist_bl_unlock(&dentry->d_sb->s_roots);
f1ee616214cb2 (NeilBrown 2017-12-21 09:45:40 +1100 2082) }
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2083) spin_unlock(&dentry->d_lock);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 2084) spin_unlock(&inode->i_lock);
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2085)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2086) return dentry;
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2087)
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2088) out_iput:
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2089) iput(inode);
9308a6128d907 (Christoph Hellwig 2008-08-11 15:49:12 +0200 2090) return res;
4ea3ada2955e4 (Christoph Hellwig 2008-08-11 15:48:57 +0200 2091) }
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2092)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2093) struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2094) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2095) return __d_instantiate_anon(dentry, inode, true);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2096) }
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2097) EXPORT_SYMBOL(d_instantiate_anon);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2098)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2099) static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2100) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2101) struct dentry *tmp;
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2102) struct dentry *res;
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2103)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2104) if (!inode)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2105) return ERR_PTR(-ESTALE);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2106) if (IS_ERR(inode))
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2107) return ERR_CAST(inode);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2108)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2109) res = d_find_any_alias(inode);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2110) if (res)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2111) goto out_iput;
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2112)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2113) tmp = d_alloc_anon(inode->i_sb);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2114) if (!tmp) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2115) res = ERR_PTR(-ENOMEM);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2116) goto out_iput;
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2117) }
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2118)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2119) return __d_instantiate_anon(tmp, inode, disconnected);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2120)
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2121) out_iput:
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2122) iput(inode);
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2123) return res;
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2124) }
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2125)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2126) /**
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2127) * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2128) * @inode: inode to allocate the dentry for
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2129) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2130) * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2131) * similar open by handle operations. The returned dentry may be anonymous,
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2132) * or may have a full name (if the inode was already in the cache).
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2133) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2134) * When called on a directory inode, we must ensure that the inode only ever
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2135) * has one dentry. If a dentry is found, that is returned instead of
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2136) * allocating a new one.
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2137) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2138) * On successful return, the reference to the inode has been transferred
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2139) * to the dentry. In case of an error the reference on the inode is released.
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2140) * To make it easier to use in export operations a %NULL or IS_ERR inode may
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2141) * be passed in and the error will be propagated to the return value,
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2142) * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2143) */
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2144) struct dentry *d_obtain_alias(struct inode *inode)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2145) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2146) return __d_obtain_alias(inode, true);
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2147) }
adc487204a937 (Benny Halevy 2009-02-27 14:02:59 -0800 2148) EXPORT_SYMBOL(d_obtain_alias);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2149)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2150) /**
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2151) * d_obtain_root - find or allocate a dentry for a given inode
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2152) * @inode: inode to allocate the dentry for
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2153) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2154) * Obtain an IS_ROOT dentry for the root of a filesystem.
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2155) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2156) * We must ensure that directory inodes only ever have one dentry. If a
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2157) * dentry is found, that is returned instead of allocating a new one.
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2158) *
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2159) * On successful return, the reference to the inode has been transferred
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2160) * to the dentry. In case of an error the reference on the inode is
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2161) * released. A %NULL or IS_ERR inode may be passed in and will be the
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2162) * error will be propagate to the return value, with a %NULL @inode
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2163) * replaced by ERR_PTR(-ESTALE).
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2164) */
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2165) struct dentry *d_obtain_root(struct inode *inode)
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2166) {
f9c34674bc60e (Miklos Szeredi 2018-01-19 11:39:52 +0100 2167) return __d_obtain_alias(inode, false);
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2168) }
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2169) EXPORT_SYMBOL(d_obtain_root);
1a0a397e41cb1 (J. Bruce Fields 2014-02-14 17:35:37 -0500 2170)
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2171) /**
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2172) * d_add_ci - lookup or allocate new dentry with case-exact name
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2173) * @inode: the inode case-insensitive lookup has found
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2174) * @dentry: the negative dentry that was passed to the parent's lookup func
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2175) * @name: the case-exact name to be associated with the returned dentry
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2176) *
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2177) * This is to avoid filling the dcache with case-insensitive names to the
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2178) * same inode, only the actual correct case is stored in the dcache for
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2179) * case-insensitive filesystems.
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2180) *
3d742d4b6ebb3 (Randy Dunlap 2021-02-24 12:00:48 -0800 2181) * For a case-insensitive lookup match and if the case-exact dentry
3d742d4b6ebb3 (Randy Dunlap 2021-02-24 12:00:48 -0800 2182) * already exists in the dcache, use it and return it.
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2183) *
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2184) * If no entry exists with the exact case name, allocate new dentry with
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2185) * the exact case, and return the spliced entry.
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2186) */
e45b590b97646 (Christoph Hellwig 2008-08-07 23:49:07 +0200 2187) struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2188) struct qstr *name)
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2189) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2190) struct dentry *found, *res;
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2191)
b6520c8193484 (Christoph Hellwig 2009-01-05 19:10:37 +0100 2192) /*
b6520c8193484 (Christoph Hellwig 2009-01-05 19:10:37 +0100 2193) * First check if a dentry matching the name already exists,
b6520c8193484 (Christoph Hellwig 2009-01-05 19:10:37 +0100 2194) * if not go ahead and create it now.
b6520c8193484 (Christoph Hellwig 2009-01-05 19:10:37 +0100 2195) */
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2196) found = d_hash_and_lookup(dentry->d_parent, name);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2197) if (found) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2198) iput(inode);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2199) return found;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2200) }
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2201) if (d_in_lookup(dentry)) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2202) found = d_alloc_parallel(dentry->d_parent, name,
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2203) dentry->d_wait);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2204) if (IS_ERR(found) || !d_in_lookup(found)) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2205) iput(inode);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2206) return found;
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2207) }
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2208) } else {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2209) found = d_alloc(dentry->d_parent, name);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2210) if (!found) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2211) iput(inode);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2212) return ERR_PTR(-ENOMEM);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2213) }
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2214) }
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2215) res = d_splice_alias(inode, found);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2216) if (res) {
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2217) dput(found);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2218) return res;
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2219) }
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2220) return found;
9403540c06531 (Barry Naujok 2008-05-21 16:50:46 +1000 2221) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2222) EXPORT_SYMBOL(d_add_ci);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2223)
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2224)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2225) static inline bool d_same_name(const struct dentry *dentry,
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2226) const struct dentry *parent,
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2227) const struct qstr *name)
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2228) {
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2229) if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2230) if (dentry->d_name.len != name->len)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2231) return false;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2232) return dentry_cmp(dentry, name->name, name->len) == 0;
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2233) }
6fa67e7075593 (Al Viro 2016-07-31 16:37:25 -0400 2234) return parent->d_op->d_compare(dentry,
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2235) dentry->d_name.len, dentry->d_name.name,
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2236) name) == 0;
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2237) }
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2238)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2239) /**
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2240) * __d_lookup_rcu - search for a dentry (racy, store-free)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2241) * @parent: parent dentry
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2242) * @name: qstr of name we wish to find
1f1e6e523e43e (Randy Dunlap 2012-03-18 21:23:05 -0700 2243) * @seqp: returns d_seq value at the point where the dentry was found
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2244) * Returns: dentry, or NULL
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2245) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2246) * __d_lookup_rcu is the dcache lookup function for rcu-walk name
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2247) * resolution (store-free path walking) design described in
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2248) * Documentation/filesystems/path-lookup.txt.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2249) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2250) * This is not to be used outside core vfs.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2251) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2252) * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2253) * held, and rcu_read_lock held. The returned dentry must not be stored into
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2254) * without taking d_lock and checking d_seq sequence count against @seq
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2255) * returned here.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2256) *
15570086b590a (Linus Torvalds 2013-09-02 11:38:06 -0700 2257) * A refcount may be taken on the found dentry with the d_rcu_to_refcount
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2258) * function.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2259) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2260) * Alternatively, __d_lookup_rcu may be called again to look up the child of
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2261) * the returned dentry, so long as its parent's seqlock is checked after the
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2262) * child is looked up. Thus, an interlocking stepping of sequence lock checks
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2263) * is formed, giving integrity down the path walk.
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2264) *
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2265) * NOTE! The caller *has* to check the resulting dentry against the sequence
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2266) * number we've returned before using any of the resulting dentry state!
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2267) */
8966be90304b3 (Linus Torvalds 2012-03-02 14:23:30 -0800 2268) struct dentry *__d_lookup_rcu(const struct dentry *parent,
8966be90304b3 (Linus Torvalds 2012-03-02 14:23:30 -0800 2269) const struct qstr *name,
da53be12bbb4f (Linus Torvalds 2013-05-21 15:22:44 -0700 2270) unsigned *seqp)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2271) {
26fe575028703 (Linus Torvalds 2012-05-10 13:14:12 -0700 2272) u64 hashlen = name->hash_len;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2273) const unsigned char *str = name->name;
8387ff2577eb9 (Linus Torvalds 2016-06-10 07:51:30 -0700 2274) struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 2275) struct hlist_bl_node *node;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2276) struct dentry *dentry;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2277)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2278) /*
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2279) * Note: There is significant duplication with __d_lookup_rcu which is
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2280) * required to prevent single threaded performance regressions
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2281) * especially on architectures where smp_rmb (in seqcounts) are costly.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2282) * Keep the two functions in sync.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2283) */
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2284)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2285) /*
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2286) * The hash list is protected using RCU.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2287) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2288) * Carefully use d_seq when comparing a candidate dentry, to avoid
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2289) * races with d_move().
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2290) *
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2291) * It is possible that concurrent renames can mess up our list
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2292) * walk here and result in missing our dentry, resulting in the
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2293) * false-negative result. d_lookup() protects against concurrent
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2294) * renames using rename_lock seqlock.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2295) *
b0a4bb830e099 (Namhyung Kim 2011-01-22 15:31:32 +0900 2296) * See Documentation/filesystems/path-lookup.txt for more details.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2297) */
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 2298) hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
8966be90304b3 (Linus Torvalds 2012-03-02 14:23:30 -0800 2299) unsigned seq;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2300)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2301) seqretry:
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2302) /*
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2303) * The dentry sequence count protects us from concurrent
da53be12bbb4f (Linus Torvalds 2013-05-21 15:22:44 -0700 2304) * renames, and thus protects parent and name fields.
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2305) *
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2306) * The caller must perform a seqcount check in order
da53be12bbb4f (Linus Torvalds 2013-05-21 15:22:44 -0700 2307) * to do anything useful with the returned dentry.
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2308) *
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2309) * NOTE! We do a "raw" seqcount_begin here. That means that
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2310) * we don't wait for the sequence count to stabilize if it
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2311) * is in the middle of a sequence change. If we do the slow
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2312) * dentry compare, we will do seqretries until it is stable,
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2313) * and if we end up with a successful lookup, we actually
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2314) * want to exit RCU lookup anyway.
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2315) *
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2316) * Note that raw_seqcount_begin still *does* smp_rmb(), so
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2317) * we are still guaranteed NUL-termination of ->d_name.name.
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2318) */
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2319) seq = raw_seqcount_begin(&dentry->d_seq);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2320) if (dentry->d_parent != parent)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2321) continue;
2e321806b681b (Linus Torvalds 2012-05-21 18:48:10 -0700 2322) if (d_unhashed(dentry))
2e321806b681b (Linus Torvalds 2012-05-21 18:48:10 -0700 2323) continue;
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2324)
830c0f0edca67 (Linus Torvalds 2011-08-06 22:41:50 -0700 2325) if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2326) int tlen;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2327) const char *tname;
26fe575028703 (Linus Torvalds 2012-05-10 13:14:12 -0700 2328) if (dentry->d_name.hash != hashlen_hash(hashlen))
26fe575028703 (Linus Torvalds 2012-05-10 13:14:12 -0700 2329) continue;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2330) tlen = dentry->d_name.len;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2331) tname = dentry->d_name.name;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2332) /* we want a consistent (name,len) pair */
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2333) if (read_seqcount_retry(&dentry->d_seq, seq)) {
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2334) cpu_relax();
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2335) goto seqretry;
12f8ad4b0533d (Linus Torvalds 2012-05-04 14:59:14 -0700 2336) }
6fa67e7075593 (Al Viro 2016-07-31 16:37:25 -0400 2337) if (parent->d_op->d_compare(dentry,
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2338) tlen, tname, name) != 0)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2339) continue;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2340) } else {
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2341) if (dentry->d_name.hash_len != hashlen)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2342) continue;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2343) if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2344) continue;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2345) }
da53be12bbb4f (Linus Torvalds 2013-05-21 15:22:44 -0700 2346) *seqp = seq;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2347) return dentry;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2348) }
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2349) return NULL;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2350) }
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2351)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2352) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2353) * d_lookup - search for a dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2354) * @parent: parent dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2355) * @name: qstr of name we wish to find
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2356) * Returns: dentry, or NULL
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2357) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2358) * d_lookup searches the children of the parent dentry for the name in
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2359) * question. If the dentry is found its reference count is incremented and the
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2360) * dentry is returned. The caller must use dput to free the entry when it has
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2361) * finished using it. %NULL is returned if the dentry does not exist.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2362) */
da2d8455ed7eb (Al Viro 2013-01-24 18:29:34 -0500 2363) struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2364) {
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2365) struct dentry *dentry;
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 2366) unsigned seq;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2367)
b8314f9303a98 (Daeseok Youn 2014-08-11 11:46:53 +0900 2368) do {
b8314f9303a98 (Daeseok Youn 2014-08-11 11:46:53 +0900 2369) seq = read_seqbegin(&rename_lock);
b8314f9303a98 (Daeseok Youn 2014-08-11 11:46:53 +0900 2370) dentry = __d_lookup(parent, name);
b8314f9303a98 (Daeseok Youn 2014-08-11 11:46:53 +0900 2371) if (dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2372) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2373) } while (read_seqretry(&rename_lock, seq));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2374) return dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2375) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2376) EXPORT_SYMBOL(d_lookup);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2377)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2378) /**
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2379) * __d_lookup - search for a dentry (racy)
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2380) * @parent: parent dentry
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2381) * @name: qstr of name we wish to find
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2382) * Returns: dentry, or NULL
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2383) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2384) * __d_lookup is like d_lookup, however it may (rarely) return a
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2385) * false-negative result due to unrelated rename activity.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2386) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2387) * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2388) * however it must be used carefully, eg. with a following d_lookup in
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2389) * the case of failure.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2390) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2391) * __d_lookup callers must be commented.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2392) */
a713ca2ab9d14 (Al Viro 2013-01-24 18:27:00 -0500 2393) struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2394) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2395) unsigned int hash = name->hash;
8387ff2577eb9 (Linus Torvalds 2016-06-10 07:51:30 -0700 2396) struct hlist_bl_head *b = d_hash(hash);
ceb5bdc2d246f (Nicholas Piggin 2011-01-07 17:50:05 +1100 2397) struct hlist_bl_node *node;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2398) struct dentry *found = NULL;
665a7583f32ab (Paul E. McKenney 2005-11-07 00:59:17 -0800 2399) struct dentry *dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2400)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2401) /*
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2402) * Note: There is significant duplication with __d_lookup_rcu which is
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2403) * required to prevent single threaded performance regressions
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2404) * especially on architectures where smp_rmb (in seqcounts) are costly.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2405) * Keep the two functions in sync.
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2406) */
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2407)
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2408) /*
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2409) * The hash list is protected using RCU.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2410) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2411) * Take d_lock when comparing a candidate dentry, to avoid races
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2412) * with d_move().
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2413) *
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2414) * It is possible that concurrent renames can mess up our list
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2415) * walk here and result in missing our dentry, resulting in the
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2416) * false-negative result. d_lookup() protects against concurrent
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2417) * renames using rename_lock seqlock.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2418) *
b0a4bb830e099 (Namhyung Kim 2011-01-22 15:31:32 +0900 2419) * See Documentation/filesystems/path-lookup.txt for more details.
b04f784e5d19e (Nicholas Piggin 2010-08-18 04:37:34 +1000 2420) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2421) rcu_read_lock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2422)
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 2423) hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2424)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2425) if (dentry->d_name.hash != hash)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2426) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2427)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2428) spin_lock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2429) if (dentry->d_parent != parent)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2430) goto next;
d0185c0882d76 (Linus Torvalds 2008-09-29 07:42:57 -0700 2431) if (d_unhashed(dentry))
d0185c0882d76 (Linus Torvalds 2008-09-29 07:42:57 -0700 2432) goto next;
d0185c0882d76 (Linus Torvalds 2008-09-29 07:42:57 -0700 2433)
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2434) if (!d_same_name(dentry, parent, name))
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2435) goto next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2436)
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 2437) dentry->d_lockref.count++;
d0185c0882d76 (Linus Torvalds 2008-09-29 07:42:57 -0700 2438) found = dentry;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2439) spin_unlock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2440) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2441) next:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2442) spin_unlock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2443) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2444) rcu_read_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2445)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2446) return found;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2447) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2448)
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2449) /**
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2450) * d_hash_and_lookup - hash the qstr then search for a dentry
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2451) * @dir: Directory to search in
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2452) * @name: qstr of name we wish to find
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2453) *
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2454) * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2455) */
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2456) struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2457) {
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2458) /*
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2459) * Check for a fs-specific hash function. Note that we must
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2460) * calculate the standard hash first, as the d_op->d_hash()
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2461) * routine may choose to leave the hash value unchanged.
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2462) */
8387ff2577eb9 (Linus Torvalds 2016-06-10 07:51:30 -0700 2463) name->hash = full_name_hash(dir, name->name, name->len);
fb045adb99d9b (Nicholas Piggin 2011-01-07 17:49:55 +1100 2464) if (dir->d_flags & DCACHE_OP_HASH) {
da53be12bbb4f (Linus Torvalds 2013-05-21 15:22:44 -0700 2465) int err = dir->d_op->d_hash(dir, name);
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2466) if (unlikely(err < 0))
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2467) return ERR_PTR(err);
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2468) }
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2469) return d_lookup(dir, name);
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2470) }
4f522a247bc26 (Al Viro 2013-02-11 23:20:37 -0500 2471) EXPORT_SYMBOL(d_hash_and_lookup);
3e7e241f8c5c8 (Eric W. Biederman 2006-03-31 02:31:43 -0800 2472)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2473) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2474) * When a file is deleted, we have two options:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2475) * - turn this dentry into a negative dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2476) * - unhash this dentry and free it.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2477) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2478) * Usually, we want to just turn this into
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2479) * a negative dentry, but if anybody else is
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2480) * currently using the dentry or the inode
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2481) * we can't do that and we fall back on removing
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2482) * it from the hash queues and waiting for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2483) * it to be deleted later when it has no users
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2484) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2485)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2486) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2487) * d_delete - delete a dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2488) * @dentry: The dentry to delete
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2489) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2490) * Turn the dentry into a negative dentry if possible, otherwise
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2491) * remove it from the hash queues so it can be deleted later
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2492) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2493)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2494) void d_delete(struct dentry * dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2495) {
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2496) struct inode *inode = dentry->d_inode;
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2497)
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2498) spin_lock(&inode->i_lock);
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2499) spin_lock(&dentry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2500) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2501) * Are we the only user?
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2502) */
98474236f72e5 (Waiman Long 2013-08-28 18:24:59 -0700 2503) if (dentry->d_lockref.count == 1) {
13e3c5e5b9c67 (Al Viro 2010-05-21 16:11:04 -0400 2504) dentry->d_flags &= ~DCACHE_CANT_MOUNT;
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2505) dentry_unlink_inode(dentry);
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2506) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2507) __d_drop(dentry);
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2508) spin_unlock(&dentry->d_lock);
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2509) spin_unlock(&inode->i_lock);
c19457f0aed7f (Al Viro 2018-02-23 21:02:31 -0500 2510) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2511) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2512) EXPORT_SYMBOL(d_delete);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2513)
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2514) static void __d_rehash(struct dentry *entry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2515) {
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2516) struct hlist_bl_head *b = d_hash(entry->d_name.hash);
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 2517)
1879fd6a26571 (Christoph Hellwig 2011-04-25 14:01:36 -0400 2518) hlist_bl_lock(b);
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 2519) hlist_bl_add_head_rcu(&entry->d_hash, b);
1879fd6a26571 (Christoph Hellwig 2011-04-25 14:01:36 -0400 2520) hlist_bl_unlock(b);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2521) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2522)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2523) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2524) * d_rehash - add an entry back to the hash
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2525) * @entry: dentry to add to the hash
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2526) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2527) * Adds a dentry to the hash according to its name.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2528) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2529)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2530) void d_rehash(struct dentry * entry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2531) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2532) spin_lock(&entry->d_lock);
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2533) __d_rehash(entry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2534) spin_unlock(&entry->d_lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2535) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2536) EXPORT_SYMBOL(d_rehash);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2537)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2538) static inline unsigned start_dir_add(struct inode *dir)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2539) {
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2540) /*
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2541) * The caller has a spinlock_t (dentry::d_lock) acquired which disables
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2542) * preemption on !PREEMPT_RT. On PREEMPT_RT the lock does not disable
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2543) * preemption and it has be done explicitly.
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2544) */
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2545) if (IS_ENABLED(CONFIG_PREEMPT_RT))
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2546) preempt_disable();
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2547) for (;;) {
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2548) unsigned n = dir->i_dir_seq;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2549) if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2550) return n;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2551) cpu_relax();
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2552) }
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2553) }
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2554)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2555) static inline void end_dir_add(struct inode *dir, unsigned n)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2556) {
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2557) smp_store_release(&dir->i_dir_seq, n + 2);
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2558) if (IS_ENABLED(CONFIG_PREEMPT_RT))
7b829c0257ae9 (Sebastian Andrzej Siewior 2017-10-20 11:29:53 +0200 2559) preempt_enable();
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2560) }
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2561)
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2562) static void d_wait_lookup(struct dentry *dentry)
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2563) {
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2564) struct swait_queue __wait;
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2565)
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2566) if (!d_in_lookup(dentry))
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2567) return;
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2568)
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2569) INIT_LIST_HEAD(&__wait.task_list);
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2570) do {
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2571) prepare_to_swait_exclusive(dentry->d_wait, &__wait, TASK_UNINTERRUPTIBLE);
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2572) spin_unlock(&dentry->d_lock);
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2573) schedule();
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2574) spin_lock(&dentry->d_lock);
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2575) } while (d_in_lookup(dentry));
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2576) finish_swait(dentry->d_wait, &__wait);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2577) }
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2578)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2579) struct dentry *d_alloc_parallel(struct dentry *parent,
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2580) const struct qstr *name,
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2581) struct swait_queue_head *wq)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2582) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2583) unsigned int hash = name->hash;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2584) struct hlist_bl_head *b = in_lookup_hash(parent, hash);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2585) struct hlist_bl_node *node;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2586) struct dentry *new = d_alloc(parent, name);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2587) struct dentry *dentry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2588) unsigned seq, r_seq, d_seq;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2589)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2590) if (unlikely(!new))
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2591) return ERR_PTR(-ENOMEM);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2592)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2593) retry:
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2594) rcu_read_lock();
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2595) seq = smp_load_acquire(&parent->d_inode->i_dir_seq);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2596) r_seq = read_seqbegin(&rename_lock);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2597) dentry = __d_lookup_rcu(parent, name, &d_seq);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2598) if (unlikely(dentry)) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2599) if (!lockref_get_not_dead(&dentry->d_lockref)) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2600) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2601) goto retry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2602) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2603) if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2604) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2605) dput(dentry);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2606) goto retry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2607) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2608) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2609) dput(new);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2610) return dentry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2611) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2612) if (unlikely(read_seqretry(&rename_lock, r_seq))) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2613) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2614) goto retry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2615) }
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2616)
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2617) if (unlikely(seq & 1)) {
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2618) rcu_read_unlock();
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2619) goto retry;
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2620) }
015555fd4d293 (Will Deacon 2018-02-19 14:55:54 +0000 2621)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2622) hlist_bl_lock(b);
8cc07c808c9d5 (Will Deacon 2018-02-19 14:55:55 +0000 2623) if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2624) hlist_bl_unlock(b);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2625) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2626) goto retry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2627) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2628) /*
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2629) * No changes for the parent since the beginning of d_lookup().
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2630) * Since all removals from the chain happen with hlist_bl_lock(),
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2631) * any potential in-lookup matches are going to stay here until
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2632) * we unlock the chain. All fields are stable in everything
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2633) * we encounter.
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2634) */
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2635) hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2636) if (dentry->d_name.hash != hash)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2637) continue;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2638) if (dentry->d_parent != parent)
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2639) continue;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2640) if (!d_same_name(dentry, parent, name))
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2641) continue;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2642) hlist_bl_unlock(b);
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2643) /* now we can try to grab a reference */
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2644) if (!lockref_get_not_dead(&dentry->d_lockref)) {
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2645) rcu_read_unlock();
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2646) goto retry;
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2647) }
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2648)
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2649) rcu_read_unlock();
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2650) /*
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2651) * somebody is likely to be still doing lookup for it;
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2652) * wait for them to finish
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2653) */
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2654) spin_lock(&dentry->d_lock);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2655) d_wait_lookup(dentry);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2656) /*
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2657) * it's not in-lookup anymore; in principle we should repeat
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2658) * everything from dcache lookup, but it's likely to be what
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2659) * d_lookup() would've found anyway. If it is, just return it;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2660) * otherwise we really have to repeat the whole thing.
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2661) */
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2662) if (unlikely(dentry->d_name.hash != hash))
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2663) goto mismatch;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2664) if (unlikely(dentry->d_parent != parent))
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2665) goto mismatch;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2666) if (unlikely(d_unhashed(dentry)))
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2667) goto mismatch;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2668) if (unlikely(!d_same_name(dentry, parent, name)))
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2669) goto mismatch;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2670) /* OK, it *is* a hashed match; return it */
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2671) spin_unlock(&dentry->d_lock);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2672) dput(new);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2673) return dentry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2674) }
e7d6ef9790bc2 (Al Viro 2016-06-20 01:35:59 -0400 2675) rcu_read_unlock();
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2676) /* we can't take ->d_lock here; it's OK, though. */
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2677) new->d_flags |= DCACHE_PAR_LOOKUP;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2678) new->d_wait = wq;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2679) hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2680) hlist_bl_unlock(b);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2681) return new;
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2682) mismatch:
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2683) spin_unlock(&dentry->d_lock);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2684) dput(dentry);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2685) goto retry;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2686) }
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2687) EXPORT_SYMBOL(d_alloc_parallel);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2688)
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2689) void __d_lookup_done(struct dentry *dentry)
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2690) {
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2691) struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2692) dentry->d_name.hash);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2693) hlist_bl_lock(b);
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2694) dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2695) __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
f530d6efc0c93 (Sebastian Andrzej Siewior 2016-09-14 14:35:49 +0200 2696) swake_up_all(dentry->d_wait);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2697) dentry->d_wait = NULL;
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2698) hlist_bl_unlock(b);
94bdd655caba2 (Al Viro 2016-04-15 02:42:04 -0400 2699) INIT_HLIST_NODE(&dentry->d_u.d_alias);
d9171b9345261 (Al Viro 2016-04-15 03:33:13 -0400 2700) INIT_LIST_HEAD(&dentry->d_lru);
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2701) }
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2702) EXPORT_SYMBOL(__d_lookup_done);
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2703)
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2704) /* inode->i_lock held if inode is non-NULL */
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2705)
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2706) static inline void __d_add(struct dentry *dentry, struct inode *inode)
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2707) {
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2708) struct inode *dir = NULL;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2709) unsigned n;
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2710) spin_lock(&dentry->d_lock);
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2711) if (unlikely(d_in_lookup(dentry))) {
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2712) dir = dentry->d_parent->d_inode;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2713) n = start_dir_add(dir);
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2714) __d_lookup_done(dentry);
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2715) }
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2716) if (inode) {
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2717) unsigned add_flags = d_flags_for_inode(inode);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2718) hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2719) raw_write_seqcount_begin(&dentry->d_seq);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2720) __d_set_inode_and_type(dentry, inode, add_flags);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2721) raw_write_seqcount_end(&dentry->d_seq);
affda48410a5b (Al Viro 2016-05-29 18:35:12 -0400 2722) fsnotify_update_flags(dentry);
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2723) }
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2724) __d_rehash(dentry);
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2725) if (dir)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2726) end_dir_add(dir, n);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2727) spin_unlock(&dentry->d_lock);
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2728) if (inode)
0568d705b0087 (Al Viro 2016-04-14 19:40:56 -0400 2729) spin_unlock(&inode->i_lock);
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2730) }
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2731)
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2732) /**
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2733) * d_add - add dentry to hash queues
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2734) * @entry: dentry to add
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2735) * @inode: The inode to attach to this dentry
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2736) *
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2737) * This adds the entry to the hash queues and initializes @inode.
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2738) * The entry was actually filled in earlier during d_alloc().
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2739) */
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2740)
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2741) void d_add(struct dentry *entry, struct inode *inode)
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2742) {
b96809173e94e (Al Viro 2016-04-11 00:53:26 -0400 2743) if (inode) {
b96809173e94e (Al Viro 2016-04-11 00:53:26 -0400 2744) security_d_instantiate(entry, inode);
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2745) spin_lock(&inode->i_lock);
b96809173e94e (Al Viro 2016-04-11 00:53:26 -0400 2746) }
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 2747) __d_add(entry, inode);
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2748) }
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2749) EXPORT_SYMBOL(d_add);
34d0d19dc0929 (Al Viro 2016-03-08 21:01:03 -0500 2750)
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2751) /**
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2752) * d_exact_alias - find and hash an exact unhashed alias
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2753) * @entry: dentry to add
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2754) * @inode: The inode to go with this dentry
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2755) *
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2756) * If an unhashed dentry with the same name/parent and desired
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2757) * inode already exists, hash and return it. Otherwise, return
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2758) * NULL.
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2759) *
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2760) * Parent directory should be locked.
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2761) */
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2762) struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2763) {
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2764) struct dentry *alias;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2765) unsigned int hash = entry->d_name.hash;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2766)
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2767) spin_lock(&inode->i_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2768) hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2769) /*
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2770) * Don't need alias->d_lock here, because aliases with
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2771) * d_parent == entry->d_parent are not subject to name or
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2772) * parent changes, because the parent inode i_mutex is held.
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2773) */
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2774) if (alias->d_name.hash != hash)
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2775) continue;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2776) if (alias->d_parent != entry->d_parent)
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2777) continue;
d4c91a8f7e551 (Al Viro 2016-06-25 23:33:49 -0400 2778) if (!d_same_name(alias, entry->d_parent, &entry->d_name))
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2779) continue;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2780) spin_lock(&alias->d_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2781) if (!d_unhashed(alias)) {
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2782) spin_unlock(&alias->d_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2783) alias = NULL;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2784) } else {
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2785) __dget_dlock(alias);
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2786) __d_rehash(alias);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2787) spin_unlock(&alias->d_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2788) }
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2789) spin_unlock(&inode->i_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2790) return alias;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2791) }
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2792) spin_unlock(&inode->i_lock);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2793) return NULL;
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2794) }
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2795) EXPORT_SYMBOL(d_exact_alias);
668d0cd56ef7b (Al Viro 2016-03-08 12:44:17 -0500 2796)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2797) static void swap_names(struct dentry *dentry, struct dentry *target)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2798) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2799) if (unlikely(dname_external(target))) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2800) if (unlikely(dname_external(dentry))) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2801) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2802) * Both external: swap the pointers
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2803) */
9a8d5bb4ad829 (Wu Fengguang 2009-01-07 18:09:14 -0800 2804) swap(target->d_name.name, dentry->d_name.name);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2805) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2806) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2807) * dentry:internal, target:external. Steal target's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2808) * storage and make target internal.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2809) */
321bcf9216303 (J. Bruce Fields 2007-10-21 16:41:38 -0700 2810) memcpy(target->d_iname, dentry->d_name.name,
321bcf9216303 (J. Bruce Fields 2007-10-21 16:41:38 -0700 2811) dentry->d_name.len + 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2812) dentry->d_name.name = target->d_name.name;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2813) target->d_name.name = target->d_iname;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2814) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2815) } else {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2816) if (unlikely(dname_external(dentry))) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2817) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2818) * dentry:external, target:internal. Give dentry's
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2819) * storage to target and make dentry internal
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2820) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2821) memcpy(dentry->d_iname, target->d_name.name,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2822) target->d_name.len + 1);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2823) target->d_name.name = dentry->d_name.name;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2824) dentry->d_name.name = dentry->d_iname;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2825) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2826) /*
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2827) * Both are internal.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2828) */
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2829) unsigned int i;
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2830) BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2831) for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2832) swap(((long *) &dentry->d_iname)[i],
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2833) ((long *) &target->d_iname)[i]);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2834) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2835) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2836) }
a28ddb87cdddb (Linus Torvalds 2014-09-24 12:27:39 -0700 2837) swap(dentry->d_name.hash_len, target->d_name.hash_len);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2838) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2839)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2840) static void copy_name(struct dentry *dentry, struct dentry *target)
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2841) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2842) struct external_name *old_name = NULL;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2843) if (unlikely(dname_external(dentry)))
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2844) old_name = external_name(dentry);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2845) if (unlikely(dname_external(target))) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2846) atomic_inc(&external_name(target)->u.count);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2847) dentry->d_name = target->d_name;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2848) } else {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2849) memcpy(dentry->d_iname, target->d_name.name,
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2850) target->d_name.len + 1);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2851) dentry->d_name.name = dentry->d_iname;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2852) dentry->d_name.hash_len = target->d_name.hash_len;
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2853) }
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2854) if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2e03b4bc4ae84 (Vlastimil Babka 2018-10-26 15:05:41 -0700 2855) kfree_rcu(old_name, u.head);
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2856) }
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2857)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2858) /*
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2859) * __d_move - move a dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2860) * @dentry: entry to move
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2861) * @target: new dentry
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2862) * @exchange: exchange the two dentries
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2863) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2864) * Update the dcache to reflect the move of a file name. Negative
c46c887744b33 (Jeff Layton 2011-07-26 13:33:16 -0400 2865) * dcache entries should not be moved in this way. Caller must hold
c46c887744b33 (Jeff Layton 2011-07-26 13:33:16 -0400 2866) * rename_lock, the i_mutex of the source and target directories,
c46c887744b33 (Jeff Layton 2011-07-26 13:33:16 -0400 2867) * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2868) */
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2869) static void __d_move(struct dentry *dentry, struct dentry *target,
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2870) bool exchange)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2871) {
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2872) struct dentry *old_parent, *p;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2873) struct inode *dir = NULL;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2874) unsigned n;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2875)
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2876) WARN_ON(!dentry->d_inode);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2877) if (WARN_ON(dentry == target))
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2878) return;
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2879)
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 2880) BUG_ON(d_ancestor(target, dentry));
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2881) old_parent = dentry->d_parent;
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2882) p = d_ancestor(old_parent, target);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2883) if (IS_ROOT(dentry)) {
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2884) BUG_ON(p);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2885) spin_lock(&target->d_parent->d_lock);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2886) } else if (!p) {
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2887) /* target is not a descendent of dentry->d_parent */
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2888) spin_lock(&target->d_parent->d_lock);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2889) spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2890) } else {
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2891) BUG_ON(p == dentry);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2892) spin_lock(&old_parent->d_lock);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2893) if (p != target)
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2894) spin_lock_nested(&target->d_parent->d_lock,
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2895) DENTRY_D_LOCK_NESTED);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2896) }
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2897) spin_lock_nested(&dentry->d_lock, 2);
42177007aa277 (Al Viro 2018-03-11 15:15:46 -0400 2898) spin_lock_nested(&target->d_lock, 3);
2fd6b7f50797f (Nicholas Piggin 2011-01-07 17:49:34 +1100 2899)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2900) if (unlikely(d_in_lookup(target))) {
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2901) dir = target->d_parent->d_inode;
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2902) n = start_dir_add(dir);
85c7f81041d57 (Al Viro 2016-04-14 19:52:13 -0400 2903) __d_lookup_done(target);
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2904) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2905)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2906) write_seqcount_begin(&dentry->d_seq);
1ca7d67cf5d5a (John Stultz 2013-10-07 15:51:59 -0700 2907) write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2908)
15d3c589f6305 (Al Viro 2016-07-29 17:45:21 -0400 2909) /* unhash both */
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 2910) if (!d_unhashed(dentry))
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 2911) ___d_drop(dentry);
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 2912) if (!d_unhashed(target))
0632a9ac7bc0a (Al Viro 2018-03-07 00:49:10 -0500 2913) ___d_drop(target);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2914)
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2915) /* ... and switch them in the tree */
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2916) dentry->d_parent = target->d_parent;
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2917) if (!exchange) {
8d85b4845a668 (Al Viro 2014-09-29 14:54:27 -0400 2918) copy_name(dentry, target);
61647823aa920 (NeilBrown 2017-11-10 15:45:41 +1100 2919) target->d_hash.pprev = NULL;
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2920) dentry->d_parent->d_lockref.count++;
5467a68cbf688 (Al Viro 2019-03-15 22:23:19 -0400 2921) if (dentry != old_parent) /* wasn't IS_ROOT */
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2922) WARN_ON(!--old_parent->d_lockref.count);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2923) } else {
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2924) target->d_parent = old_parent;
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2925) swap_names(dentry, target);
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 2926) list_move(&target->d_child, &target->d_parent->d_subdirs);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2927) __d_rehash(target);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2928) fsnotify_update_flags(target);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2929) }
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2930) list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2931) __d_rehash(dentry);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2932) fsnotify_update_flags(dentry);
0bf3d5c1604ec (Eric Biggers 2019-03-20 11:39:11 -0700 2933) fscrypt_handle_d_move(dentry);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2934)
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2935) write_seqcount_end(&target->d_seq);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2936) write_seqcount_end(&dentry->d_seq);
31e6b01f4183f (Nicholas Piggin 2011-01-07 17:49:52 +1100 2937)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2938) if (dir)
84e710da2a1df (Al Viro 2016-04-15 00:58:55 -0400 2939) end_dir_add(dir, n);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2940)
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2941) if (dentry->d_parent != old_parent)
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2942) spin_unlock(&dentry->d_parent->d_lock);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2943) if (dentry != old_parent)
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2944) spin_unlock(&old_parent->d_lock);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2945) spin_unlock(&target->d_lock);
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 2946) spin_unlock(&dentry->d_lock);
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2947) }
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2948)
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2949) /*
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2950) * d_move - move a dentry
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2951) * @dentry: entry to move
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2952) * @target: new dentry
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2953) *
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2954) * Update the dcache to reflect the move of a file name. Negative
c46c887744b33 (Jeff Layton 2011-07-26 13:33:16 -0400 2955) * dcache entries should not be moved in this way. See the locking
c46c887744b33 (Jeff Layton 2011-07-26 13:33:16 -0400 2956) * requirements for __d_move.
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2957) */
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2958) void d_move(struct dentry *dentry, struct dentry *target)
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2959) {
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 2960) write_seqlock(&rename_lock);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2961) __d_move(dentry, target, false);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2962) write_sequnlock(&rename_lock);
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2963) }
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 2964) EXPORT_SYMBOL(d_move);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2965)
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2966) /*
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2967) * d_exchange - exchange two dentries
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2968) * @dentry1: first dentry
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2969) * @dentry2: second dentry
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2970) */
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2971) void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2972) {
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2973) write_seqlock(&rename_lock);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2974)
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2975) WARN_ON(!dentry1->d_inode);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2976) WARN_ON(!dentry2->d_inode);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2977) WARN_ON(IS_ROOT(dentry1));
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2978) WARN_ON(IS_ROOT(dentry2));
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2979)
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2980) __d_move(dentry1, dentry2, true);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2981)
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2982) write_sequnlock(&rename_lock);
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2983) }
da1ce0670c14d (Miklos Szeredi 2014-04-01 17:08:43 +0200 2984)
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2985) /**
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2986) * d_ancestor - search for an ancestor
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2987) * @p1: ancestor dentry
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2988) * @p2: child dentry
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2989) *
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2990) * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2991) * an ancestor of p2, else NULL.
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2992) */
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2993) struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2994) {
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2995) struct dentry *p;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2996)
871c0067d53ba (OGAWA Hirofumi 2008-10-16 07:50:27 +0900 2997) for (p = p2; !IS_ROOT(p); p = p->d_parent) {
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 2998) if (p->d_parent == p1)
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 2999) return p;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3000) }
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3001) return NULL;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3002) }
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3003)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3004) /*
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3005) * This helper attempts to cope with remotely renamed directories
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3006) *
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3007) * It assumes that the caller is already holding
a03e283bf5c3d (Eric W. Biederman 2015-08-15 13:36:41 -0500 3008) * dentry->d_parent->d_inode->i_mutex, and rename_lock
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3009) *
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3010) * Note: If ever the locking in lock_rename() changes, then please
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3011) * remember to update this too...
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3012) */
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3013) static int __d_unalias(struct inode *inode,
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 3014) struct dentry *dentry, struct dentry *alias)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3015) {
9902af79c01a8 (Al Viro 2016-04-15 15:08:36 -0400 3016) struct mutex *m1 = NULL;
9902af79c01a8 (Al Viro 2016-04-15 15:08:36 -0400 3017) struct rw_semaphore *m2 = NULL;
3d330dc175d3f (J. Bruce Fields 2015-02-10 10:55:53 -0500 3018) int ret = -ESTALE;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3019)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3020) /* If alias and dentry share a parent, then no extra locks required */
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3021) if (alias->d_parent == dentry->d_parent)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3022) goto out_unalias;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3023)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3024) /* See lock_rename() */
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3025) if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3026) goto out_err;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3027) m1 = &dentry->d_sb->s_vfs_rename_mutex;
9902af79c01a8 (Al Viro 2016-04-15 15:08:36 -0400 3028) if (!inode_trylock_shared(alias->d_parent->d_inode))
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3029) goto out_err;
9902af79c01a8 (Al Viro 2016-04-15 15:08:36 -0400 3030) m2 = &alias->d_parent->d_inode->i_rwsem;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3031) out_unalias:
8ed936b5671bf (Eric W. Biederman 2013-10-01 18:33:48 -0700 3032) __d_move(alias, dentry, false);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3033) ret = 0;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3034) out_err:
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3035) if (m2)
9902af79c01a8 (Al Viro 2016-04-15 15:08:36 -0400 3036) up_read(m2);
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3037) if (m1)
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3038) mutex_unlock(m1);
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3039) return ret;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3040) }
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3041)
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3042) /**
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3043) * d_splice_alias - splice a disconnected dentry into the tree if one exists
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3044) * @inode: the inode which may have a disconnected dentry
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3045) * @dentry: a negative dentry which we want to point to the inode.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3046) *
da093a9b76efc (J. Bruce Fields 2014-02-17 18:03:57 -0500 3047) * If inode is a directory and has an IS_ROOT alias, then d_move that in
da093a9b76efc (J. Bruce Fields 2014-02-17 18:03:57 -0500 3048) * place of the given dentry and return it, else simply d_add the inode
da093a9b76efc (J. Bruce Fields 2014-02-17 18:03:57 -0500 3049) * to the dentry and return NULL.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3050) *
908790fa3b779 (J. Bruce Fields 2014-02-17 17:58:42 -0500 3051) * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
908790fa3b779 (J. Bruce Fields 2014-02-17 17:58:42 -0500 3052) * we should error out: directories can't have multiple aliases.
908790fa3b779 (J. Bruce Fields 2014-02-17 17:58:42 -0500 3053) *
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3054) * This is needed in the lookup routine of any filesystem that is exportable
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3055) * (via knfsd) so that we can build dcache paths to directories effectively.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3056) *
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3057) * If a dentry was found and moved, then it is returned. Otherwise NULL
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3058) * is returned. This matches the expected return value of ->lookup.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3059) *
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3060) * Cluster filesystems may call this function with a negative, hashed dentry.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3061) * In that case, we know that the inode will be a regular file, and also this
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3062) * will only occur during atomic_open. So we need to check for the dentry
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3063) * being already hashed only in the final case.
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3064) */
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3065) struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3066) {
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3067) if (IS_ERR(inode))
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3068) return ERR_CAST(inode);
3f70bd51cb440 (J. Bruce Fields 2014-02-18 14:11:26 -0500 3069)
770bfad846ab6 (David Howells 2006-08-22 20:06:07 -0400 3070) BUG_ON(!d_unhashed(dentry));
770bfad846ab6 (David Howells 2006-08-22 20:06:07 -0400 3071)
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 3072) if (!inode)
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3073) goto out;
de689f5e36637 (Al Viro 2016-03-09 18:05:42 -0500 3074)
b96809173e94e (Al Viro 2016-04-11 00:53:26 -0400 3075) security_d_instantiate(dentry, inode);
873feea09ebc9 (Nicholas Piggin 2011-01-07 17:50:06 +1100 3076) spin_lock(&inode->i_lock);
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3077) if (S_ISDIR(inode->i_mode)) {
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3078) struct dentry *new = __d_find_any_alias(inode);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3079) if (unlikely(new)) {
a03e283bf5c3d (Eric W. Biederman 2015-08-15 13:36:41 -0500 3080) /* The reference to new ensures it remains an alias */
a03e283bf5c3d (Eric W. Biederman 2015-08-15 13:36:41 -0500 3081) spin_unlock(&inode->i_lock);
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 3082) write_seqlock(&rename_lock);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3083) if (unlikely(d_ancestor(new, dentry))) {
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3084) write_sequnlock(&rename_lock);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3085) dput(new);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3086) new = ERR_PTR(-ELOOP);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3087) pr_warn_ratelimited(
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3088) "VFS: Lookup of '%s' in %s %s"
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3089) " would have caused loop\n",
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3090) dentry->d_name.name,
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3091) inode->i_sb->s_type->name,
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3092) inode->i_sb->s_id);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3093) } else if (!IS_ROOT(new)) {
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 3094) struct dentry *old_parent = dget(new->d_parent);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3095) int err = __d_unalias(inode, dentry, new);
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 3096) write_sequnlock(&rename_lock);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3097) if (err) {
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3098) dput(new);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3099) new = ERR_PTR(err);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3100) }
076515fc92679 (Al Viro 2018-03-10 23:15:52 -0500 3101) dput(old_parent);
1836750115f20 (Al Viro 2011-07-12 21:42:24 -0400 3102) } else {
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3103) __d_move(new, dentry, false);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3104) write_sequnlock(&rename_lock);
dd179946db249 (David Howells 2011-08-16 15:31:30 +0100 3105) }
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3106) iput(inode);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3107) return new;
9eaef27b36a6b (Trond Myklebust 2006-10-21 10:24:20 -0700 3108) }
770bfad846ab6 (David Howells 2006-08-22 20:06:07 -0400 3109) }
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3110) out:
ed782b5a70a01 (Al Viro 2016-03-09 19:52:39 -0500 3111) __d_add(dentry, inode);
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3112) return NULL;
770bfad846ab6 (David Howells 2006-08-22 20:06:07 -0400 3113) }
b5ae6b15bd73e (Al Viro 2014-10-12 22:16:02 -0400 3114) EXPORT_SYMBOL(d_splice_alias);
770bfad846ab6 (David Howells 2006-08-22 20:06:07 -0400 3115)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3116) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3117) * Test whether new_dentry is a subdirectory of old_dentry.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3118) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3119) * Trivially implemented using the dcache structure
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3120) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3121)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3122) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3123) * is_subdir - is new dentry a subdirectory of old_dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3124) * @new_dentry: new dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3125) * @old_dentry: old dentry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3126) *
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3127) * Returns true if new_dentry is a subdirectory of the parent (at any depth).
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3128) * Returns false otherwise.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3129) * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3130) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3131)
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3132) bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3133) {
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3134) bool result;
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3135) unsigned seq;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3136)
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3137) if (new_dentry == old_dentry)
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3138) return true;
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3139)
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3140) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3141) /* for restarting inner loop in case of seq retry */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3142) seq = read_seqbegin(&rename_lock);
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3143) /*
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3144) * Need rcu_readlock to protect against the d_parent trashing
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3145) * due to d_move
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3146) */
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3147) rcu_read_lock();
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3148) if (d_ancestor(old_dentry, new_dentry))
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3149) result = true;
e2761a1167633 (OGAWA Hirofumi 2008-10-16 07:50:28 +0900 3150) else
a6e5787fc8fc9 (Yaowei Bai 2015-11-17 14:40:11 +0800 3151) result = false;
949854d024550 (Nicholas Piggin 2011-01-07 17:49:37 +1100 3152) rcu_read_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3153) } while (read_seqretry(&rename_lock, seq));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3154)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3155) return result;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3156) }
e8f9e5b780b04 (Amir Goldstein 2018-01-11 11:33:24 +0200 3157) EXPORT_SYMBOL(is_subdir);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3158)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3159) static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3160) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3161) struct dentry *root = data;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3162) if (dentry != root) {
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3163) if (d_unhashed(dentry) || !dentry->d_inode)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3164) return D_WALK_SKIP;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3165)
01ddc4ede5f09 (Miklos Szeredi 2013-09-05 11:44:34 +0200 3166) if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
01ddc4ede5f09 (Miklos Szeredi 2013-09-05 11:44:34 +0200 3167) dentry->d_flags |= DCACHE_GENOCIDE;
01ddc4ede5f09 (Miklos Szeredi 2013-09-05 11:44:34 +0200 3168) dentry->d_lockref.count--;
01ddc4ede5f09 (Miklos Szeredi 2013-09-05 11:44:34 +0200 3169) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3170) }
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3171) return D_WALK_CONTINUE;
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3172) }
58db63d086790 (Nicholas Piggin 2011-01-07 17:49:39 +1100 3173)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3174) void d_genocide(struct dentry *parent)
db14fc3abcd5d (Miklos Szeredi 2013-09-05 11:44:35 +0200 3175) {
3a8e3611e0bab (Al Viro 2018-04-15 18:27:23 -0400 3176) d_walk(parent, parent, d_genocide_kill);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3177) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3178)
cbd4a5bcb25b5 (Al Viro 2018-03-29 15:08:21 -0400 3179) EXPORT_SYMBOL(d_genocide);
cbd4a5bcb25b5 (Al Viro 2018-03-29 15:08:21 -0400 3180)
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3181) void d_tmpfile(struct dentry *dentry, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3182) {
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3183) inode_dec_link_count(inode);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3184) BUG_ON(dentry->d_name.name != dentry->d_iname ||
946e51f2bf37f (Al Viro 2014-10-26 19:19:16 -0400 3185) !hlist_unhashed(&dentry->d_u.d_alias) ||
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3186) !d_unlinked(dentry));
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3187) spin_lock(&dentry->d_parent->d_lock);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3188) spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3189) dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3190) (unsigned long long)inode->i_ino);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3191) spin_unlock(&dentry->d_lock);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3192) spin_unlock(&dentry->d_parent->d_lock);
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3193) d_instantiate(dentry, inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3194) }
60545d0d4610b (Al Viro 2013-06-07 01:20:27 -0400 3195) EXPORT_SYMBOL(d_tmpfile);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3196)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3197) static __initdata unsigned long dhash_entries;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3198) static int __init set_dhash_entries(char *str)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3199) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3200) if (!str)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3201) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3202) dhash_entries = simple_strtoul(str, &str, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3203) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3204) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3205) __setup("dhash_entries=", set_dhash_entries);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3206)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3207) static void __init dcache_init_early(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3208) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3209) /* If hashes are distributed across NUMA nodes, defer
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3210) * hash allocation until vmalloc space is available.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3211) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3212) if (hashdist)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3213) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3214)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3215) dentry_hashtable =
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3216) alloc_large_system_hash("Dentry cache",
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 3217) sizeof(struct hlist_bl_head),
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3218) dhash_entries,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3219) 13,
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 3220) HASH_EARLY | HASH_ZERO,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3221) &d_hash_shift,
b35d786b67434 (Alexey Dobriyan 2017-11-20 18:05:52 +0300 3222) NULL,
31fe62b958664 (Tim Bird 2012-05-23 13:33:35 +0000 3223) 0,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3224) 0);
854d3e63438d7 (Alexey Dobriyan 2017-11-20 18:05:07 +0300 3225) d_hash_shift = 32 - d_hash_shift;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3226) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3227)
74bf17cffc325 (Denis Cheng 2007-10-16 23:26:30 -0700 3228) static void __init dcache_init(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3229) {
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 3230) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3231) * A constructor could be added for stable state like the lists,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3232) * but it is probably not worth it because of the cache nature
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 3233) * of the dcache.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3234) */
80344266c19b8 (David Windsor 2017-06-10 22:50:44 -0400 3235) dentry_cache = KMEM_CACHE_USERCOPY(dentry,
80344266c19b8 (David Windsor 2017-06-10 22:50:44 -0400 3236) SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
80344266c19b8 (David Windsor 2017-06-10 22:50:44 -0400 3237) d_iname);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3238)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3239) /* Hash may have been set up in dcache_init_early */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3240) if (!hashdist)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3241) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3242)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3243) dentry_hashtable =
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3244) alloc_large_system_hash("Dentry cache",
b07ad9967f40b (Linus Torvalds 2011-04-23 22:32:03 -0700 3245) sizeof(struct hlist_bl_head),
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3246) dhash_entries,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3247) 13,
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 3248) HASH_ZERO,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3249) &d_hash_shift,
b35d786b67434 (Alexey Dobriyan 2017-11-20 18:05:52 +0300 3250) NULL,
31fe62b958664 (Tim Bird 2012-05-23 13:33:35 +0000 3251) 0,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3252) 0);
854d3e63438d7 (Alexey Dobriyan 2017-11-20 18:05:07 +0300 3253) d_hash_shift = 32 - d_hash_shift;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3254) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3255)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3256) /* SLAB cache for __getname() consumers */
e18b890bb0881 (Christoph Lameter 2006-12-06 20:33:20 -0800 3257) struct kmem_cache *names_cachep __read_mostly;
ec4f860597af4 (H Hartley Sweeten 2010-01-05 13:45:18 -0700 3258) EXPORT_SYMBOL(names_cachep);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3259)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3260) void __init vfs_caches_init_early(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3261) {
6916363f30838 (Sebastian Andrzej Siewior 2017-06-27 18:19:11 +0200 3262) int i;
6916363f30838 (Sebastian Andrzej Siewior 2017-06-27 18:19:11 +0200 3263)
6916363f30838 (Sebastian Andrzej Siewior 2017-06-27 18:19:11 +0200 3264) for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
6916363f30838 (Sebastian Andrzej Siewior 2017-06-27 18:19:11 +0200 3265) INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
6916363f30838 (Sebastian Andrzej Siewior 2017-06-27 18:19:11 +0200 3266)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3267) dcache_init_early();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3268) inode_init_early();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3269) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3270)
4248b0da46083 (Mel Gorman 2015-08-06 15:46:20 -0700 3271) void __init vfs_caches_init(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3272) {
6a9b88204cb00 (David Windsor 2017-06-10 22:50:30 -0400 3273) names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
6a9b88204cb00 (David Windsor 2017-06-10 22:50:30 -0400 3274) SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3275)
74bf17cffc325 (Denis Cheng 2007-10-16 23:26:30 -0700 3276) dcache_init();
74bf17cffc325 (Denis Cheng 2007-10-16 23:26:30 -0700 3277) inode_init();
4248b0da46083 (Mel Gorman 2015-08-06 15:46:20 -0700 3278) files_init();
4248b0da46083 (Mel Gorman 2015-08-06 15:46:20 -0700 3279) files_maxfiles_init();
74bf17cffc325 (Denis Cheng 2007-10-16 23:26:30 -0700 3280) mnt_init();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3281) bdev_cache_init();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3282) chrdev_init();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3283) }