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