1415 lines · 36.8 KB · C
RawDownload
/* * linux/fs/inode.c * * (C) 1997 Linus Torvalds */ #include <linux/fs.h>#include <linux/mm.h>#include <linux/dcache.h>#include <linux/init.h>#include <linux/quotaops.h>#include <linux/slab.h>#include <linux/writeback.h>#include <linux/module.h>#include <linux/backing-dev.h>#include <linux/wait.h>#include <linux/hash.h>#include <linux/swap.h>#include <linux/security.h>#include <linux/pagemap.h>#include <linux/cdev.h>#include <linux/bootmem.h>#include <linux/inotify.h>#include <linux/mount.h> /* * This is needed for the following functions: *  - inode_has_buffers *  - invalidate_inode_buffers *  - invalidate_bdev * * FIXME: remove all knowledge of the buffer layer from this file */#include <linux/buffer_head.h> /* * New inode.c implementation. * * This implementation has the basic premise of trying * to be extremely low-overhead and SMP-safe, yet be * simple enough to be "obviously correct". * * Famous last words. */ /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */ /* #define INODE_PARANOIA 1 *//* #define INODE_DEBUG 1 */ /* * Inode lookup is no longer as critical as it used to be: * most of the lookups are going to be through the dcache. */#define I_HASHBITS	i_hash_shift#define I_HASHMASK	i_hash_mask static unsigned int i_hash_mask __read_mostly;static unsigned int i_hash_shift __read_mostly; /* * Each inode can be on two separate lists. One is * the hash list of the inode, used for lookups. The * other linked list is the "type" list: *  "in_use" - valid inode, i_count > 0, i_nlink > 0 *  "dirty"  - as "in_use" but also dirty *  "unused" - valid inode, i_count = 0 * * A "dirty" list is maintained for each super block, * allowing for low-overhead inode sync() operations. */ LIST_HEAD(inode_in_use);LIST_HEAD(inode_unused);static struct hlist_head *inode_hashtable __read_mostly; /* * A simple spinlock to protect the list manipulations. * * NOTE! You also have to own the lock if you change * the i_state of an inode while it is in use.. */DEFINE_SPINLOCK(inode_lock); /* * iprune_mutex provides exclusion between the kswapd or try_to_free_pages * icache shrinking path, and the umount path.  Without this exclusion, * by the time prune_icache calls iput for the inode whose pages it has * been invalidating, or by the time it calls clear_inode & destroy_inode * from its final dispose_list, the struct super_block they refer to * (for inode->i_sb->s_op) may already have been freed and reused. */static DEFINE_MUTEX(iprune_mutex); /* * Statistics gathering.. */struct inodes_stat_t inodes_stat; static struct kmem_cache * inode_cachep __read_mostly; static struct inode *alloc_inode(struct super_block *sb){	static const struct address_space_operations empty_aops;	static struct inode_operations empty_iops;	static const struct file_operations empty_fops;	struct inode *inode; 	if (sb->s_op->alloc_inode)		inode = sb->s_op->alloc_inode(sb);	else		inode = (struct inode *) kmem_cache_alloc(inode_cachep, GFP_KERNEL); 	if (inode) {		struct address_space * const mapping = &inode->i_data; 		inode->i_sb = sb;		inode->i_blkbits = sb->s_blocksize_bits;		inode->i_flags = 0;		atomic_set(&inode->i_count, 1);		inode->i_op = &empty_iops;		inode->i_fop = &empty_fops;		inode->i_nlink = 1;		atomic_set(&inode->i_writecount, 0);		inode->i_size = 0;		inode->i_blocks = 0;		inode->i_bytes = 0;		inode->i_generation = 0;#ifdef CONFIG_QUOTA		memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));#endif		inode->i_pipe = NULL;		inode->i_bdev = NULL;		inode->i_cdev = NULL;		inode->i_rdev = 0;		inode->dirtied_when = 0;		if (security_inode_alloc(inode)) {			if (inode->i_sb->s_op->destroy_inode)				inode->i_sb->s_op->destroy_inode(inode);			else				kmem_cache_free(inode_cachep, (inode));			return NULL;		} 		mapping->a_ops = &empty_aops; 		mapping->host = inode;		mapping->flags = 0;		mapping_set_gfp_mask(mapping, GFP_HIGHUSER);		mapping->assoc_mapping = NULL;		mapping->backing_dev_info = &default_backing_dev_info; 		/*		 * If the block_device provides a backing_dev_info for client		 * inodes then use that.  Otherwise the inode share the bdev's		 * backing_dev_info.		 */		if (sb->s_bdev) {			struct backing_dev_info *bdi; 			bdi = sb->s_bdev->bd_inode_backing_dev_info;			if (!bdi)				bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;			mapping->backing_dev_info = bdi;		}		inode->i_private = NULL;		inode->i_mapping = mapping;	}	return inode;} void destroy_inode(struct inode *inode) {	BUG_ON(inode_has_buffers(inode));	security_inode_free(inode);	if (inode->i_sb->s_op->destroy_inode)		inode->i_sb->s_op->destroy_inode(inode);	else		kmem_cache_free(inode_cachep, (inode));}  /* * These are initializations that only need to be done * once, because the fields are idempotent across use * of the inode, so let the slab aware of that. */void inode_init_once(struct inode *inode){	memset(inode, 0, sizeof(*inode));	INIT_HLIST_NODE(&inode->i_hash);	INIT_LIST_HEAD(&inode->i_dentry);	INIT_LIST_HEAD(&inode->i_devices);	mutex_init(&inode->i_mutex);	init_rwsem(&inode->i_alloc_sem);	INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);	rwlock_init(&inode->i_data.tree_lock);	spin_lock_init(&inode->i_data.i_mmap_lock);	INIT_LIST_HEAD(&inode->i_data.private_list);	spin_lock_init(&inode->i_data.private_lock);	INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);	INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);	spin_lock_init(&inode->i_lock);	i_size_ordered_init(inode);#ifdef CONFIG_INOTIFY	INIT_LIST_HEAD(&inode->inotify_watches);	mutex_init(&inode->inotify_mutex);#endif} EXPORT_SYMBOL(inode_init_once); static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags){	struct inode * inode = (struct inode *) foo; 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==	    SLAB_CTOR_CONSTRUCTOR)		inode_init_once(inode);} /* * inode_lock must be held */void __iget(struct inode * inode){	if (atomic_read(&inode->i_count)) {		atomic_inc(&inode->i_count);		return;	}	atomic_inc(&inode->i_count);	if (!(inode->i_state & (I_DIRTY|I_LOCK)))		list_move(&inode->i_list, &inode_in_use);	inodes_stat.nr_unused--;} /** * clear_inode - clear an inode * @inode: inode to clear * * This is called by the filesystem to tell us * that the inode is no longer useful. We just * terminate it with extreme prejudice. */void clear_inode(struct inode *inode){	might_sleep();	invalidate_inode_buffers(inode);       	BUG_ON(inode->i_data.nrpages);	BUG_ON(!(inode->i_state & I_FREEING));	BUG_ON(inode->i_state & I_CLEAR);	wait_on_inode(inode);	DQUOT_DROP(inode);	if (inode->i_sb && inode->i_sb->s_op->clear_inode)		inode->i_sb->s_op->clear_inode(inode);	if (S_ISBLK(inode->i_mode) && inode->i_bdev)		bd_forget(inode);	if (S_ISCHR(inode->i_mode) && inode->i_cdev)		cd_forget(inode);	inode->i_state = I_CLEAR;} EXPORT_SYMBOL(clear_inode); /* * dispose_list - dispose of the contents of a local list * @head: the head of the list to free * * Dispose-list gets a local list with local inodes in it, so it doesn't * need to worry about list corruption and SMP locks. */static void dispose_list(struct list_head *head){	int nr_disposed = 0; 	while (!list_empty(head)) {		struct inode *inode; 		inode = list_entry(head->next, struct inode, i_list);		list_del(&inode->i_list); 		if (inode->i_data.nrpages)			truncate_inode_pages(&inode->i_data, 0);		clear_inode(inode); 		spin_lock(&inode_lock);		hlist_del_init(&inode->i_hash);		list_del_init(&inode->i_sb_list);		spin_unlock(&inode_lock); 		wake_up_inode(inode);		destroy_inode(inode);		nr_disposed++;	}	spin_lock(&inode_lock);	inodes_stat.nr_inodes -= nr_disposed;	spin_unlock(&inode_lock);} /* * Invalidate all inodes for a device. */static int invalidate_list(struct list_head *head, struct list_head *dispose){	struct list_head *next;	int busy = 0, count = 0; 	next = head->next;	for (;;) {		struct list_head * tmp = next;		struct inode * inode; 		/*		 * We can reschedule here without worrying about the list's		 * consistency because the per-sb list of inodes must not		 * change during umount anymore, and because iprune_mutex keeps		 * shrink_icache_memory() away.		 */		cond_resched_lock(&inode_lock); 		next = next->next;		if (tmp == head)			break;		inode = list_entry(tmp, struct inode, i_sb_list);		invalidate_inode_buffers(inode);		if (!atomic_read(&inode->i_count)) {			list_move(&inode->i_list, dispose);			inode->i_state |= I_FREEING;			count++;			continue;		}		busy = 1;	}	/* only unused inodes may be cached with i_count zero */	inodes_stat.nr_unused -= count;	return busy;} /** *	invalidate_inodes	- discard the inodes on a device *	@sb: superblock * *	Discard all of the inodes for a given superblock. If the discard *	fails because there are busy inodes then a non zero value is returned. *	If the discard is successful all the inodes have been discarded. */int invalidate_inodes(struct super_block * sb){	int busy;	LIST_HEAD(throw_away); 	mutex_lock(&iprune_mutex);	spin_lock(&inode_lock);	inotify_unmount_inodes(&sb->s_inodes);	busy = invalidate_list(&sb->s_inodes, &throw_away);	spin_unlock(&inode_lock); 	dispose_list(&throw_away);	mutex_unlock(&iprune_mutex); 	return busy;} EXPORT_SYMBOL(invalidate_inodes); static int can_unuse(struct inode *inode){	if (inode->i_state)		return 0;	if (inode_has_buffers(inode))		return 0;	if (atomic_read(&inode->i_count))		return 0;	if (inode->i_data.nrpages)		return 0;	return 1;} /* * Scan `goal' inodes on the unused list for freeable ones. They are moved to * a temporary list and then are freed outside inode_lock by dispose_list(). * * Any inodes which are pinned purely because of attached pagecache have their * pagecache removed.  We expect the final iput() on that inode to add it to * the front of the inode_unused list.  So look for it there and if the * inode is still freeable, proceed.  The right inode is found 99.9% of the * time in testing on a 4-way. * * If the inode has metadata buffers attached to mapping->private_list then * try to remove them. */static void prune_icache(int nr_to_scan){	LIST_HEAD(freeable);	int nr_pruned = 0;	int nr_scanned;	unsigned long reap = 0; 	mutex_lock(&iprune_mutex);	spin_lock(&inode_lock);	for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {		struct inode *inode; 		if (list_empty(&inode_unused))			break; 		inode = list_entry(inode_unused.prev, struct inode, i_list); 		if (inode->i_state || atomic_read(&inode->i_count)) {			list_move(&inode->i_list, &inode_unused);			continue;		}		if (inode_has_buffers(inode) || inode->i_data.nrpages) {			__iget(inode);			spin_unlock(&inode_lock);			if (remove_inode_buffers(inode))				reap += invalidate_mapping_pages(&inode->i_data,								0, -1);			iput(inode);			spin_lock(&inode_lock); 			if (inode != list_entry(inode_unused.next,						struct inode, i_list))				continue;	/* wrong inode or list_empty */			if (!can_unuse(inode))				continue;		}		list_move(&inode->i_list, &freeable);		inode->i_state |= I_FREEING;		nr_pruned++;	}	inodes_stat.nr_unused -= nr_pruned;	if (current_is_kswapd())		__count_vm_events(KSWAPD_INODESTEAL, reap);	else		__count_vm_events(PGINODESTEAL, reap);	spin_unlock(&inode_lock); 	dispose_list(&freeable);	mutex_unlock(&iprune_mutex);} /* * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here, * "unused" means that no dentries are referring to the inodes: the files are * not open and the dcache references to those inodes have already been * reclaimed. * * This function is passed the number of inodes to scan, and it returns the * total number of remaining possibly-reclaimable inodes. */static int shrink_icache_memory(int nr, gfp_t gfp_mask){	if (nr) {		/*		 * Nasty deadlock avoidance.  We may hold various FS locks,		 * and we don't want to recurse into the FS that called us		 * in clear_inode() and friends..	 	 */		if (!(gfp_mask & __GFP_FS))			return -1;		prune_icache(nr);	}	return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;} static void __wait_on_freeing_inode(struct inode *inode);/* * Called with the inode lock held. * NOTE: we are not increasing the inode-refcount, you must call __iget() * by hand after calling find_inode now! This simplifies iunique and won't * add any additional branch in the common code. */static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data){	struct hlist_node *node;	struct inode * inode = NULL; repeat:	hlist_for_each (node, head) { 		inode = hlist_entry(node, struct inode, i_hash);		if (inode->i_sb != sb)			continue;		if (!test(inode, data))			continue;		if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {			__wait_on_freeing_inode(inode);			goto repeat;		}		break;	}	return node ? inode : NULL;} /* * find_inode_fast is the fast path version of find_inode, see the comment at * iget_locked for details. */static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino){

Lines 1–500 of 1415.