2141 lines · 53.8 KB · C
RawDownload
/* * 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. */ #include <linux/syscalls.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/fs.h>#include <linux/fsnotify.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/smp_lock.h>#include <linux/hash.h>#include <linux/cache.h>#include <linux/module.h>#include <linux/mount.h>#include <linux/file.h>#include <asm/uaccess.h>#include <linux/security.h>#include <linux/seqlock.h>#include <linux/swap.h>#include <linux/bootmem.h>#include "internal.h"  int sysctl_vfs_cache_pressure __read_mostly = 100;EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);  __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); EXPORT_SYMBOL(dcache_lock); static struct kmem_cache *dentry_cache __read_mostly; #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname)) /* * 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. */#define D_HASHBITS     d_hash_shift#define D_HASHMASK     d_hash_mask static unsigned int d_hash_mask __read_mostly;static unsigned int d_hash_shift __read_mostly;static struct hlist_head *dentry_hashtable __read_mostly;static LIST_HEAD(dentry_unused); /* Statistics gathering. */struct dentry_stat_t dentry_stat = {	.age_limit = 45,}; static void __d_free(struct dentry *dentry){	if (dname_external(dentry))		kfree(dentry->d_name.name);	kmem_cache_free(dentry_cache, dentry); } static void d_callback(struct rcu_head *head){	struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);	__d_free(dentry);} /* * no dcache_lock, please.  The caller must decrement dentry_stat.nr_dentry * inside dcache_lock. */static void d_free(struct dentry *dentry){	if (dentry->d_op && dentry->d_op->d_release)		dentry->d_op->d_release(dentry);	/* if dentry was never inserted into hash, immediate free is OK */	if (dentry->d_hash.pprev == NULL)		__d_free(dentry);	else		call_rcu(&dentry->d_u.d_rcu, d_callback);} /* * Release the dentry's inode, using the filesystem * d_iput() operation if defined. * Called with dcache_lock and per dentry lock held, drops both. */static void dentry_iput(struct dentry * dentry){	struct inode *inode = dentry->d_inode;	if (inode) {		dentry->d_inode = NULL;		list_del_init(&dentry->d_alias);		spin_unlock(&dentry->d_lock);		spin_unlock(&dcache_lock);		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);	} else {		spin_unlock(&dentry->d_lock);		spin_unlock(&dcache_lock);	}} /*  * 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. * * no dcache lock, please. */ void dput(struct dentry *dentry){	if (!dentry)		return; repeat:	if (atomic_read(&dentry->d_count) == 1)		might_sleep();	if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))		return; 	spin_lock(&dentry->d_lock);	if (atomic_read(&dentry->d_count)) {		spin_unlock(&dentry->d_lock);		spin_unlock(&dcache_lock);		return;	} 	/*	 * AV: ->d_delete() is _NOT_ allowed to block now.	 */	if (dentry->d_op && dentry->d_op->d_delete) {		if (dentry->d_op->d_delete(dentry))			goto unhash_it;	}	/* Unreachable? Get rid of it */ 	if (d_unhashed(dentry))		goto kill_it;  	if (list_empty(&dentry->d_lru)) {  		dentry->d_flags |= DCACHE_REFERENCED;  		list_add(&dentry->d_lru, &dentry_unused);  		dentry_stat.nr_unused++;  	} 	spin_unlock(&dentry->d_lock);	spin_unlock(&dcache_lock);	return; unhash_it:	__d_drop(dentry); kill_it: {		struct dentry *parent; 		/* If dentry was on d_lru list		 * delete it from there		 */  		if (!list_empty(&dentry->d_lru)) {  			list_del(&dentry->d_lru);  			dentry_stat.nr_unused--;  		}  		list_del(&dentry->d_u.d_child);		dentry_stat.nr_dentry--;	/* For d_free, below */		/*drops the locks, at that point nobody can reach this dentry */		dentry_iput(dentry);		parent = dentry->d_parent;		d_free(dentry);		if (dentry == parent)			return;		dentry = parent;		goto repeat;	}} /** * d_invalidate - invalidate a dentry * @dentry: dentry to invalidate * * Try to invalidate the dentry if it turns out to be * possible. If there are other dentries that can be * reached through this one we can't delete it and we * return -EBUSY. On success we return 0. * * no dcache lock. */ int d_invalidate(struct dentry * dentry){	/*	 * If it's already been dropped, return OK.	 */	spin_lock(&dcache_lock);	if (d_unhashed(dentry)) {		spin_unlock(&dcache_lock);		return 0;	}	/*	 * Check whether to do a partial shrink_dcache	 * to get rid of unused child entries.	 */	if (!list_empty(&dentry->d_subdirs)) {		spin_unlock(&dcache_lock);		shrink_dcache_parent(dentry);		spin_lock(&dcache_lock);	} 	/*	 * Somebody else still using it?	 *	 * If it's a directory, we can't drop it	 * for fear of somebody re-populating it	 * with children (even though dropping it	 * would make it unreachable from the root,	 * we might still populate it if it was a	 * working directory or similar).	 */	spin_lock(&dentry->d_lock);	if (atomic_read(&dentry->d_count) > 1) {		if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {			spin_unlock(&dentry->d_lock);			spin_unlock(&dcache_lock);			return -EBUSY;		}	} 	__d_drop(dentry);	spin_unlock(&dentry->d_lock);	spin_unlock(&dcache_lock);	return 0;} /* This should be called _only_ with dcache_lock held */ static inline struct dentry * __dget_locked(struct dentry *dentry){	atomic_inc(&dentry->d_count);	if (!list_empty(&dentry->d_lru)) {		dentry_stat.nr_unused--;		list_del_init(&dentry->d_lru);	}	return dentry;} struct dentry * dget_locked(struct dentry *dentry){	return __dget_locked(dentry);} /** * d_find_alias - grab a hashed alias of inode * @inode: inode in question * @want_discon:  flag, used by d_splice_alias, to request *          that only a DISCONNECTED alias be returned. * * 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 * of a filesystem. * * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer * any other hashed alias over that one unless @want_discon is set, * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias. */ static struct dentry * __d_find_alias(struct inode *inode, int want_discon){	struct list_head *head, *next, *tmp;	struct dentry *alias, *discon_alias=NULL; 	head = &inode->i_dentry;	next = inode->i_dentry.next;	while (next != head) {		tmp = next;		next = tmp->next;		prefetch(next);		alias = list_entry(tmp, struct dentry, d_alias); 		if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {			if (IS_ROOT(alias) &&			    (alias->d_flags & DCACHE_DISCONNECTED))				discon_alias = alias;			else if (!want_discon) {				__dget_locked(alias);				return alias;			}		}	}	if (discon_alias)		__dget_locked(discon_alias);	return discon_alias;} struct dentry * d_find_alias(struct inode *inode){	struct dentry *de = NULL; 	if (!list_empty(&inode->i_dentry)) {		spin_lock(&dcache_lock);		de = __d_find_alias(inode, 0);		spin_unlock(&dcache_lock);	}	return de;} /* *	Try to kill dentries associated with this inode. * WARNING: you must own a reference to inode. */void d_prune_aliases(struct inode *inode){	struct dentry *dentry;restart:	spin_lock(&dcache_lock);	list_for_each_entry(dentry, &inode->i_dentry, d_alias) {		spin_lock(&dentry->d_lock);		if (!atomic_read(&dentry->d_count)) {			__dget_locked(dentry);			__d_drop(dentry);			spin_unlock(&dentry->d_lock);			spin_unlock(&dcache_lock);			dput(dentry);			goto restart;		}		spin_unlock(&dentry->d_lock);	}	spin_unlock(&dcache_lock);} /* * Throw away a dentry - free the inode, dput the parent.  This requires that * the LRU list has already been removed. * * Called with dcache_lock, drops it and then regains. * Called with dentry->d_lock held, drops it. */static void prune_one_dentry(struct dentry * dentry){	struct dentry * parent; 	__d_drop(dentry);	list_del(&dentry->d_u.d_child);	dentry_stat.nr_dentry--;	/* For d_free, below */	dentry_iput(dentry);	parent = dentry->d_parent;	d_free(dentry);	if (parent != dentry)		dput(parent);	spin_lock(&dcache_lock);} /** * prune_dcache - shrink the dcache * @count: number of entries to try and free * @sb: if given, ignore dentries for other superblocks *         which are being unmounted. * * Shrink the dcache. This is done when we need * more memory, or simply when we need to unmount * something (at which point we need to unuse * all dentries). * * This function may fail to free any resources if * all the dentries are in use. */ static void prune_dcache(int count, struct super_block *sb){	spin_lock(&dcache_lock);	for (; count ; count--) {		struct dentry *dentry;		struct list_head *tmp;		struct rw_semaphore *s_umount; 		cond_resched_lock(&dcache_lock); 		tmp = dentry_unused.prev;		if (sb) {			/* Try to find a dentry for this sb, but don't try			 * too hard, if they aren't near the tail they will			 * be moved down again soon			 */			int skip = count;			while (skip && tmp != &dentry_unused &&			    list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {				skip--;				tmp = tmp->prev;			}		}		if (tmp == &dentry_unused)			break;		list_del_init(tmp);		prefetch(dentry_unused.prev); 		dentry_stat.nr_unused--;		dentry = list_entry(tmp, struct dentry, d_lru);  		spin_lock(&dentry->d_lock);		/*		 * We found an inuse dentry which was not removed from		 * dentry_unused because of laziness during lookup.  Do not free		 * it - just keep it off the dentry_unused list.		 */ 		if (atomic_read(&dentry->d_count)) { 			spin_unlock(&dentry->d_lock);			continue;		}		/* If the dentry was recently referenced, don't free it. */		if (dentry->d_flags & DCACHE_REFERENCED) {			dentry->d_flags &= ~DCACHE_REFERENCED; 			list_add(&dentry->d_lru, &dentry_unused); 			dentry_stat.nr_unused++; 			spin_unlock(&dentry->d_lock);			continue;		}		/*		 * If the dentry is not DCACHED_REFERENCED, it is time		 * to remove it from the dcache, provided the super block is		 * NULL (which means we are trying to reclaim memory)		 * or this dentry belongs to the same super block that		 * we want to shrink.		 */		/*		 * If this dentry is for "my" filesystem, then I can prune it		 * without taking the s_umount lock (I already hold it).		 */		if (sb && dentry->d_sb == sb) {			prune_one_dentry(dentry);			continue;		}		/*		 * ...otherwise we need to be sure this filesystem isn't being		 * unmounted, otherwise we could race with		 * generic_shutdown_super(), and end up holding a reference to		 * an inode while the filesystem is unmounted.		 * So we try to get s_umount, and make sure s_root isn't NULL.		 * (Take a local copy of s_umount to avoid a use-after-free of		 * `dentry').		 */		s_umount = &dentry->d_sb->s_umount;		if (down_read_trylock(s_umount)) {			if (dentry->d_sb->s_root != NULL) {				prune_one_dentry(dentry);				up_read(s_umount);				continue;			}			up_read(s_umount);		}		spin_unlock(&dentry->d_lock);		/*		 * Insert dentry at the head of the list as inserting at the		 * tail leads to a cycle.		 */ 		list_add(&dentry->d_lru, &dentry_unused);		dentry_stat.nr_unused++;	}	spin_unlock(&dcache_lock);} /* * Shrink the dcache for the specified super block.

Lines 1–500 of 2141.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t