1901 lines · 48.7 KB · C
RawDownload
/* * (C) 1997 Linus Torvalds * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation) */#include <linux/export.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/backing-dev.h>#include <linux/hash.h>#include <linux/swap.h>#include <linux/security.h>#include <linux/cdev.h>#include <linux/bootmem.h>#include <linux/fsnotify.h>#include <linux/mount.h>#include <linux/posix_acl.h>#include <linux/prefetch.h>#include <linux/buffer_head.h> /* for inode_has_buffers */#include <linux/ratelimit.h>#include <linux/list_lru.h>#include "internal.h" /* * Inode locking rules: * * inode->i_lock protects: *   inode->i_state, inode->i_hash, __iget() * Inode LRU list locks protect: *   inode->i_sb->s_inode_lru, inode->i_lru * inode_sb_list_lock protects: *   sb->s_inodes, inode->i_sb_list * bdi->wb.list_lock protects: *   bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list * inode_hash_lock protects: *   inode_hashtable, inode->i_hash * * Lock ordering: * * inode_sb_list_lock *   inode->i_lock *     Inode LRU list locks * * bdi->wb.list_lock *   inode->i_lock * * inode_hash_lock *   inode_sb_list_lock *   inode->i_lock * * iunique_lock *   inode_hash_lock */ static unsigned int i_hash_mask __read_mostly;static unsigned int i_hash_shift __read_mostly;static struct hlist_head *inode_hashtable __read_mostly;static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock); /* * Empty aops. Can be used for the cases where the user does not * define any of the address_space operations. */const struct address_space_operations empty_aops = {};EXPORT_SYMBOL(empty_aops); /* * Statistics gathering.. */struct inodes_stat_t inodes_stat; static DEFINE_PER_CPU(unsigned long, nr_inodes);static DEFINE_PER_CPU(unsigned long, nr_unused); static struct kmem_cache *inode_cachep __read_mostly; static long get_nr_inodes(void){	int i;	long sum = 0;	for_each_possible_cpu(i)		sum += per_cpu(nr_inodes, i);	return sum < 0 ? 0 : sum;} static inline long get_nr_inodes_unused(void){	int i;	long sum = 0;	for_each_possible_cpu(i)		sum += per_cpu(nr_unused, i);	return sum < 0 ? 0 : sum;} long get_nr_dirty_inodes(void){	/* not actually dirty inodes, but a wild approximation */	long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();	return nr_dirty > 0 ? nr_dirty : 0;} /* * Handle nr_inode sysctl */#ifdef CONFIG_SYSCTLint proc_nr_inodes(ctl_table *table, int write,		   void __user *buffer, size_t *lenp, loff_t *ppos){	inodes_stat.nr_inodes = get_nr_inodes();	inodes_stat.nr_unused = get_nr_inodes_unused();	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);}#endif /** * inode_init_always - perform inode structure intialisation * @sb: superblock inode belongs to * @inode: inode to initialise * * These are initializations that need to be done on every inode * allocation as the fields are not initialised by slab allocation. */int inode_init_always(struct super_block *sb, struct inode *inode){	static const struct inode_operations empty_iops;	static const struct file_operations empty_fops;	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;	inode->i_opflags = 0;	i_uid_write(inode, 0);	i_gid_write(inode, 0);	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))		goto out;	spin_lock_init(&inode->i_lock);	lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); 	mutex_init(&inode->i_mutex);	lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key); 	atomic_set(&inode->i_dio_count, 0); 	mapping->a_ops = &empty_aops;	mapping->host = inode;	mapping->flags = 0;	mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);	mapping->private_data = NULL;	mapping->backing_dev_info = &default_backing_dev_info;	mapping->writeback_index = 0; 	/*	 * 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->i_mapping->backing_dev_info;		mapping->backing_dev_info = bdi;	}	inode->i_private = NULL;	inode->i_mapping = mapping;	INIT_HLIST_HEAD(&inode->i_dentry);	/* buggered by rcu freeing */#ifdef CONFIG_FS_POSIX_ACL	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;#endif #ifdef CONFIG_FSNOTIFY	inode->i_fsnotify_mask = 0;#endif 	this_cpu_inc(nr_inodes); 	return 0;out:	return -ENOMEM;}EXPORT_SYMBOL(inode_init_always); static struct inode *alloc_inode(struct super_block *sb){	struct inode *inode; 	if (sb->s_op->alloc_inode)		inode = sb->s_op->alloc_inode(sb);	else		inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL); 	if (!inode)		return NULL; 	if (unlikely(inode_init_always(sb, 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;	} 	return inode;} void free_inode_nonrcu(struct inode *inode){	kmem_cache_free(inode_cachep, inode);}EXPORT_SYMBOL(free_inode_nonrcu); void __destroy_inode(struct inode *inode){	BUG_ON(inode_has_buffers(inode));	security_inode_free(inode);	fsnotify_inode_delete(inode);	if (!inode->i_nlink) {		WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);		atomic_long_dec(&inode->i_sb->s_remove_count);	} #ifdef CONFIG_FS_POSIX_ACL	if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)		posix_acl_release(inode->i_acl);	if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)		posix_acl_release(inode->i_default_acl);#endif	this_cpu_dec(nr_inodes);}EXPORT_SYMBOL(__destroy_inode); static void i_callback(struct rcu_head *head){	struct inode *inode = container_of(head, struct inode, i_rcu);	kmem_cache_free(inode_cachep, inode);} static void destroy_inode(struct inode *inode){	BUG_ON(!list_empty(&inode->i_lru));	__destroy_inode(inode);	if (inode->i_sb->s_op->destroy_inode)		inode->i_sb->s_op->destroy_inode(inode);	else		call_rcu(&inode->i_rcu, i_callback);} /** * drop_nlink - directly drop an inode's link count * @inode: inode * * This is a low-level filesystem helper to replace any * direct filesystem manipulation of i_nlink.  In cases * where we are attempting to track writes to the * filesystem, a decrement to zero means an imminent * write when the file is truncated and actually unlinked * on the filesystem. */void drop_nlink(struct inode *inode){	WARN_ON(inode->i_nlink == 0);	inode->__i_nlink--;	if (!inode->i_nlink)		atomic_long_inc(&inode->i_sb->s_remove_count);}EXPORT_SYMBOL(drop_nlink); /** * clear_nlink - directly zero an inode's link count * @inode: inode * * This is a low-level filesystem helper to replace any * direct filesystem manipulation of i_nlink.  See * drop_nlink() for why we care about i_nlink hitting zero. */void clear_nlink(struct inode *inode){	if (inode->i_nlink) {		inode->__i_nlink = 0;		atomic_long_inc(&inode->i_sb->s_remove_count);	}}EXPORT_SYMBOL(clear_nlink); /** * set_nlink - directly set an inode's link count * @inode: inode * @nlink: new nlink (should be non-zero) * * This is a low-level filesystem helper to replace any * direct filesystem manipulation of i_nlink. */void set_nlink(struct inode *inode, unsigned int nlink){	if (!nlink) {		clear_nlink(inode);	} else {		/* Yes, some filesystems do change nlink from zero to one */		if (inode->i_nlink == 0)			atomic_long_dec(&inode->i_sb->s_remove_count); 		inode->__i_nlink = nlink;	}}EXPORT_SYMBOL(set_nlink); /** * inc_nlink - directly increment an inode's link count * @inode: inode * * This is a low-level filesystem helper to replace any * direct filesystem manipulation of i_nlink.  Currently, * it is only here for parity with dec_nlink(). */void inc_nlink(struct inode *inode){	if (unlikely(inode->i_nlink == 0)) {		WARN_ON(!(inode->i_state & I_LINKABLE));		atomic_long_dec(&inode->i_sb->s_remove_count);	} 	inode->__i_nlink++;}EXPORT_SYMBOL(inc_nlink); void address_space_init_once(struct address_space *mapping){	memset(mapping, 0, sizeof(*mapping));	INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);	spin_lock_init(&mapping->tree_lock);	mutex_init(&mapping->i_mmap_mutex);	INIT_LIST_HEAD(&mapping->private_list);	spin_lock_init(&mapping->private_lock);	mapping->i_mmap = RB_ROOT;	INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);}EXPORT_SYMBOL(address_space_init_once); /* * 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_devices);	INIT_LIST_HEAD(&inode->i_wb_list);	INIT_LIST_HEAD(&inode->i_lru);	address_space_init_once(&inode->i_data);	i_size_ordered_init(inode);#ifdef CONFIG_FSNOTIFY	INIT_HLIST_HEAD(&inode->i_fsnotify_marks);#endif}EXPORT_SYMBOL(inode_init_once); static void init_once(void *foo){	struct inode *inode = (struct inode *) foo; 	inode_init_once(inode);} /* * inode->i_lock must be held */void __iget(struct inode *inode){	atomic_inc(&inode->i_count);} /* * get additional reference to inode; caller must already hold one. */void ihold(struct inode *inode){	WARN_ON(atomic_inc_return(&inode->i_count) < 2);}EXPORT_SYMBOL(ihold); static void inode_lru_list_add(struct inode *inode){	if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))		this_cpu_inc(nr_unused);} /* * Add inode to LRU if needed (inode is unused and clean). * * Needs inode->i_lock held. */void inode_add_lru(struct inode *inode){	if (!(inode->i_state & (I_DIRTY | I_SYNC | I_FREEING | I_WILL_FREE)) &&	    !atomic_read(&inode->i_count) && inode->i_sb->s_flags & MS_ACTIVE)		inode_lru_list_add(inode);}  static void inode_lru_list_del(struct inode *inode){ 	if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))		this_cpu_dec(nr_unused);} /** * inode_sb_list_add - add inode to the superblock list of inodes * @inode: inode to add */void inode_sb_list_add(struct inode *inode){	spin_lock(&inode_sb_list_lock);	list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);	spin_unlock(&inode_sb_list_lock);}EXPORT_SYMBOL_GPL(inode_sb_list_add); static inline void inode_sb_list_del(struct inode *inode){	if (!list_empty(&inode->i_sb_list)) {		spin_lock(&inode_sb_list_lock);		list_del_init(&inode->i_sb_list);		spin_unlock(&inode_sb_list_lock);	}} static unsigned long hash(struct super_block *sb, unsigned long hashval){	unsigned long tmp; 	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /			L1_CACHE_BYTES;	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);	return tmp & i_hash_mask;} /** *	__insert_inode_hash - hash an inode *	@inode: unhashed inode *	@hashval: unsigned long value used to locate this object in the *		inode_hashtable. * *	Add an inode to the inode hash for this superblock. */void __insert_inode_hash(struct inode *inode, unsigned long hashval){	struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); 	spin_lock(&inode_hash_lock);	spin_lock(&inode->i_lock);	hlist_add_head(&inode->i_hash, b);	spin_unlock(&inode->i_lock);	spin_unlock(&inode_hash_lock);}EXPORT_SYMBOL(__insert_inode_hash); /** *	__remove_inode_hash - remove an inode from the hash *	@inode: inode to unhash * *	Remove an inode from the superblock. */void __remove_inode_hash(struct inode *inode){	spin_lock(&inode_hash_lock);	spin_lock(&inode->i_lock);	hlist_del_init(&inode->i_hash);	spin_unlock(&inode->i_lock);	spin_unlock(&inode_hash_lock);}EXPORT_SYMBOL(__remove_inode_hash); void clear_inode(struct inode *inode){	might_sleep();	/*	 * We have to cycle tree_lock here because reclaim can be still in the

Lines 1–500 of 1901.