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