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