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