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