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