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