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