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