dcache.c 83.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * fs/dcache.c
 *
 * Complete reimplementation
 * (C) 1997 Thomas Schoebel-Theuer,
 * with heavy changes by Linus Torvalds
 */

/*
 * Notes on the allocation strategy:
 *
 * The dcache is a master of the icache - whenever a dcache entry
 * exists, the inode will always exist. "iput()" is done either when
 * the dcache entry is deleted or garbage collected.
 */

17
#include <linux/ratelimit.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/fs.h>
21
#include <linux/fsnotify.h>
Linus Torvalds's avatar
Linus Torvalds committed
22 23 24 25
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/hash.h>
#include <linux/cache.h>
26
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29
#include <linux/security.h>
#include <linux/seqlock.h>
#include <linux/bootmem.h>
30 31
#include <linux/bit_spinlock.h>
#include <linux/rculist_bl.h>
32
#include <linux/list_lru.h>
33
#include "internal.h"
34
#include "mount.h"
Linus Torvalds's avatar
Linus Torvalds committed
35

Nick Piggin's avatar
Nick Piggin committed
36 37
/*
 * Usage:
38
 * dcache->d_inode->i_lock protects:
39
 *   - i_dentry, d_u.d_alias, d_inode of aliases
40 41
 * dcache_hash_bucket lock protects:
 *   - the dcache hash table
42 43
 * s_roots bl list spinlock protects:
 *   - the s_roots list (see __d_drop)
44
 * dentry->d_sb->s_dentry_lru_lock protects:
Nick Piggin's avatar
Nick Piggin committed
45 46 47 48 49
 *   - the dcache lru lists and counters
 * d_lock protects:
 *   - d_flags
 *   - d_name
 *   - d_lru
Nick Piggin's avatar
Nick Piggin committed
50
 *   - d_count
Nick Piggin's avatar
Nick Piggin committed
51
 *   - d_unhashed()
Nick Piggin's avatar
Nick Piggin committed
52 53
 *   - d_parent and d_subdirs
 *   - childrens' d_child and d_parent
54
 *   - d_u.d_alias, d_inode
Nick Piggin's avatar
Nick Piggin committed
55 56
 *
 * Ordering:
57
 * dentry->d_inode->i_lock
Nick Piggin's avatar
Nick Piggin committed
58
 *   dentry->d_lock
59
 *     dentry->d_sb->s_dentry_lru_lock
60
 *     dcache_hash_bucket lock
61
 *     s_roots lock
Nick Piggin's avatar
Nick Piggin committed
62
 *
Nick Piggin's avatar
Nick Piggin committed
63 64 65 66 67 68 69
 * If there is an ancestor relationship:
 * dentry->d_parent->...->d_parent->d_lock
 *   ...
 *     dentry->d_parent->d_lock
 *       dentry->d_lock
 *
 * If no ancestor relationship:
70
 * arbitrary, since it's serialized on rename_lock
Nick Piggin's avatar
Nick Piggin committed
71
 */
72
int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds's avatar
Linus Torvalds committed
73 74
EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);

Al Viro's avatar
Al Viro committed
75
__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds's avatar
Linus Torvalds committed
76

77
EXPORT_SYMBOL(rename_lock);
Linus Torvalds's avatar
Linus Torvalds committed
78

79
static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds's avatar
Linus Torvalds committed
80

David Howells's avatar
David Howells committed
81 82 83 84 85
const struct qstr empty_name = QSTR_INIT("", 0);
EXPORT_SYMBOL(empty_name);
const struct qstr slash_name = QSTR_INIT("/", 1);
EXPORT_SYMBOL(slash_name);

Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90 91 92 93 94
/*
 * This is the single most critical data structure when it comes
 * to the dcache: the hashtable for lookups. Somebody should try
 * to make this good - I've just made it work.
 *
 * This hash-function tries to avoid losing too many bits of hash
 * information, yet avoid using a prime hash-size or similar.
 */

95
static unsigned int d_hash_shift __read_mostly;
96

97
static struct hlist_bl_head *dentry_hashtable __read_mostly;
98

99
static inline struct hlist_bl_head *d_hash(unsigned int hash)
100
{
101
	return dentry_hashtable + (hash >> d_hash_shift);
102 103
}

Al Viro's avatar
Al Viro committed
104 105 106 107 108 109 110 111 112 113 114
#define IN_LOOKUP_SHIFT 10
static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];

static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
					unsigned int hash)
{
	hash += (unsigned long) parent / L1_CACHE_BYTES;
	return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
}


Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118 119
/* Statistics gathering. */
struct dentry_stat_t dentry_stat = {
	.age_limit = 45,
};

120
static DEFINE_PER_CPU(long, nr_dentry);
121
static DEFINE_PER_CPU(long, nr_dentry_unused);
122 123

#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
124 125 126 127 128 129 130 131 132 133 134 135 136

/*
 * Here we resort to our own counters instead of using generic per-cpu counters
 * for consistency with what the vfs inode code does. We are expected to harvest
 * better code and performance by having our own specialized counters.
 *
 * Please note that the loop is done over all possible CPUs, not over all online
 * CPUs. The reason for this is that we don't want to play games with CPUs going
 * on and off. If one of them goes off, we will just keep their counters.
 *
 * glommer: See cffbc8a for details, and if you ever intend to change this,
 * please update all vfs counters to match.
 */
137
static long get_nr_dentry(void)
138 139
{
	int i;
140
	long sum = 0;
141 142 143 144 145
	for_each_possible_cpu(i)
		sum += per_cpu(nr_dentry, i);
	return sum < 0 ? 0 : sum;
}

146 147 148 149 150 151 152 153 154
static long get_nr_dentry_unused(void)
{
	int i;
	long sum = 0;
	for_each_possible_cpu(i)
		sum += per_cpu(nr_dentry_unused, i);
	return sum < 0 ? 0 : sum;
}

155
int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
156 157
		   size_t *lenp, loff_t *ppos)
{
158
	dentry_stat.nr_dentry = get_nr_dentry();
159
	dentry_stat.nr_unused = get_nr_dentry_unused();
160
	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
161 162 163
}
#endif

164 165 166 167
/*
 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
 * The strings are both count bytes long, and count is non-zero.
 */
168 169 170 171 172 173 174 175 176 177 178 179
#ifdef CONFIG_DCACHE_WORD_ACCESS

#include <asm/word-at-a-time.h>
/*
 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
 * aligned allocation for this particular component. We don't
 * strictly need the load_unaligned_zeropad() safety, but it
 * doesn't hurt either.
 *
 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
 * need the careful unaligned handling.
 */
180
static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
181
{
182 183 184
	unsigned long a,b,mask;

	for (;;) {
185
		a = read_word_at_a_time(cs);
186
		b = load_unaligned_zeropad(ct);
187 188 189 190 191 192 193 194 195 196
		if (tcount < sizeof(unsigned long))
			break;
		if (unlikely(a != b))
			return 1;
		cs += sizeof(unsigned long);
		ct += sizeof(unsigned long);
		tcount -= sizeof(unsigned long);
		if (!tcount)
			return 0;
	}
197
	mask = bytemask_from_count(tcount);
198
	return unlikely(!!((a ^ b) & mask));
199 200
}

201
#else
202

203
static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
204
{
205 206 207 208 209 210 211 212 213 214
	do {
		if (*cs != *ct)
			return 1;
		cs++;
		ct++;
		tcount--;
	} while (tcount);
	return 0;
}

215 216
#endif

217 218 219 220
static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
{
	/*
	 * Be careful about RCU walk racing with rename:
221
	 * use 'READ_ONCE' to fetch the name pointer.
222 223 224 225 226 227 228 229 230 231 232 233 234
	 *
	 * NOTE! Even if a rename will mean that the length
	 * was not loaded atomically, we don't care. The
	 * RCU walk will check the sequence count eventually,
	 * and catch it. And we won't overrun the buffer,
	 * because we're reading the name pointer atomically,
	 * and a dentry name is guaranteed to be properly
	 * terminated with a NUL byte.
	 *
	 * End result: even if 'len' is wrong, we'll exit
	 * early because the data cannot match (there can
	 * be no NUL in the ct/tcount data)
	 */
235
	const unsigned char *cs = READ_ONCE(dentry->d_name.name);
236

237
	return dentry_string_cmp(cs, ct, tcount);
238 239
}

240 241 242 243 244 245 246 247 248 249 250 251 252
struct external_name {
	union {
		atomic_t count;
		struct rcu_head head;
	} u;
	unsigned char name[];
};

static inline struct external_name *external_name(struct dentry *dentry)
{
	return container_of(dentry->d_name.name, struct external_name, name[0]);
}

Christoph Hellwig's avatar
Christoph Hellwig committed
253
static void __d_free(struct rcu_head *head)
Linus Torvalds's avatar
Linus Torvalds committed
254
{
Christoph Hellwig's avatar
Christoph Hellwig committed
255 256
	struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);

257 258 259
	kmem_cache_free(dentry_cache, dentry); 
}

260 261 262 263 264 265 266 267 268 269 270 271
static void __d_free_external_name(struct rcu_head *head)
{
	struct external_name *name = container_of(head, struct external_name,
						  u.head);

	mod_node_page_state(page_pgdat(virt_to_page(name)),
			    NR_INDIRECTLY_RECLAIMABLE_BYTES,
			    -ksize(name));

	kfree(name);
}

272 273 274
static void __d_free_external(struct rcu_head *head)
{
	struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
275 276 277 278

	__d_free_external_name(&external_name(dentry)->u.head);

	kmem_cache_free(dentry_cache, dentry);
Linus Torvalds's avatar
Linus Torvalds committed
279 280
}

281 282 283 284 285
static inline int dname_external(const struct dentry *dentry)
{
	return dentry->d_name.name != dentry->d_iname;
}

Al Viro's avatar
Al Viro committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
{
	spin_lock(&dentry->d_lock);
	if (unlikely(dname_external(dentry))) {
		struct external_name *p = external_name(dentry);
		atomic_inc(&p->u.count);
		spin_unlock(&dentry->d_lock);
		name->name = p->name;
	} else {
		memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
		spin_unlock(&dentry->d_lock);
		name->name = name->inline_name;
	}
}
EXPORT_SYMBOL(take_dentry_name_snapshot);

void release_dentry_name_snapshot(struct name_snapshot *name)
{
	if (unlikely(name->name != name->inline_name)) {
		struct external_name *p;
		p = container_of(name->name, struct external_name, name[0]);
		if (unlikely(atomic_dec_and_test(&p->u.count)))
308
			call_rcu(&p->u.head, __d_free_external_name);
Al Viro's avatar
Al Viro committed
309 310 311 312
	}
}
EXPORT_SYMBOL(release_dentry_name_snapshot);

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static inline void __d_set_inode_and_type(struct dentry *dentry,
					  struct inode *inode,
					  unsigned type_flags)
{
	unsigned flags;

	dentry->d_inode = inode;
	flags = READ_ONCE(dentry->d_flags);
	flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
	flags |= type_flags;
	WRITE_ONCE(dentry->d_flags, flags);
}

static inline void __d_clear_type_and_inode(struct dentry *dentry)
{
	unsigned flags = READ_ONCE(dentry->d_flags);

	flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
	WRITE_ONCE(dentry->d_flags, flags);
	dentry->d_inode = NULL;
}

Al Viro's avatar
Al Viro committed
335 336
static void dentry_free(struct dentry *dentry)
{
337
	WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
338 339 340 341 342 343 344
	if (unlikely(dname_external(dentry))) {
		struct external_name *p = external_name(dentry);
		if (likely(atomic_dec_and_test(&p->u.count))) {
			call_rcu(&dentry->d_u.d_rcu, __d_free_external);
			return;
		}
	}
Al Viro's avatar
Al Viro committed
345 346 347 348 349 350 351
	/* if dentry was never visible to RCU, immediate free is OK */
	if (!(dentry->d_flags & DCACHE_RCUACCESS))
		__d_free(&dentry->d_u.d_rcu);
	else
		call_rcu(&dentry->d_u.d_rcu, __d_free);
}

Linus Torvalds's avatar
Linus Torvalds committed
352 353
/*
 * Release the dentry's inode, using the filesystem
354
 * d_iput() operation if defined.
Nick Piggin's avatar
Nick Piggin committed
355 356 357
 */
static void dentry_unlink_inode(struct dentry * dentry)
	__releases(dentry->d_lock)
358
	__releases(dentry->d_inode->i_lock)
Nick Piggin's avatar
Nick Piggin committed
359 360
{
	struct inode *inode = dentry->d_inode;
361

362
	raw_write_seqcount_begin(&dentry->d_seq);
363
	__d_clear_type_and_inode(dentry);
364
	hlist_del_init(&dentry->d_u.d_alias);
365
	raw_write_seqcount_end(&dentry->d_seq);
Nick Piggin's avatar
Nick Piggin committed
366
	spin_unlock(&dentry->d_lock);
367
	spin_unlock(&inode->i_lock);
Nick Piggin's avatar
Nick Piggin committed
368 369 370 371 372 373 374 375
	if (!inode->i_nlink)
		fsnotify_inoderemove(inode);
	if (dentry->d_op && dentry->d_op->d_iput)
		dentry->d_op->d_iput(dentry, inode);
	else
		iput(inode);
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/*
 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
 * is in use - which includes both the "real" per-superblock
 * LRU list _and_ the DCACHE_SHRINK_LIST use.
 *
 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
 * on the shrink list (ie not on the superblock LRU list).
 *
 * The per-cpu "nr_dentry_unused" counters are updated with
 * the DCACHE_LRU_LIST bit.
 *
 * These helper functions make sure we always follow the
 * rules. d_lock must be held by the caller.
 */
#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
static void d_lru_add(struct dentry *dentry)
{
	D_FLAG_VERIFY(dentry, 0);
	dentry->d_flags |= DCACHE_LRU_LIST;
	this_cpu_inc(nr_dentry_unused);
	WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
}

static void d_lru_del(struct dentry *dentry)
{
	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
	dentry->d_flags &= ~DCACHE_LRU_LIST;
	this_cpu_dec(nr_dentry_unused);
	WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
}

static void d_shrink_del(struct dentry *dentry)
{
	D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
	list_del_init(&dentry->d_lru);
	dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
	this_cpu_dec(nr_dentry_unused);
}

static void d_shrink_add(struct dentry *dentry, struct list_head *list)
{
	D_FLAG_VERIFY(dentry, 0);
	list_add(&dentry->d_lru, list);
	dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
	this_cpu_inc(nr_dentry_unused);
}

/*
 * These can only be called under the global LRU lock, ie during the
 * callback for freeing the LRU list. "isolate" removes it from the
 * LRU lists entirely, while shrink_move moves it to the indicated
 * private list.
 */
429
static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
430 431 432 433
{
	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
	dentry->d_flags &= ~DCACHE_LRU_LIST;
	this_cpu_dec(nr_dentry_unused);
434
	list_lru_isolate(lru, &dentry->d_lru);
435 436
}

437 438
static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
			      struct list_head *list)
439 440 441
{
	D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
	dentry->d_flags |= DCACHE_SHRINK_LIST;
442
	list_lru_isolate_move(lru, &dentry->d_lru, list);
443 444
}

Nick Piggin's avatar
Nick Piggin committed
445 446 447 448 449 450 451 452 453 454 455 456 457
/**
 * d_drop - drop a dentry
 * @dentry: dentry to drop
 *
 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
 * be found through a VFS lookup any more. Note that this is different from
 * deleting the dentry - d_delete will try to mark the dentry negative if
 * possible, giving a successful _negative_ lookup, while d_drop will
 * just make the cache lookup fail.
 *
 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
 * reason (NFS timeouts or autofs deletes).
 *
458 459 460
 * __d_drop requires dentry->d_lock
 * ___d_drop doesn't mark dentry as "unhashed"
 *   (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
Nick Piggin's avatar
Nick Piggin committed
461
 */
462
static void ___d_drop(struct dentry *dentry)
Nick Piggin's avatar
Nick Piggin committed
463
{
464 465 466 467 468 469 470 471 472 473
	struct hlist_bl_head *b;
	/*
	 * Hashed dentries are normally on the dentry hashtable,
	 * with the exception of those newly allocated by
	 * d_obtain_root, which are always IS_ROOT:
	 */
	if (unlikely(IS_ROOT(dentry)))
		b = &dentry->d_sb->s_roots;
	else
		b = d_hash(dentry->d_name.hash);
474

475 476 477
	hlist_bl_lock(b);
	__hlist_bl_del(&dentry->d_hash);
	hlist_bl_unlock(b);
Nick Piggin's avatar
Nick Piggin committed
478
}
479 480 481

void __d_drop(struct dentry *dentry)
{
482 483 484 485 486
	if (!d_unhashed(dentry)) {
		___d_drop(dentry);
		dentry->d_hash.pprev = NULL;
		write_seqcount_invalidate(&dentry->d_seq);
	}
487
}
Nick Piggin's avatar
Nick Piggin committed
488 489 490 491 492 493 494 495 496 497
EXPORT_SYMBOL(__d_drop);

void d_drop(struct dentry *dentry)
{
	spin_lock(&dentry->d_lock);
	__d_drop(dentry);
	spin_unlock(&dentry->d_lock);
}
EXPORT_SYMBOL(d_drop);

Al Viro's avatar
Al Viro committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
{
	struct dentry *next;
	/*
	 * Inform d_walk() and shrink_dentry_list() that we are no longer
	 * attached to the dentry tree
	 */
	dentry->d_flags |= DCACHE_DENTRY_KILLED;
	if (unlikely(list_empty(&dentry->d_child)))
		return;
	__list_del_entry(&dentry->d_child);
	/*
	 * Cursors can move around the list of children.  While we'd been
	 * a normal list member, it didn't matter - ->d_child.next would've
	 * been updated.  However, from now on it won't be and for the
	 * things like d_walk() it might end up with a nasty surprise.
	 * Normally d_walk() doesn't care about cursors moving around -
	 * ->d_lock on parent prevents that and since a cursor has no children
	 * of its own, we get through it without ever unlocking the parent.
	 * There is one exception, though - if we ascend from a child that
	 * gets killed as soon as we unlock it, the next sibling is found
	 * using the value left in its ->d_child.next.  And if _that_
	 * pointed to a cursor, and cursor got moved (e.g. by lseek())
	 * before d_walk() regains parent->d_lock, we'll end up skipping
	 * everything the cursor had been moved past.
	 *
	 * Solution: make sure that the pointer left behind in ->d_child.next
	 * points to something that won't be moving around.  I.e. skip the
	 * cursors.
	 */
	while (dentry->d_child.next != &parent->d_subdirs) {
		next = list_entry(dentry->d_child.next, struct dentry, d_child);
		if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
			break;
		dentry->d_child.next = next->d_child.next;
	}
}

Al Viro's avatar
Al Viro committed
536
static void __dentry_kill(struct dentry *dentry)
537
{
538 539 540
	struct dentry *parent = NULL;
	bool can_free = true;
	if (!IS_ROOT(dentry))
541
		parent = dentry->d_parent;
Nick Piggin's avatar
Nick Piggin committed
542

543 544 545 546 547
	/*
	 * The dentry is now unrecoverably dead to the world.
	 */
	lockref_mark_dead(&dentry->d_lockref);

Sage Weil's avatar
Sage Weil committed
548 549 550 551
	/*
	 * inform the fs via d_prune that this dentry is about to be
	 * unhashed and destroyed.
	 */
552
	if (dentry->d_flags & DCACHE_OP_PRUNE)
Yan, Zheng's avatar
Yan, Zheng committed
553 554
		dentry->d_op->d_prune(dentry);

555 556 557 558
	if (dentry->d_flags & DCACHE_LRU_LIST) {
		if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
			d_lru_del(dentry);
	}
559 560
	/* if it was on the hash then remove it */
	__d_drop(dentry);
Al Viro's avatar
Al Viro committed
561
	dentry_unlist(dentry, parent);
Al Viro's avatar
Al Viro committed
562 563
	if (parent)
		spin_unlock(&parent->d_lock);
564 565 566 567
	if (dentry->d_inode)
		dentry_unlink_inode(dentry);
	else
		spin_unlock(&dentry->d_lock);
Al Viro's avatar
Al Viro committed
568 569 570 571
	this_cpu_dec(nr_dentry);
	if (dentry->d_op && dentry->d_op->d_release)
		dentry->d_op->d_release(dentry);

572 573 574 575 576 577 578 579
	spin_lock(&dentry->d_lock);
	if (dentry->d_flags & DCACHE_SHRINK_LIST) {
		dentry->d_flags |= DCACHE_MAY_FREE;
		can_free = false;
	}
	spin_unlock(&dentry->d_lock);
	if (likely(can_free))
		dentry_free(dentry);
580
	cond_resched();
Al Viro's avatar
Al Viro committed
581 582
}

583
static struct dentry *__lock_parent(struct dentry *dentry)
584
{
585
	struct dentry *parent;
586
	rcu_read_lock();
587
	spin_unlock(&dentry->d_lock);
588
again:
589
	parent = READ_ONCE(dentry->d_parent);
590 591 592 593 594 595 596 597 598 599 600 601 602
	spin_lock(&parent->d_lock);
	/*
	 * We can't blindly lock dentry until we are sure
	 * that we won't violate the locking order.
	 * Any changes of dentry->d_parent must have
	 * been done with parent->d_lock held, so
	 * spin_lock() above is enough of a barrier
	 * for checking if it's still our child.
	 */
	if (unlikely(parent != dentry->d_parent)) {
		spin_unlock(&parent->d_lock);
		goto again;
	}
603 604
	rcu_read_unlock();
	if (parent != dentry)
605
		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
606
	else
607 608 609 610
		parent = NULL;
	return parent;
}

611 612 613 614 615 616 617 618 619 620
static inline struct dentry *lock_parent(struct dentry *dentry)
{
	struct dentry *parent = dentry->d_parent;
	if (IS_ROOT(dentry))
		return NULL;
	if (likely(spin_trylock(&parent->d_lock)))
		return parent;
	return __lock_parent(dentry);
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
static inline bool retain_dentry(struct dentry *dentry)
{
	WARN_ON(d_in_lookup(dentry));

	/* Unreachable? Get rid of it */
	if (unlikely(d_unhashed(dentry)))
		return false;

	if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
		return false;

	if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
		if (dentry->d_op->d_delete(dentry))
			return false;
	}
636 637 638 639 640 641
	/* retain; LRU fodder */
	dentry->d_lockref.count--;
	if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
		d_lru_add(dentry);
	else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
		dentry->d_flags |= DCACHE_REFERENCED;
642 643 644
	return true;
}

645 646 647 648 649 650 651 652 653 654 655 656
/*
 * Finish off a dentry we've decided to kill.
 * dentry->d_lock must be held, returns with it unlocked.
 * Returns dentry requiring refcount drop, or NULL if we're done.
 */
static struct dentry *dentry_kill(struct dentry *dentry)
	__releases(dentry->d_lock)
{
	struct inode *inode = dentry->d_inode;
	struct dentry *parent = NULL;

	if (inode && unlikely(!spin_trylock(&inode->i_lock)))
657
		goto slow_positive;
658 659 660 661

	if (!IS_ROOT(dentry)) {
		parent = dentry->d_parent;
		if (unlikely(!spin_trylock(&parent->d_lock))) {
662 663 664 665 666 667 668 669
			parent = __lock_parent(dentry);
			if (likely(inode || !dentry->d_inode))
				goto got_locks;
			/* negative that became positive */
			if (parent)
				spin_unlock(&parent->d_lock);
			inode = dentry->d_inode;
			goto slow_positive;
670 671 672 673 674
		}
	}
	__dentry_kill(dentry);
	return parent;

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
slow_positive:
	spin_unlock(&dentry->d_lock);
	spin_lock(&inode->i_lock);
	spin_lock(&dentry->d_lock);
	parent = lock_parent(dentry);
got_locks:
	if (unlikely(dentry->d_lockref.count != 1)) {
		dentry->d_lockref.count--;
	} else if (likely(!retain_dentry(dentry))) {
		__dentry_kill(dentry);
		return parent;
	}
	/* we are keeping it, after all */
	if (inode)
		spin_unlock(&inode->i_lock);
	if (parent)
		spin_unlock(&parent->d_lock);
692
	spin_unlock(&dentry->d_lock);
693
	return NULL;
694 695
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
/*
 * Try to do a lockless dput(), and return whether that was successful.
 *
 * If unsuccessful, we return false, having already taken the dentry lock.
 *
 * The caller needs to hold the RCU read lock, so that the dentry is
 * guaranteed to stay around even if the refcount goes down to zero!
 */
static inline bool fast_dput(struct dentry *dentry)
{
	int ret;
	unsigned int d_flags;

	/*
	 * If we have a d_op->d_delete() operation, we sould not
711
	 * let the dentry count go to zero, so use "put_or_lock".
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	 */
	if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
		return lockref_put_or_lock(&dentry->d_lockref);

	/*
	 * .. otherwise, we can try to just decrement the
	 * lockref optimistically.
	 */
	ret = lockref_put_return(&dentry->d_lockref);

	/*
	 * If the lockref_put_return() failed due to the lock being held
	 * by somebody else, the fast path has failed. We will need to
	 * get the lock, and then check the count again.
	 */
	if (unlikely(ret < 0)) {
		spin_lock(&dentry->d_lock);
		if (dentry->d_lockref.count > 1) {
			dentry->d_lockref.count--;
			spin_unlock(&dentry->d_lock);
			return 1;
		}
		return 0;
	}

	/*
	 * If we weren't the last ref, we're done.
	 */
	if (ret)
		return 1;

	/*
	 * Careful, careful. The reference count went down
	 * to zero, but we don't hold the dentry lock, so
	 * somebody else could get it again, and do another
	 * dput(), and we need to not race with that.
	 *
	 * However, there is a very special and common case
	 * where we don't care, because there is nothing to
	 * do: the dentry is still hashed, it does not have
	 * a 'delete' op, and it's referenced and already on
	 * the LRU list.
	 *
	 * NOTE! Since we aren't locked, these values are
	 * not "stable". However, it is sufficient that at
	 * some point after we dropped the reference the
	 * dentry was hashed and the flags had the proper
	 * value. Other dentry users may have re-gotten
	 * a reference to the dentry and change that, but
	 * our work is done - we can leave the dentry
	 * around with a zero refcount.
	 */
	smp_rmb();
765
	d_flags = READ_ONCE(dentry->d_flags);
766
	d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

	/* Nothing to do? Dropping the reference was all we needed? */
	if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
		return 1;

	/*
	 * Not the fast normal case? Get the lock. We've already decremented
	 * the refcount, but we'll need to re-check the situation after
	 * getting the lock.
	 */
	spin_lock(&dentry->d_lock);

	/*
	 * Did somebody else grab a reference to it in the meantime, and
	 * we're no longer the last user after all? Alternatively, somebody
	 * else could have killed it and marked it dead. Either way, we
	 * don't need to do anything else.
	 */
	if (dentry->d_lockref.count) {
		spin_unlock(&dentry->d_lock);
		return 1;
	}

	/*
	 * Re-get the reference we optimistically dropped. We hold the
	 * lock, and we just tested that it was zero, so we can just
	 * set it to 1.
	 */
	dentry->d_lockref.count = 1;
	return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
/* 
 * This is dput
 *
 * This is complicated by the fact that we do not want to put
 * dentries that are no longer on any hash chain on the unused
 * list: we'd much rather just get rid of them immediately.
 *
 * However, that implies that we have to traverse the dentry
 * tree upwards to the parents which might _also_ now be
 * scheduled for deletion (it may have been only waiting for
 * its last child to go away).
 *
 * This tail recursion is done by hand as we don't want to depend
 * on the compiler to always get this right (gcc generally doesn't).
 * Real recursion would eat up our stack space.
 */

/*
 * dput - release a dentry
 * @dentry: dentry to release 
 *
 * Release a dentry. This will drop the usage count and if appropriate
 * call the dentry unlink method as well as removing it from the queues and
 * releasing its resources. If the parent dentries were scheduled for release
 * they too may now get deleted.
 */
void dput(struct dentry *dentry)
{
828 829
	while (dentry) {
		might_sleep();
Linus Torvalds's avatar
Linus Torvalds committed
830

831 832 833 834 835
		rcu_read_lock();
		if (likely(fast_dput(dentry))) {
			rcu_read_unlock();
			return;
		}
836

837
		/* Slow case: now with the dentry lock held */
838 839
		rcu_read_unlock();

840 841 842 843
		if (likely(retain_dentry(dentry))) {
			spin_unlock(&dentry->d_lock);
			return;
		}
844

845
		dentry = dentry_kill(dentry);
846
	}
Linus Torvalds's avatar
Linus Torvalds committed
847
}
848
EXPORT_SYMBOL(dput);
Linus Torvalds's avatar
Linus Torvalds committed
849 850


Nick Piggin's avatar
Nick Piggin committed
851
/* This must be called with d_lock held */
852
static inline void __dget_dlock(struct dentry *dentry)
Nick Piggin's avatar
Nick Piggin committed
853
{
854
	dentry->d_lockref.count++;
Nick Piggin's avatar
Nick Piggin committed
855 856
}

857
static inline void __dget(struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
858
{
859
	lockref_get(&dentry->d_lockref);
Linus Torvalds's avatar
Linus Torvalds committed
860 861
}

Nick Piggin's avatar
Nick Piggin committed
862 863
struct dentry *dget_parent(struct dentry *dentry)
{
864
	int gotref;
Nick Piggin's avatar
Nick Piggin committed
865 866
	struct dentry *ret;

867 868 869 870 871
	/*
	 * Do optimistic parent lookup without any
	 * locking.
	 */
	rcu_read_lock();
872
	ret = READ_ONCE(dentry->d_parent);
873 874 875
	gotref = lockref_get_not_zero(&ret->d_lockref);
	rcu_read_unlock();
	if (likely(gotref)) {
876
		if (likely(ret == READ_ONCE(dentry->d_parent)))
877 878 879 880
			return ret;
		dput(ret);
	}

Nick Piggin's avatar
Nick Piggin committed
881
repeat:
882 883 884 885 886
	/*
	 * Don't need rcu_dereference because we re-check it was correct under
	 * the lock.
	 */
	rcu_read_lock();
Nick Piggin's avatar
Nick Piggin committed
887
	ret = dentry->d_parent;
888 889 890 891
	spin_lock(&ret->d_lock);
	if (unlikely(ret != dentry->d_parent)) {
		spin_unlock(&ret->d_lock);
		rcu_read_unlock();
Nick Piggin's avatar
Nick Piggin committed
892 893
		goto repeat;
	}
894
	rcu_read_unlock();
895 896
	BUG_ON(!ret->d_lockref.count);
	ret->d_lockref.count++;
Nick Piggin's avatar
Nick Piggin committed
897 898 899 900 901
	spin_unlock(&ret->d_lock);
	return ret;
}
EXPORT_SYMBOL(dget_parent);

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
static struct dentry * __d_find_any_alias(struct inode *inode)
{
	struct dentry *alias;

	if (hlist_empty(&inode->i_dentry))
		return NULL;
	alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
	__dget(alias);
	return alias;
}

/**
 * d_find_any_alias - find any alias for a given inode
 * @inode: inode to find an alias for
 *
 * If any aliases exist for the given inode, take and return a
 * reference for one of them.  If no aliases exist, return %NULL.
 */
struct dentry *d_find_any_alias(struct inode *inode)
{
	struct dentry *de;

	spin_lock(&inode->i_lock);
	de = __d_find_any_alias(inode);
	spin_unlock(&inode->i_lock);
	return de;
}
EXPORT_SYMBOL(d_find_any_alias);

Linus Torvalds's avatar
Linus Torvalds committed
931 932 933 934 935 936 937 938
/**
 * d_find_alias - grab a hashed alias of inode
 * @inode: inode in question
 *
 * If inode has a hashed alias, or is a directory and has any alias,
 * acquire the reference to alias and return it. Otherwise return NULL.
 * Notice that if inode is a directory there can be only one alias and
 * it can be unhashed only if it has no children, or if it is the root
939 940
 * of a filesystem, or if the directory was renamed and d_revalidate
 * was the first vfs operation to notice.
Linus Torvalds's avatar
Linus Torvalds committed
941
 *
942
 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
943
 * any other hashed alias over that one.
Linus Torvalds's avatar
Linus Torvalds committed
944
 */
945
static struct dentry *__d_find_alias(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
947 948 949 950
	struct dentry *alias;

	if (S_ISDIR(inode->i_mode))
		return __d_find_any_alias(inode);
Linus Torvalds's avatar
Linus Torvalds committed
951

952
	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Piggin's avatar
Nick Piggin committed
953
		spin_lock(&alias->d_lock);
954
 		if (!d_unhashed(alias)) {
955 956 957
			__dget_dlock(alias);
			spin_unlock(&alias->d_lock);
			return alias;
Linus Torvalds's avatar
Linus Torvalds committed
958
		}
Nick Piggin's avatar
Nick Piggin committed
959
		spin_unlock(&alias->d_lock);
Linus Torvalds's avatar
Linus Torvalds committed
960
	}
Nick Piggin's avatar
Nick Piggin committed
961
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
962 963
}

Nick Piggin's avatar
Nick Piggin committed
964
struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
965
{
966 967
	struct dentry *de = NULL;

968
	if (!hlist_empty(&inode->i_dentry)) {
969
		spin_lock(&inode->i_lock);
970
		de = __d_find_alias(inode);
971
		spin_unlock(&inode->i_lock);
972
	}
Linus Torvalds's avatar
Linus Torvalds committed
973 974
	return de;
}
975
EXPORT_SYMBOL(d_find_alias);
Linus Torvalds's avatar
Linus Torvalds committed
976 977 978 979 980 981 982

/*
 *	Try to kill dentries associated with this inode.
 * WARNING: you must own a reference to inode.
 */
void d_prune_aliases(struct inode *inode)
{
983
	struct dentry *dentry;
Linus Torvalds's avatar
Linus Torvalds committed
984
restart:
985
	spin_lock(&inode->i_lock);
986
	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds's avatar
Linus Torvalds committed
987
		spin_lock(&dentry->d_lock);
988
		if (!dentry->d_lockref.count) {
989 990 991
			struct dentry *parent = lock_parent(dentry);
			if (likely(!dentry->d_lockref.count)) {
				__dentry_kill(dentry);
992
				dput(parent);
993 994 995 996
				goto restart;
			}
			if (parent)
				spin_unlock(&parent->d_lock);
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999
		}
		spin_unlock(&dentry->d_lock);
	}
1000
	spin_unlock(&inode->i_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1001
}
1002
EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds's avatar
Linus Torvalds committed
1003

1004 1005
/*
 * Lock a dentry from shrink list.
1006 1007
 * Called under rcu_read_lock() and dentry->d_lock; the former
 * guarantees that nothing we access will be freed under us.
1008
 * Note that dentry is *not* protected from concurrent dentry_kill(),
1009 1010
 * d_delete(), etc.
 *
1011 1012 1013 1014 1015
 * Return false if dentry has been disrupted or grabbed, leaving
 * the caller to kick it off-list.  Otherwise, return true and have
 * that dentry's inode and parent both locked.
 */
static bool shrink_lock_dentry(struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
1016
{
1017 1018
	struct inode *inode;
	struct dentry *parent;
1019

1020 1021 1022 1023 1024 1025 1026
	if (dentry->d_lockref.count)
		return false;

	inode = dentry->d_inode;
	if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
		spin_unlock(&dentry->d_lock);
		spin_lock(&inode->i_lock);
1027
		spin_lock(&dentry->d_lock);
1028 1029 1030 1031 1032 1033
		if (unlikely(dentry->d_lockref.count))
			goto out;
		/* changed inode means that somebody had grabbed it */
		if (unlikely(inode != dentry->d_inode))
			goto out;
	}
1034

1035 1036 1037
	parent = dentry->d_parent;
	if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
		return true;
1038

1039 1040 1041 1042 1043 1044 1045 1046
	spin_unlock(&dentry->d_lock);
	spin_lock(&parent->d_lock);
	if (unlikely(parent != dentry->d_parent)) {
		spin_unlock(&parent->d_lock);
		spin_lock(&dentry->d_lock);
		goto out;
	}
	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1047
	if (likely(!dentry->d_lockref.count))
1048 1049 1050 1051 1052 1053 1054
		return true;
	spin_unlock(&parent->d_lock);
out:
	if (inode)
		spin_unlock(&inode->i_lock);
	return false;
}
1055

1056 1057 1058 1059
static void shrink_dentry_list(struct list_head *list)
{
	while (!list_empty(list)) {
		struct dentry *dentry, *parent;
1060

1061 1062
		dentry = list_entry(list->prev, struct dentry, d_lru);
		spin_lock(&dentry->d_lock);
1063
		rcu_read_lock();
1064 1065
		if (!shrink_lock_dentry(dentry)) {
			bool can_free = false;
1066
			rcu_read_unlock();
1067 1068 1069
			d_shrink_del(dentry);
			if (dentry->d_lockref.count < 0)
				can_free = dentry->d_flags & DCACHE_MAY_FREE;
1070 1071 1072 1073 1074
			spin_unlock(&dentry->d_lock);
			if (can_free)
				dentry_free(dentry);
			continue;
		}
1075
		rcu_read_unlock();
1076 1077
		d_shrink_del(dentry);
		parent = dentry->d_parent;
1078
		__dentry_kill(dentry);
1079 1080
		if (parent == dentry)
			continue;
Al Viro's avatar
Al Viro committed
1081 1082 1083 1084 1085 1086 1087
		/*
		 * We need to prune ancestors too. This is necessary to prevent
		 * quadratic behavior of shrink_dcache_parent(), but is also
		 * expected to be beneficial in reducing dentry cache
		 * fragmentation.
		 */
		dentry = parent;
1088 1089
		while (dentry && !lockref_put_or_lock(&dentry->d_lockref))
			dentry = dentry_kill(dentry);
1090
	}
1091 1092
}

1093 1094
static enum lru_status dentry_lru_isolate(struct list_head *item,
		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
{
	struct list_head *freeable = arg;
	struct dentry	*dentry = container_of(item, struct dentry, d_lru);


	/*
	 * we are inverting the lru lock/dentry->d_lock here,
	 * so use a trylock. If we fail to get the lock, just skip
	 * it
	 */
	if (!spin_trylock(&dentry->d_lock))
		return LRU_SKIP;

	/*
	 * Referenced dentries are still in use. If they have active
	 * counts, just remove them from the LRU. Otherwise give them
	 * another pass through the LRU.
	 */
	if (dentry->d_lockref.count) {
1114
		d_lru_isolate(lru, dentry);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		spin_unlock(&dentry->d_lock);
		return LRU_REMOVED;
	}

	if (dentry->d_flags & DCACHE_REFERENCED) {
		dentry->d_flags &= ~DCACHE_REFERENCED;
		spin_unlock(&dentry->d_lock);

		/*
		 * The list move itself will be made by the common LRU code. At
		 * this point, we've dropped the dentry->d_lock but keep the
		 * lru lock. This is safe to do, since every list movement is
		 * protected by the lru lock even if both locks are held.
		 *
		 * This is guaranteed by the fact that all LRU management
		 * functions are intermediated by the LRU API calls like
		 * list_lru_add and list_lru_del. List movement in this file
		 * only ever occur through this functions or through callbacks
		 * like this one, that are called from the LRU API.
		 *
		 * The only exceptions to this are functions like
		 * shrink_dentry_list, and code that first checks for the
		 * DCACHE_SHRINK_LIST flag.  Those are guaranteed to be
		 * operating only with stack provided lists after they are
		 * properly isolated from the main list.  It is thus, always a
		 * local access.
		 */
		return LRU_ROTATE;
	}

1145
	d_lru_shrink_move(lru, dentry, freeable);
1146 1147 1148 1149 1150
	spin_unlock(&dentry->d_lock);

	return LRU_REMOVED;
}

1151
/**
1152 1153
 * prune_dcache_sb - shrink the dcache
 * @sb: superblock
1154
 * @sc: shrink control, passed to list_lru_shrink_walk()
1155
 *
1156 1157
 * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
 * is done when we need more memory and called from the superblock shrinker
1158
 * function.
1159
 *
1160 1161
 * This function may fail to free any resources if all the dentries are in
 * use.
1162
 */
1163
long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
1164
{
1165 1166
	LIST_HEAD(dispose);
	long freed;
1167

1168 1169
	freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
				     dentry_lru_isolate, &dispose);
1170
	shrink_dentry_list(&dispose);
1171
	return freed;
1172
}
Nick Piggin's avatar
Nick Piggin committed
1173

1174
static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1175
		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1176
{
1177 1178
	struct list_head *freeable = arg;
	struct dentry	*dentry = container_of(item, struct dentry, d_lru);
1179

1180 1181 1182 1183 1184 1185 1186 1187
	/*
	 * we are inverting the lru lock/dentry->d_lock here,
	 * so use a trylock. If we fail to get the lock, just skip
	 * it
	 */
	if (!spin_trylock(&dentry->d_lock))
		return LRU_SKIP;

1188
	d_lru_shrink_move(lru, dentry, freeable);
1189
	spin_unlock(&dentry->d_lock);
1190

1191
	return LRU_REMOVED;
1192 1193
}

1194

Linus Torvalds's avatar
Linus Torvalds committed
1195 1196 1197 1198
/**
 * shrink_dcache_sb - shrink dcache for a superblock
 * @sb: superblock
 *
1199 1200
 * Shrink the dcache for the specified super block. This is used to free
 * the dcache before unmounting a file system.
Linus Torvalds's avatar
Linus Torvalds committed
1201
 */
1202
void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds's avatar
Linus Torvalds committed
1203
{
1204 1205 1206 1207 1208 1209
	long freed;

	do {
		LIST_HEAD(dispose);

		freed = list_lru_walk(&sb->s_dentry_lru,
1210
			dentry_lru_isolate_shrink, &dispose, 1024);
1211

1212 1213
		this_cpu_sub(nr_dentry_unused, freed);
		shrink_dentry_list(&dispose);
1214
	} while (list_lru_count(&sb->s_dentry_lru) > 0);
Linus Torvalds's avatar
Linus Torvalds committed
1215
}
1216
EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds's avatar
Linus Torvalds committed
1217

Miklos Szeredi's avatar
Miklos Szeredi committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
/**
 * enum d_walk_ret - action to talke during tree walk
 * @D_WALK_CONTINUE:	contrinue walk
 * @D_WALK_QUIT:	quit walk
 * @D_WALK_NORETRY:	quit when retry is needed
 * @D_WALK_SKIP:	skip this dentry and its children
 */
enum d_walk_ret {
	D_WALK_CONTINUE,
	D_WALK_QUIT,
	D_WALK_NORETRY,
	D_WALK_SKIP,
};
1231

Linus Torvalds's avatar
Linus Torvalds committed
1232
/**
Miklos Szeredi's avatar
Miklos Szeredi committed
1233 1234 1235 1236
 * d_walk - walk the dentry tree
 * @parent:	start of walk
 * @data:	data passed to @enter() and @finish()
 * @enter:	callback when first entering the dentry
Linus Torvalds's avatar
Linus Torvalds committed
1237
 *
Al Viro's avatar
Al Viro committed