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