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