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