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 write_seqlock(&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 write_sequnlock(&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 = NULL; 1735 dentry->d_fsdata = NULL; 1736 INIT_HLIST_BL_NODE(&dentry->d_hash); 1737 INIT_LIST_HEAD(&dentry->d_lru); 1738 INIT_HLIST_HEAD(&dentry->d_children); 1739 INIT_HLIST_NODE(&dentry->d_u.d_alias); 1740 INIT_HLIST_NODE(&dentry->d_sib); 1741 d_set_d_op(dentry, dentry->d_sb->s_d_op); 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 if (!sb->s_d_op) 1825 d_set_d_op(dentry, &anon_ops); 1826 } 1827 return dentry; 1828 } 1829 1830 struct dentry *d_alloc_name(struct dentry *parent, const char *name) 1831 { 1832 struct qstr q; 1833 1834 q.name = name; 1835 q.hash_len = hashlen_string(parent, name); 1836 return d_alloc(parent, &q); 1837 } 1838 EXPORT_SYMBOL(d_alloc_name); 1839 1840 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op) 1841 { 1842 WARN_ON_ONCE(dentry->d_op); 1843 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH | 1844 DCACHE_OP_COMPARE | 1845 DCACHE_OP_REVALIDATE | 1846 DCACHE_OP_WEAK_REVALIDATE | 1847 DCACHE_OP_DELETE | 1848 DCACHE_OP_REAL)); 1849 dentry->d_op = op; 1850 if (!op) 1851 return; 1852 if (op->d_hash) 1853 dentry->d_flags |= DCACHE_OP_HASH; 1854 if (op->d_compare) 1855 dentry->d_flags |= DCACHE_OP_COMPARE; 1856 if (op->d_revalidate) 1857 dentry->d_flags |= DCACHE_OP_REVALIDATE; 1858 if (op->d_weak_revalidate) 1859 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE; 1860 if (op->d_delete) 1861 dentry->d_flags |= DCACHE_OP_DELETE; 1862 if (op->d_prune) 1863 dentry->d_flags |= DCACHE_OP_PRUNE; 1864 if (op->d_real) 1865 dentry->d_flags |= DCACHE_OP_REAL; 1866 1867 } 1868 EXPORT_SYMBOL(d_set_d_op); 1869 1870 static unsigned d_flags_for_inode(struct inode *inode) 1871 { 1872 unsigned add_flags = DCACHE_REGULAR_TYPE; 1873 1874 if (!inode) 1875 return DCACHE_MISS_TYPE; 1876 1877 if (S_ISDIR(inode->i_mode)) { 1878 add_flags = DCACHE_DIRECTORY_TYPE; 1879 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) { 1880 if (unlikely(!inode->i_op->lookup)) 1881 add_flags = DCACHE_AUTODIR_TYPE; 1882 else 1883 inode->i_opflags |= IOP_LOOKUP; 1884 } 1885 goto type_determined; 1886 } 1887 1888 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 1889 if (unlikely(inode->i_op->get_link)) { 1890 add_flags = DCACHE_SYMLINK_TYPE; 1891 goto type_determined; 1892 } 1893 inode->i_opflags |= IOP_NOFOLLOW; 1894 } 1895 1896 if (unlikely(!S_ISREG(inode->i_mode))) 1897 add_flags = DCACHE_SPECIAL_TYPE; 1898 1899 type_determined: 1900 if (unlikely(IS_AUTOMOUNT(inode))) 1901 add_flags |= DCACHE_NEED_AUTOMOUNT; 1902 return add_flags; 1903 } 1904 1905 static void __d_instantiate(struct dentry *dentry, struct inode *inode) 1906 { 1907 unsigned add_flags = d_flags_for_inode(inode); 1908 WARN_ON(d_in_lookup(dentry)); 1909 1910 spin_lock(&dentry->d_lock); 1911 /* 1912 * The negative counter only tracks dentries on the LRU. Don't dec if 1913 * d_lru is on another list. 1914 */ 1915 if ((dentry->d_flags & 1916 (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST) 1917 this_cpu_dec(nr_dentry_negative); 1918 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); 1919 raw_write_seqcount_begin(&dentry->d_seq); 1920 __d_set_inode_and_type(dentry, inode, add_flags); 1921 raw_write_seqcount_end(&dentry->d_seq); 1922 fsnotify_update_flags(dentry); 1923 spin_unlock(&dentry->d_lock); 1924 } 1925 1926 /** 1927 * d_instantiate - fill in inode information for a dentry 1928 * @entry: dentry to complete 1929 * @inode: inode to attach to this dentry 1930 * 1931 * Fill in inode information in the entry. 1932 * 1933 * This turns negative dentries into productive full members 1934 * of society. 1935 * 1936 * NOTE! This assumes that the inode count has been incremented 1937 * (or otherwise set) by the caller to indicate that it is now 1938 * in use by the dcache. 1939 */ 1940 1941 void d_instantiate(struct dentry *entry, struct inode * inode) 1942 { 1943 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); 1944 if (inode) { 1945 security_d_instantiate(entry, inode); 1946 spin_lock(&inode->i_lock); 1947 __d_instantiate(entry, inode); 1948 spin_unlock(&inode->i_lock); 1949 } 1950 } 1951 EXPORT_SYMBOL(d_instantiate); 1952 1953 /* 1954 * This should be equivalent to d_instantiate() + unlock_new_inode(), 1955 * with lockdep-related part of unlock_new_inode() done before 1956 * anything else. Use that instead of open-coding d_instantiate()/ 1957 * unlock_new_inode() combinations. 1958 */ 1959 void d_instantiate_new(struct dentry *entry, struct inode *inode) 1960 { 1961 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); 1962 BUG_ON(!inode); 1963 lockdep_annotate_inode_mutex_key(inode); 1964 security_d_instantiate(entry, inode); 1965 spin_lock(&inode->i_lock); 1966 __d_instantiate(entry, inode); 1967 WARN_ON(!(inode->i_state & I_NEW)); 1968 inode->i_state &= ~I_NEW & ~I_CREATING; 1969 /* 1970 * Pairs with the barrier in prepare_to_wait_event() to make sure 1971 * ___wait_var_event() either sees the bit cleared or 1972 * waitqueue_active() check in wake_up_var() sees the waiter. 1973 */ 1974 smp_mb(); 1975 inode_wake_up_bit(inode, __I_NEW); 1976 spin_unlock(&inode->i_lock); 1977 } 1978 EXPORT_SYMBOL(d_instantiate_new); 1979 1980 struct dentry *d_make_root(struct inode *root_inode) 1981 { 1982 struct dentry *res = NULL; 1983 1984 if (root_inode) { 1985 res = d_alloc_anon(root_inode->i_sb); 1986 if (res) 1987 d_instantiate(res, root_inode); 1988 else 1989 iput(root_inode); 1990 } 1991 return res; 1992 } 1993 EXPORT_SYMBOL(d_make_root); 1994 1995 static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected) 1996 { 1997 struct super_block *sb; 1998 struct dentry *new, *res; 1999 2000 if (!inode) 2001 return ERR_PTR(-ESTALE); 2002 if (IS_ERR(inode)) 2003 return ERR_CAST(inode); 2004 2005 sb = inode->i_sb; 2006 2007 res = d_find_any_alias(inode); /* existing alias? */ 2008 if (res) 2009 goto out; 2010 2011 new = d_alloc_anon(sb); 2012 if (!new) { 2013 res = ERR_PTR(-ENOMEM); 2014 goto out; 2015 } 2016 2017 security_d_instantiate(new, inode); 2018 spin_lock(&inode->i_lock); 2019 res = __d_find_any_alias(inode); /* recheck under lock */ 2020 if (likely(!res)) { /* still no alias, attach a disconnected dentry */ 2021 unsigned add_flags = d_flags_for_inode(inode); 2022 2023 if (disconnected) 2024 add_flags |= DCACHE_DISCONNECTED; 2025 2026 spin_lock(&new->d_lock); 2027 __d_set_inode_and_type(new, inode, add_flags); 2028 hlist_add_head(&new->d_u.d_alias, &inode->i_dentry); 2029 if (!disconnected) { 2030 hlist_bl_lock(&sb->s_roots); 2031 hlist_bl_add_head(&new->d_hash, &sb->s_roots); 2032 hlist_bl_unlock(&sb->s_roots); 2033 } 2034 spin_unlock(&new->d_lock); 2035 spin_unlock(&inode->i_lock); 2036 inode = NULL; /* consumed by new->d_inode */ 2037 res = new; 2038 } else { 2039 spin_unlock(&inode->i_lock); 2040 dput(new); 2041 } 2042 2043 out: 2044 iput(inode); 2045 return res; 2046 } 2047 2048 /** 2049 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode 2050 * @inode: inode to allocate the dentry for 2051 * 2052 * Obtain a dentry for an inode resulting from NFS filehandle conversion or 2053 * similar open by handle operations. The returned dentry may be anonymous, 2054 * or may have a full name (if the inode was already in the cache). 2055 * 2056 * When called on a directory inode, we must ensure that the inode only ever 2057 * has one dentry. If a dentry is found, that is returned instead of 2058 * allocating a new one. 2059 * 2060 * On successful return, the reference to the inode has been transferred 2061 * to the dentry. In case of an error the reference on the inode is released. 2062 * To make it easier to use in export operations a %NULL or IS_ERR inode may 2063 * be passed in and the error will be propagated to the return value, 2064 * with a %NULL @inode replaced by ERR_PTR(-ESTALE). 2065 */ 2066 struct dentry *d_obtain_alias(struct inode *inode) 2067 { 2068 return __d_obtain_alias(inode, true); 2069 } 2070 EXPORT_SYMBOL(d_obtain_alias); 2071 2072 /** 2073 * d_obtain_root - find or allocate a dentry for a given inode 2074 * @inode: inode to allocate the dentry for 2075 * 2076 * Obtain an IS_ROOT dentry for the root of a filesystem. 2077 * 2078 * We must ensure that directory inodes only ever have one dentry. If a 2079 * dentry is found, that is returned instead of allocating a new one. 2080 * 2081 * On successful return, the reference to the inode has been transferred 2082 * to the dentry. In case of an error the reference on the inode is 2083 * released. A %NULL or IS_ERR inode may be passed in and will be the 2084 * error will be propagate to the return value, with a %NULL @inode 2085 * replaced by ERR_PTR(-ESTALE). 2086 */ 2087 struct dentry *d_obtain_root(struct inode *inode) 2088 { 2089 return __d_obtain_alias(inode, false); 2090 } 2091 EXPORT_SYMBOL(d_obtain_root); 2092 2093 /** 2094 * d_add_ci - lookup or allocate new dentry with case-exact name 2095 * @dentry: the negative dentry that was passed to the parent's lookup func 2096 * @inode: the inode case-insensitive lookup has found 2097 * @name: the case-exact name to be associated with the returned dentry 2098 * 2099 * This is to avoid filling the dcache with case-insensitive names to the 2100 * same inode, only the actual correct case is stored in the dcache for 2101 * case-insensitive filesystems. 2102 * 2103 * For a case-insensitive lookup match and if the case-exact dentry 2104 * already exists in the dcache, use it and return it. 2105 * 2106 * If no entry exists with the exact case name, allocate new dentry with 2107 * the exact case, and return the spliced entry. 2108 */ 2109 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, 2110 struct qstr *name) 2111 { 2112 struct dentry *found, *res; 2113 2114 /* 2115 * First check if a dentry matching the name already exists, 2116 * if not go ahead and create it now. 2117 */ 2118 found = d_hash_and_lookup(dentry->d_parent, name); 2119 if (found) { 2120 iput(inode); 2121 return found; 2122 } 2123 if (d_in_lookup(dentry)) { 2124 found = d_alloc_parallel(dentry->d_parent, name, 2125 dentry->d_wait); 2126 if (IS_ERR(found) || !d_in_lookup(found)) { 2127 iput(inode); 2128 return found; 2129 } 2130 } else { 2131 found = d_alloc(dentry->d_parent, name); 2132 if (!found) { 2133 iput(inode); 2134 return ERR_PTR(-ENOMEM); 2135 } 2136 } 2137 res = d_splice_alias(inode, found); 2138 if (res) { 2139 d_lookup_done(found); 2140 dput(found); 2141 return res; 2142 } 2143 return found; 2144 } 2145 EXPORT_SYMBOL(d_add_ci); 2146 2147 /** 2148 * d_same_name - compare dentry name with case-exact name 2149 * @dentry: the negative dentry that was passed to the parent's lookup func 2150 * @parent: parent dentry 2151 * @name: the case-exact name to be associated with the returned dentry 2152 * 2153 * Return: true if names are same, or false 2154 */ 2155 bool d_same_name(const struct dentry *dentry, const struct dentry *parent, 2156 const struct qstr *name) 2157 { 2158 if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) { 2159 if (dentry->d_name.len != name->len) 2160 return false; 2161 return dentry_cmp(dentry, name->name, name->len) == 0; 2162 } 2163 return parent->d_op->d_compare(dentry, 2164 dentry->d_name.len, dentry->d_name.name, 2165 name) == 0; 2166 } 2167 EXPORT_SYMBOL_GPL(d_same_name); 2168 2169 /* 2170 * This is __d_lookup_rcu() when the parent dentry has 2171 * DCACHE_OP_COMPARE, which makes things much nastier. 2172 */ 2173 static noinline struct dentry *__d_lookup_rcu_op_compare( 2174 const struct dentry *parent, 2175 const struct qstr *name, 2176 unsigned *seqp) 2177 { 2178 u64 hashlen = name->hash_len; 2179 struct hlist_bl_head *b = d_hash(hashlen); 2180 struct hlist_bl_node *node; 2181 struct dentry *dentry; 2182 2183 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { 2184 int tlen; 2185 const char *tname; 2186 unsigned seq; 2187 2188 seqretry: 2189 seq = raw_seqcount_begin(&dentry->d_seq); 2190 if (dentry->d_parent != parent) 2191 continue; 2192 if (d_unhashed(dentry)) 2193 continue; 2194 if (dentry->d_name.hash != hashlen_hash(hashlen)) 2195 continue; 2196 tlen = dentry->d_name.len; 2197 tname = dentry->d_name.name; 2198 /* we want a consistent (name,len) pair */ 2199 if (read_seqcount_retry(&dentry->d_seq, seq)) { 2200 cpu_relax(); 2201 goto seqretry; 2202 } 2203 if (parent->d_op->d_compare(dentry, tlen, tname, name) != 0) 2204 continue; 2205 *seqp = seq; 2206 return dentry; 2207 } 2208 return NULL; 2209 } 2210 2211 /** 2212 * __d_lookup_rcu - search for a dentry (racy, store-free) 2213 * @parent: parent dentry 2214 * @name: qstr of name we wish to find 2215 * @seqp: returns d_seq value at the point where the dentry was found 2216 * Returns: dentry, or NULL 2217 * 2218 * __d_lookup_rcu is the dcache lookup function for rcu-walk name 2219 * resolution (store-free path walking) design described in 2220 * Documentation/filesystems/path-lookup.txt. 2221 * 2222 * This is not to be used outside core vfs. 2223 * 2224 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock 2225 * held, and rcu_read_lock held. The returned dentry must not be stored into 2226 * without taking d_lock and checking d_seq sequence count against @seq 2227 * returned here. 2228 * 2229 * Alternatively, __d_lookup_rcu may be called again to look up the child of 2230 * the returned dentry, so long as its parent's seqlock is checked after the 2231 * child is looked up. Thus, an interlocking stepping of sequence lock checks 2232 * is formed, giving integrity down the path walk. 2233 * 2234 * NOTE! The caller *has* to check the resulting dentry against the sequence 2235 * number we've returned before using any of the resulting dentry state! 2236 */ 2237 struct dentry *__d_lookup_rcu(const struct dentry *parent, 2238 const struct qstr *name, 2239 unsigned *seqp) 2240 { 2241 u64 hashlen = name->hash_len; 2242 const unsigned char *str = name->name; 2243 struct hlist_bl_head *b = d_hash(hashlen); 2244 struct hlist_bl_node *node; 2245 struct dentry *dentry; 2246 2247 /* 2248 * Note: There is significant duplication with __d_lookup_rcu which is 2249 * required to prevent single threaded performance regressions 2250 * especially on architectures where smp_rmb (in seqcounts) are costly. 2251 * Keep the two functions in sync. 2252 */ 2253 2254 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) 2255 return __d_lookup_rcu_op_compare(parent, name, seqp); 2256 2257 /* 2258 * The hash list is protected using RCU. 2259 * 2260 * Carefully use d_seq when comparing a candidate dentry, to avoid 2261 * races with d_move(). 2262 * 2263 * It is possible that concurrent renames can mess up our list 2264 * walk here and result in missing our dentry, resulting in the 2265 * false-negative result. d_lookup() protects against concurrent 2266 * renames using rename_lock seqlock. 2267 * 2268 * See Documentation/filesystems/path-lookup.txt for more details. 2269 */ 2270 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { 2271 unsigned seq; 2272 2273 /* 2274 * The dentry sequence count protects us from concurrent 2275 * renames, and thus protects parent and name fields. 2276 * 2277 * The caller must perform a seqcount check in order 2278 * to do anything useful with the returned dentry. 2279 * 2280 * NOTE! We do a "raw" seqcount_begin here. That means that 2281 * we don't wait for the sequence count to stabilize if it 2282 * is in the middle of a sequence change. If we do the slow 2283 * dentry compare, we will do seqretries until it is stable, 2284 * and if we end up with a successful lookup, we actually 2285 * want to exit RCU lookup anyway. 2286 * 2287 * Note that raw_seqcount_begin still *does* smp_rmb(), so 2288 * we are still guaranteed NUL-termination of ->d_name.name. 2289 */ 2290 seq = raw_seqcount_begin(&dentry->d_seq); 2291 if (dentry->d_parent != parent) 2292 continue; 2293 if (d_unhashed(dentry)) 2294 continue; 2295 if (dentry->d_name.hash_len != hashlen) 2296 continue; 2297 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0) 2298 continue; 2299 *seqp = seq; 2300 return dentry; 2301 } 2302 return NULL; 2303 } 2304 2305 /** 2306 * d_lookup - search for a dentry 2307 * @parent: parent dentry 2308 * @name: qstr of name we wish to find 2309 * Returns: dentry, or NULL 2310 * 2311 * d_lookup searches the children of the parent dentry for the name in 2312 * question. If the dentry is found its reference count is incremented and the 2313 * dentry is returned. The caller must use dput to free the entry when it has 2314 * finished using it. %NULL is returned if the dentry does not exist. 2315 */ 2316 struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name) 2317 { 2318 struct dentry *dentry; 2319 unsigned seq; 2320 2321 do { 2322 seq = read_seqbegin(&rename_lock); 2323 dentry = __d_lookup(parent, name); 2324 if (dentry) 2325 break; 2326 } while (read_seqretry(&rename_lock, seq)); 2327 return dentry; 2328 } 2329 EXPORT_SYMBOL(d_lookup); 2330 2331 /** 2332 * __d_lookup - search for a dentry (racy) 2333 * @parent: parent dentry 2334 * @name: qstr of name we wish to find 2335 * Returns: dentry, or NULL 2336 * 2337 * __d_lookup is like d_lookup, however it may (rarely) return a 2338 * false-negative result due to unrelated rename activity. 2339 * 2340 * __d_lookup is slightly faster by avoiding rename_lock read seqlock, 2341 * however it must be used carefully, eg. with a following d_lookup in 2342 * the case of failure. 2343 * 2344 * __d_lookup callers must be commented. 2345 */ 2346 struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name) 2347 { 2348 unsigned int hash = name->hash; 2349 struct hlist_bl_head *b = d_hash(hash); 2350 struct hlist_bl_node *node; 2351 struct dentry *found = NULL; 2352 struct dentry *dentry; 2353 2354 /* 2355 * Note: There is significant duplication with __d_lookup_rcu which is 2356 * required to prevent single threaded performance regressions 2357 * especially on architectures where smp_rmb (in seqcounts) are costly. 2358 * Keep the two functions in sync. 2359 */ 2360 2361 /* 2362 * The hash list is protected using RCU. 2363 * 2364 * Take d_lock when comparing a candidate dentry, to avoid races 2365 * with d_move(). 2366 * 2367 * It is possible that concurrent renames can mess up our list 2368 * walk here and result in missing our dentry, resulting in the 2369 * false-negative result. d_lookup() protects against concurrent 2370 * renames using rename_lock seqlock. 2371 * 2372 * See Documentation/filesystems/path-lookup.txt for more details. 2373 */ 2374 rcu_read_lock(); 2375 2376 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { 2377 2378 if (dentry->d_name.hash != hash) 2379 continue; 2380 2381 spin_lock(&dentry->d_lock); 2382 if (dentry->d_parent != parent) 2383 goto next; 2384 if (d_unhashed(dentry)) 2385 goto next; 2386 2387 if (!d_same_name(dentry, parent, name)) 2388 goto next; 2389 2390 dentry->d_lockref.count++; 2391 found = dentry; 2392 spin_unlock(&dentry->d_lock); 2393 break; 2394 next: 2395 spin_unlock(&dentry->d_lock); 2396 } 2397 rcu_read_unlock(); 2398 2399 return found; 2400 } 2401 2402 /** 2403 * d_hash_and_lookup - hash the qstr then search for a dentry 2404 * @dir: Directory to search in 2405 * @name: qstr of name we wish to find 2406 * 2407 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error) 2408 */ 2409 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) 2410 { 2411 /* 2412 * Check for a fs-specific hash function. Note that we must 2413 * calculate the standard hash first, as the d_op->d_hash() 2414 * routine may choose to leave the hash value unchanged. 2415 */ 2416 name->hash = full_name_hash(dir, name->name, name->len); 2417 if (dir->d_flags & DCACHE_OP_HASH) { 2418 int err = dir->d_op->d_hash(dir, name); 2419 if (unlikely(err < 0)) 2420 return ERR_PTR(err); 2421 } 2422 return d_lookup(dir, name); 2423 } 2424 2425 /* 2426 * When a file is deleted, we have two options: 2427 * - turn this dentry into a negative dentry 2428 * - unhash this dentry and free it. 2429 * 2430 * Usually, we want to just turn this into 2431 * a negative dentry, but if anybody else is 2432 * currently using the dentry or the inode 2433 * we can't do that and we fall back on removing 2434 * it from the hash queues and waiting for 2435 * it to be deleted later when it has no users 2436 */ 2437 2438 /** 2439 * d_delete - delete a dentry 2440 * @dentry: The dentry to delete 2441 * 2442 * Turn the dentry into a negative dentry if possible, otherwise 2443 * remove it from the hash queues so it can be deleted later 2444 */ 2445 2446 void d_delete(struct dentry * dentry) 2447 { 2448 struct inode *inode = dentry->d_inode; 2449 2450 spin_lock(&inode->i_lock); 2451 spin_lock(&dentry->d_lock); 2452 /* 2453 * Are we the only user? 2454 */ 2455 if (dentry->d_lockref.count == 1) { 2456 if (dentry_negative_policy) 2457 __d_drop(dentry); 2458 dentry->d_flags &= ~DCACHE_CANT_MOUNT; 2459 dentry_unlink_inode(dentry); 2460 } else { 2461 __d_drop(dentry); 2462 spin_unlock(&dentry->d_lock); 2463 spin_unlock(&inode->i_lock); 2464 } 2465 } 2466 EXPORT_SYMBOL(d_delete); 2467 2468 static void __d_rehash(struct dentry *entry) 2469 { 2470 struct hlist_bl_head *b = d_hash(entry->d_name.hash); 2471 2472 hlist_bl_lock(b); 2473 hlist_bl_add_head_rcu(&entry->d_hash, b); 2474 hlist_bl_unlock(b); 2475 } 2476 2477 /** 2478 * d_rehash - add an entry back to the hash 2479 * @entry: dentry to add to the hash 2480 * 2481 * Adds a dentry to the hash according to its name. 2482 */ 2483 2484 void d_rehash(struct dentry * entry) 2485 { 2486 spin_lock(&entry->d_lock); 2487 __d_rehash(entry); 2488 spin_unlock(&entry->d_lock); 2489 } 2490 EXPORT_SYMBOL(d_rehash); 2491 2492 static inline unsigned start_dir_add(struct inode *dir) 2493 { 2494 preempt_disable_nested(); 2495 for (;;) { 2496 unsigned n = dir->i_dir_seq; 2497 if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n) 2498 return n; 2499 cpu_relax(); 2500 } 2501 } 2502 2503 static inline void end_dir_add(struct inode *dir, unsigned int n, 2504 wait_queue_head_t *d_wait) 2505 { 2506 smp_store_release(&dir->i_dir_seq, n + 2); 2507 preempt_enable_nested(); 2508 if (wq_has_sleeper(d_wait)) 2509 wake_up_all(d_wait); 2510 } 2511 2512 static void d_wait_lookup(struct dentry *dentry) 2513 { 2514 if (d_in_lookup(dentry)) { 2515 DECLARE_WAITQUEUE(wait, current); 2516 add_wait_queue(dentry->d_wait, &wait); 2517 do { 2518 set_current_state(TASK_UNINTERRUPTIBLE); 2519 spin_unlock(&dentry->d_lock); 2520 schedule(); 2521 spin_lock(&dentry->d_lock); 2522 } while (d_in_lookup(dentry)); 2523 } 2524 } 2525 2526 struct dentry *d_alloc_parallel(struct dentry *parent, 2527 const struct qstr *name, 2528 wait_queue_head_t *wq) 2529 { 2530 unsigned int hash = name->hash; 2531 struct hlist_bl_head *b = in_lookup_hash(parent, hash); 2532 struct hlist_bl_node *node; 2533 struct dentry *new = d_alloc(parent, name); 2534 struct dentry *dentry; 2535 unsigned seq, r_seq, d_seq; 2536 2537 if (unlikely(!new)) 2538 return ERR_PTR(-ENOMEM); 2539 2540 retry: 2541 rcu_read_lock(); 2542 seq = smp_load_acquire(&parent->d_inode->i_dir_seq); 2543 r_seq = read_seqbegin(&rename_lock); 2544 dentry = __d_lookup_rcu(parent, name, &d_seq); 2545 if (unlikely(dentry)) { 2546 if (!lockref_get_not_dead(&dentry->d_lockref)) { 2547 rcu_read_unlock(); 2548 goto retry; 2549 } 2550 if (read_seqcount_retry(&dentry->d_seq, d_seq)) { 2551 rcu_read_unlock(); 2552 dput(dentry); 2553 goto retry; 2554 } 2555 rcu_read_unlock(); 2556 dput(new); 2557 return dentry; 2558 } 2559 if (unlikely(read_seqretry(&rename_lock, r_seq))) { 2560 rcu_read_unlock(); 2561 goto retry; 2562 } 2563 2564 if (unlikely(seq & 1)) { 2565 rcu_read_unlock(); 2566 goto retry; 2567 } 2568 2569 hlist_bl_lock(b); 2570 if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) { 2571 hlist_bl_unlock(b); 2572 rcu_read_unlock(); 2573 goto retry; 2574 } 2575 /* 2576 * No changes for the parent since the beginning of d_lookup(). 2577 * Since all removals from the chain happen with hlist_bl_lock(), 2578 * any potential in-lookup matches are going to stay here until 2579 * we unlock the chain. All fields are stable in everything 2580 * we encounter. 2581 */ 2582 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) { 2583 if (dentry->d_name.hash != hash) 2584 continue; 2585 if (dentry->d_parent != parent) 2586 continue; 2587 if (!d_same_name(dentry, parent, name)) 2588 continue; 2589 hlist_bl_unlock(b); 2590 /* now we can try to grab a reference */ 2591 if (!lockref_get_not_dead(&dentry->d_lockref)) { 2592 rcu_read_unlock(); 2593 goto retry; 2594 } 2595 2596 rcu_read_unlock(); 2597 /* 2598 * somebody is likely to be still doing lookup for it; 2599 * wait for them to finish 2600 */ 2601 spin_lock(&dentry->d_lock); 2602 d_wait_lookup(dentry); 2603 /* 2604 * it's not in-lookup anymore; in principle we should repeat 2605 * everything from dcache lookup, but it's likely to be what 2606 * d_lookup() would've found anyway. If it is, just return it; 2607 * otherwise we really have to repeat the whole thing. 2608 */ 2609 if (unlikely(dentry->d_name.hash != hash)) 2610 goto mismatch; 2611 if (unlikely(dentry->d_parent != parent)) 2612 goto mismatch; 2613 if (unlikely(d_unhashed(dentry))) 2614 goto mismatch; 2615 if (unlikely(!d_same_name(dentry, parent, name))) 2616 goto mismatch; 2617 /* OK, it *is* a hashed match; return it */ 2618 spin_unlock(&dentry->d_lock); 2619 dput(new); 2620 return dentry; 2621 } 2622 rcu_read_unlock(); 2623 /* we can't take ->d_lock here; it's OK, though. */ 2624 new->d_flags |= DCACHE_PAR_LOOKUP; 2625 new->d_wait = wq; 2626 hlist_bl_add_head(&new->d_u.d_in_lookup_hash, b); 2627 hlist_bl_unlock(b); 2628 return new; 2629 mismatch: 2630 spin_unlock(&dentry->d_lock); 2631 dput(dentry); 2632 goto retry; 2633 } 2634 EXPORT_SYMBOL(d_alloc_parallel); 2635 2636 /* 2637 * - Unhash the dentry 2638 * - Retrieve and clear the waitqueue head in dentry 2639 * - Return the waitqueue head 2640 */ 2641 static wait_queue_head_t *__d_lookup_unhash(struct dentry *dentry) 2642 { 2643 wait_queue_head_t *d_wait; 2644 struct hlist_bl_head *b; 2645 2646 lockdep_assert_held(&dentry->d_lock); 2647 2648 b = in_lookup_hash(dentry->d_parent, dentry->d_name.hash); 2649 hlist_bl_lock(b); 2650 dentry->d_flags &= ~DCACHE_PAR_LOOKUP; 2651 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash); 2652 d_wait = dentry->d_wait; 2653 dentry->d_wait = NULL; 2654 hlist_bl_unlock(b); 2655 INIT_HLIST_NODE(&dentry->d_u.d_alias); 2656 INIT_LIST_HEAD(&dentry->d_lru); 2657 return d_wait; 2658 } 2659 2660 void __d_lookup_unhash_wake(struct dentry *dentry) 2661 { 2662 spin_lock(&dentry->d_lock); 2663 wake_up_all(__d_lookup_unhash(dentry)); 2664 spin_unlock(&dentry->d_lock); 2665 } 2666 EXPORT_SYMBOL(__d_lookup_unhash_wake); 2667 2668 /* inode->i_lock held if inode is non-NULL */ 2669 2670 static inline void __d_add(struct dentry *dentry, struct inode *inode) 2671 { 2672 wait_queue_head_t *d_wait; 2673 struct inode *dir = NULL; 2674 unsigned n; 2675 spin_lock(&dentry->d_lock); 2676 if (unlikely(d_in_lookup(dentry))) { 2677 dir = dentry->d_parent->d_inode; 2678 n = start_dir_add(dir); 2679 d_wait = __d_lookup_unhash(dentry); 2680 } 2681 if (inode) { 2682 unsigned add_flags = d_flags_for_inode(inode); 2683 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); 2684 raw_write_seqcount_begin(&dentry->d_seq); 2685 __d_set_inode_and_type(dentry, inode, add_flags); 2686 raw_write_seqcount_end(&dentry->d_seq); 2687 fsnotify_update_flags(dentry); 2688 } 2689 __d_rehash(dentry); 2690 if (dir) 2691 end_dir_add(dir, n, d_wait); 2692 spin_unlock(&dentry->d_lock); 2693 if (inode) 2694 spin_unlock(&inode->i_lock); 2695 } 2696 2697 /** 2698 * d_add - add dentry to hash queues 2699 * @entry: dentry to add 2700 * @inode: The inode to attach to this dentry 2701 * 2702 * This adds the entry to the hash queues and initializes @inode. 2703 * The entry was actually filled in earlier during d_alloc(). 2704 */ 2705 2706 void d_add(struct dentry *entry, struct inode *inode) 2707 { 2708 if (inode) { 2709 security_d_instantiate(entry, inode); 2710 spin_lock(&inode->i_lock); 2711 } 2712 __d_add(entry, inode); 2713 } 2714 EXPORT_SYMBOL(d_add); 2715 2716 static void swap_names(struct dentry *dentry, struct dentry *target) 2717 { 2718 if (unlikely(dname_external(target))) { 2719 if (unlikely(dname_external(dentry))) { 2720 /* 2721 * Both external: swap the pointers 2722 */ 2723 swap(target->d_name.name, dentry->d_name.name); 2724 } else { 2725 /* 2726 * dentry:internal, target:external. Steal target's 2727 * storage and make target internal. 2728 */ 2729 dentry->d_name.name = target->d_name.name; 2730 target->d_shortname = dentry->d_shortname; 2731 target->d_name.name = target->d_shortname.string; 2732 } 2733 } else { 2734 if (unlikely(dname_external(dentry))) { 2735 /* 2736 * dentry:external, target:internal. Give dentry's 2737 * storage to target and make dentry internal 2738 */ 2739 target->d_name.name = dentry->d_name.name; 2740 dentry->d_shortname = target->d_shortname; 2741 dentry->d_name.name = dentry->d_shortname.string; 2742 } else { 2743 /* 2744 * Both are internal. 2745 */ 2746 for (int i = 0; i < DNAME_INLINE_WORDS; i++) 2747 swap(dentry->d_shortname.words[i], 2748 target->d_shortname.words[i]); 2749 } 2750 } 2751 swap(dentry->d_name.hash_len, target->d_name.hash_len); 2752 } 2753 2754 static void copy_name(struct dentry *dentry, struct dentry *target) 2755 { 2756 struct external_name *old_name = NULL; 2757 if (unlikely(dname_external(dentry))) 2758 old_name = external_name(dentry); 2759 if (unlikely(dname_external(target))) { 2760 atomic_inc(&external_name(target)->count); 2761 dentry->d_name = target->d_name; 2762 } else { 2763 dentry->d_shortname = target->d_shortname; 2764 dentry->d_name.name = dentry->d_shortname.string; 2765 dentry->d_name.hash_len = target->d_name.hash_len; 2766 } 2767 if (old_name && likely(atomic_dec_and_test(&old_name->count))) 2768 kfree_rcu(old_name, head); 2769 } 2770 2771 /* 2772 * __d_move - move a dentry 2773 * @dentry: entry to move 2774 * @target: new dentry 2775 * @exchange: exchange the two dentries 2776 * 2777 * Update the dcache to reflect the move of a file name. Negative 2778 * dcache entries should not be moved in this way. Caller must hold 2779 * rename_lock, the i_mutex of the source and target directories, 2780 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename(). 2781 */ 2782 static void __d_move(struct dentry *dentry, struct dentry *target, 2783 bool exchange) 2784 { 2785 struct dentry *old_parent, *p; 2786 wait_queue_head_t *d_wait; 2787 struct inode *dir = NULL; 2788 unsigned n; 2789 2790 WARN_ON(!dentry->d_inode); 2791 if (WARN_ON(dentry == target)) 2792 return; 2793 2794 BUG_ON(d_ancestor(target, dentry)); 2795 old_parent = dentry->d_parent; 2796 p = d_ancestor(old_parent, target); 2797 if (IS_ROOT(dentry)) { 2798 BUG_ON(p); 2799 spin_lock(&target->d_parent->d_lock); 2800 } else if (!p) { 2801 /* target is not a descendent of dentry->d_parent */ 2802 spin_lock(&target->d_parent->d_lock); 2803 spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED); 2804 } else { 2805 BUG_ON(p == dentry); 2806 spin_lock(&old_parent->d_lock); 2807 if (p != target) 2808 spin_lock_nested(&target->d_parent->d_lock, 2809 DENTRY_D_LOCK_NESTED); 2810 } 2811 spin_lock_nested(&dentry->d_lock, 2); 2812 spin_lock_nested(&target->d_lock, 3); 2813 2814 if (unlikely(d_in_lookup(target))) { 2815 dir = target->d_parent->d_inode; 2816 n = start_dir_add(dir); 2817 d_wait = __d_lookup_unhash(target); 2818 } 2819 2820 write_seqcount_begin(&dentry->d_seq); 2821 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED); 2822 2823 /* unhash both */ 2824 if (!d_unhashed(dentry)) 2825 ___d_drop(dentry); 2826 if (!d_unhashed(target)) 2827 ___d_drop(target); 2828 2829 /* ... and switch them in the tree */ 2830 dentry->d_parent = target->d_parent; 2831 if (!exchange) { 2832 copy_name(dentry, target); 2833 target->d_hash.pprev = NULL; 2834 dentry->d_parent->d_lockref.count++; 2835 if (dentry != old_parent) /* wasn't IS_ROOT */ 2836 WARN_ON(!--old_parent->d_lockref.count); 2837 } else { 2838 target->d_parent = old_parent; 2839 swap_names(dentry, target); 2840 if (!hlist_unhashed(&target->d_sib)) 2841 __hlist_del(&target->d_sib); 2842 hlist_add_head(&target->d_sib, &target->d_parent->d_children); 2843 __d_rehash(target); 2844 fsnotify_update_flags(target); 2845 } 2846 if (!hlist_unhashed(&dentry->d_sib)) 2847 __hlist_del(&dentry->d_sib); 2848 hlist_add_head(&dentry->d_sib, &dentry->d_parent->d_children); 2849 __d_rehash(dentry); 2850 fsnotify_update_flags(dentry); 2851 fscrypt_handle_d_move(dentry); 2852 2853 write_seqcount_end(&target->d_seq); 2854 write_seqcount_end(&dentry->d_seq); 2855 2856 if (dir) 2857 end_dir_add(dir, n, d_wait); 2858 2859 if (dentry->d_parent != old_parent) 2860 spin_unlock(&dentry->d_parent->d_lock); 2861 if (dentry != old_parent) 2862 spin_unlock(&old_parent->d_lock); 2863 spin_unlock(&target->d_lock); 2864 spin_unlock(&dentry->d_lock); 2865 } 2866 2867 /* 2868 * d_move - move a dentry 2869 * @dentry: entry to move 2870 * @target: new dentry 2871 * 2872 * Update the dcache to reflect the move of a file name. Negative 2873 * dcache entries should not be moved in this way. See the locking 2874 * requirements for __d_move. 2875 */ 2876 void d_move(struct dentry *dentry, struct dentry *target) 2877 { 2878 write_seqlock(&rename_lock); 2879 __d_move(dentry, target, false); 2880 write_sequnlock(&rename_lock); 2881 } 2882 EXPORT_SYMBOL(d_move); 2883 2884 /* 2885 * d_exchange - exchange two dentries 2886 * @dentry1: first dentry 2887 * @dentry2: second dentry 2888 */ 2889 void d_exchange(struct dentry *dentry1, struct dentry *dentry2) 2890 { 2891 write_seqlock(&rename_lock); 2892 2893 WARN_ON(!dentry1->d_inode); 2894 WARN_ON(!dentry2->d_inode); 2895 WARN_ON(IS_ROOT(dentry1)); 2896 WARN_ON(IS_ROOT(dentry2)); 2897 2898 __d_move(dentry1, dentry2, true); 2899 2900 write_sequnlock(&rename_lock); 2901 } 2902 2903 /** 2904 * d_ancestor - search for an ancestor 2905 * @p1: ancestor dentry 2906 * @p2: child dentry 2907 * 2908 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is 2909 * an ancestor of p2, else NULL. 2910 */ 2911 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2) 2912 { 2913 struct dentry *p; 2914 2915 for (p = p2; !IS_ROOT(p); p = p->d_parent) { 2916 if (p->d_parent == p1) 2917 return p; 2918 } 2919 return NULL; 2920 } 2921 2922 /* 2923 * This helper attempts to cope with remotely renamed directories 2924 * 2925 * It assumes that the caller is already holding 2926 * dentry->d_parent->d_inode->i_mutex, and rename_lock 2927 * 2928 * Note: If ever the locking in lock_rename() changes, then please 2929 * remember to update this too... 2930 */ 2931 static int __d_unalias(struct dentry *dentry, struct dentry *alias) 2932 { 2933 struct mutex *m1 = NULL; 2934 struct rw_semaphore *m2 = NULL; 2935 int ret = -ESTALE; 2936 2937 /* If alias and dentry share a parent, then no extra locks required */ 2938 if (alias->d_parent == dentry->d_parent) 2939 goto out_unalias; 2940 2941 /* See lock_rename() */ 2942 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) 2943 goto out_err; 2944 m1 = &dentry->d_sb->s_vfs_rename_mutex; 2945 if (!inode_trylock_shared(alias->d_parent->d_inode)) 2946 goto out_err; 2947 m2 = &alias->d_parent->d_inode->i_rwsem; 2948 out_unalias: 2949 if (alias->d_op && alias->d_op->d_unalias_trylock && 2950 !alias->d_op->d_unalias_trylock(alias)) 2951 goto out_err; 2952 __d_move(alias, dentry, false); 2953 if (alias->d_op && alias->d_op->d_unalias_unlock) 2954 alias->d_op->d_unalias_unlock(alias); 2955 ret = 0; 2956 out_err: 2957 if (m2) 2958 up_read(m2); 2959 if (m1) 2960 mutex_unlock(m1); 2961 return ret; 2962 } 2963 2964 /** 2965 * d_splice_alias - splice a disconnected dentry into the tree if one exists 2966 * @inode: the inode which may have a disconnected dentry 2967 * @dentry: a negative dentry which we want to point to the inode. 2968 * 2969 * If inode is a directory and has an IS_ROOT alias, then d_move that in 2970 * place of the given dentry and return it, else simply d_add the inode 2971 * to the dentry and return NULL. 2972 * 2973 * If a non-IS_ROOT directory is found, the filesystem is corrupt, and 2974 * we should error out: directories can't have multiple aliases. 2975 * 2976 * This is needed in the lookup routine of any filesystem that is exportable 2977 * (via knfsd) so that we can build dcache paths to directories effectively. 2978 * 2979 * If a dentry was found and moved, then it is returned. Otherwise NULL 2980 * is returned. This matches the expected return value of ->lookup. 2981 * 2982 * Cluster filesystems may call this function with a negative, hashed dentry. 2983 * In that case, we know that the inode will be a regular file, and also this 2984 * will only occur during atomic_open. So we need to check for the dentry 2985 * being already hashed only in the final case. 2986 */ 2987 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) 2988 { 2989 if (IS_ERR(inode)) 2990 return ERR_CAST(inode); 2991 2992 BUG_ON(!d_unhashed(dentry)); 2993 2994 if (!inode) 2995 goto out; 2996 2997 security_d_instantiate(dentry, inode); 2998 spin_lock(&inode->i_lock); 2999 if (S_ISDIR(inode->i_mode)) { 3000 struct dentry *new = __d_find_any_alias(inode); 3001 if (unlikely(new)) { 3002 /* The reference to new ensures it remains an alias */ 3003 spin_unlock(&inode->i_lock); 3004 write_seqlock(&rename_lock); 3005 if (unlikely(d_ancestor(new, dentry))) { 3006 write_sequnlock(&rename_lock); 3007 dput(new); 3008 new = ERR_PTR(-ELOOP); 3009 pr_warn_ratelimited( 3010 "VFS: Lookup of '%s' in %s %s" 3011 " would have caused loop\n", 3012 dentry->d_name.name, 3013 inode->i_sb->s_type->name, 3014 inode->i_sb->s_id); 3015 } else if (!IS_ROOT(new)) { 3016 struct dentry *old_parent = dget(new->d_parent); 3017 int err = __d_unalias(dentry, new); 3018 write_sequnlock(&rename_lock); 3019 if (err) { 3020 dput(new); 3021 new = ERR_PTR(err); 3022 } 3023 dput(old_parent); 3024 } else { 3025 __d_move(new, dentry, false); 3026 write_sequnlock(&rename_lock); 3027 } 3028 iput(inode); 3029 return new; 3030 } 3031 } 3032 out: 3033 __d_add(dentry, inode); 3034 return NULL; 3035 } 3036 EXPORT_SYMBOL(d_splice_alias); 3037 3038 /* 3039 * Test whether new_dentry is a subdirectory of old_dentry. 3040 * 3041 * Trivially implemented using the dcache structure 3042 */ 3043 3044 /** 3045 * is_subdir - is new dentry a subdirectory of old_dentry 3046 * @new_dentry: new dentry 3047 * @old_dentry: old dentry 3048 * 3049 * Returns true if new_dentry is a subdirectory of the parent (at any depth). 3050 * Returns false otherwise. 3051 * Caller must ensure that "new_dentry" is pinned before calling is_subdir() 3052 */ 3053 3054 bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) 3055 { 3056 bool subdir; 3057 unsigned seq; 3058 3059 if (new_dentry == old_dentry) 3060 return true; 3061 3062 /* Access d_parent under rcu as d_move() may change it. */ 3063 rcu_read_lock(); 3064 seq = read_seqbegin(&rename_lock); 3065 subdir = d_ancestor(old_dentry, new_dentry); 3066 /* Try lockless once... */ 3067 if (read_seqretry(&rename_lock, seq)) { 3068 /* ...else acquire lock for progress even on deep chains. */ 3069 read_seqlock_excl(&rename_lock); 3070 subdir = d_ancestor(old_dentry, new_dentry); 3071 read_sequnlock_excl(&rename_lock); 3072 } 3073 rcu_read_unlock(); 3074 return subdir; 3075 } 3076 EXPORT_SYMBOL(is_subdir); 3077 3078 static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry) 3079 { 3080 struct dentry *root = data; 3081 if (dentry != root) { 3082 if (d_unhashed(dentry) || !dentry->d_inode) 3083 return D_WALK_SKIP; 3084 3085 if (!(dentry->d_flags & DCACHE_GENOCIDE)) { 3086 dentry->d_flags |= DCACHE_GENOCIDE; 3087 dentry->d_lockref.count--; 3088 } 3089 } 3090 return D_WALK_CONTINUE; 3091 } 3092 3093 void d_genocide(struct dentry *parent) 3094 { 3095 d_walk(parent, parent, d_genocide_kill); 3096 } 3097 3098 void d_mark_tmpfile(struct file *file, struct inode *inode) 3099 { 3100 struct dentry *dentry = file->f_path.dentry; 3101 3102 BUG_ON(dname_external(dentry) || 3103 !hlist_unhashed(&dentry->d_u.d_alias) || 3104 !d_unlinked(dentry)); 3105 spin_lock(&dentry->d_parent->d_lock); 3106 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 3107 dentry->d_name.len = sprintf(dentry->d_shortname.string, "#%llu", 3108 (unsigned long long)inode->i_ino); 3109 spin_unlock(&dentry->d_lock); 3110 spin_unlock(&dentry->d_parent->d_lock); 3111 } 3112 EXPORT_SYMBOL(d_mark_tmpfile); 3113 3114 void d_tmpfile(struct file *file, struct inode *inode) 3115 { 3116 struct dentry *dentry = file->f_path.dentry; 3117 3118 inode_dec_link_count(inode); 3119 d_mark_tmpfile(file, inode); 3120 d_instantiate(dentry, inode); 3121 } 3122 EXPORT_SYMBOL(d_tmpfile); 3123 3124 /* 3125 * Obtain inode number of the parent dentry. 3126 */ 3127 ino_t d_parent_ino(struct dentry *dentry) 3128 { 3129 struct dentry *parent; 3130 struct inode *iparent; 3131 unsigned seq; 3132 ino_t ret; 3133 3134 scoped_guard(rcu) { 3135 seq = raw_seqcount_begin(&dentry->d_seq); 3136 parent = READ_ONCE(dentry->d_parent); 3137 iparent = d_inode_rcu(parent); 3138 if (likely(iparent)) { 3139 ret = iparent->i_ino; 3140 if (!read_seqcount_retry(&dentry->d_seq, seq)) 3141 return ret; 3142 } 3143 } 3144 3145 spin_lock(&dentry->d_lock); 3146 ret = dentry->d_parent->d_inode->i_ino; 3147 spin_unlock(&dentry->d_lock); 3148 return ret; 3149 } 3150 EXPORT_SYMBOL(d_parent_ino); 3151 3152 static __initdata unsigned long dhash_entries; 3153 static int __init set_dhash_entries(char *str) 3154 { 3155 if (!str) 3156 return 0; 3157 dhash_entries = simple_strtoul(str, &str, 0); 3158 return 1; 3159 } 3160 __setup("dhash_entries=", set_dhash_entries); 3161 3162 static void __init dcache_init_early(void) 3163 { 3164 /* If hashes are distributed across NUMA nodes, defer 3165 * hash allocation until vmalloc space is available. 3166 */ 3167 if (hashdist) 3168 return; 3169 3170 dentry_hashtable = 3171 alloc_large_system_hash("Dentry cache", 3172 sizeof(struct hlist_bl_head), 3173 dhash_entries, 3174 13, 3175 HASH_EARLY | HASH_ZERO, 3176 &d_hash_shift, 3177 NULL, 3178 0, 3179 0); 3180 d_hash_shift = 32 - d_hash_shift; 3181 3182 runtime_const_init(shift, d_hash_shift); 3183 runtime_const_init(ptr, dentry_hashtable); 3184 } 3185 3186 static void __init dcache_init(void) 3187 { 3188 /* 3189 * A constructor could be added for stable state like the lists, 3190 * but it is probably not worth it because of the cache nature 3191 * of the dcache. 3192 */ 3193 dentry_cache = KMEM_CACHE_USERCOPY(dentry, 3194 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_ACCOUNT, 3195 d_shortname.string); 3196 3197 /* Hash may have been set up in dcache_init_early */ 3198 if (!hashdist) 3199 return; 3200 3201 dentry_hashtable = 3202 alloc_large_system_hash("Dentry cache", 3203 sizeof(struct hlist_bl_head), 3204 dhash_entries, 3205 13, 3206 HASH_ZERO, 3207 &d_hash_shift, 3208 NULL, 3209 0, 3210 0); 3211 d_hash_shift = 32 - d_hash_shift; 3212 3213 runtime_const_init(shift, d_hash_shift); 3214 runtime_const_init(ptr, dentry_hashtable); 3215 } 3216 3217 /* SLAB cache for __getname() consumers */ 3218 struct kmem_cache *names_cachep __ro_after_init; 3219 EXPORT_SYMBOL(names_cachep); 3220 3221 void __init vfs_caches_init_early(void) 3222 { 3223 int i; 3224 3225 for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++) 3226 INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]); 3227 3228 dcache_init_early(); 3229 inode_init_early(); 3230 } 3231 3232 void __init vfs_caches_init(void) 3233 { 3234 names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0, 3235 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL); 3236 3237 dcache_init(); 3238 inode_init(); 3239 files_init(); 3240 files_maxfiles_init(); 3241 mnt_init(); 3242 bdev_cache_init(); 3243 chrdev_init(); 3244 } 3245