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