| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * (C) 1997 Linus Torvalds | 
| Christoph Hellwig | 4b4563d | 2011-05-27 09:28:01 -0400 | [diff] [blame] | 3 |  * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/fs.h> | 
 | 6 | #include <linux/mm.h> | 
 | 7 | #include <linux/dcache.h> | 
 | 8 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> | 
 | 10 | #include <linux/writeback.h> | 
 | 11 | #include <linux/module.h> | 
 | 12 | #include <linux/backing-dev.h> | 
 | 13 | #include <linux/wait.h> | 
| Nick Piggin | 88e0fbc | 2009-09-22 16:43:50 -0700 | [diff] [blame] | 14 | #include <linux/rwsem.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/hash.h> | 
 | 16 | #include <linux/swap.h> | 
 | 17 | #include <linux/security.h> | 
 | 18 | #include <linux/pagemap.h> | 
 | 19 | #include <linux/cdev.h> | 
 | 20 | #include <linux/bootmem.h> | 
| Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 21 | #include <linux/fsnotify.h> | 
| Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 22 | #include <linux/mount.h> | 
| Arjan van de Ven | efaee19 | 2009-01-06 07:20:54 -0800 | [diff] [blame] | 23 | #include <linux/async.h> | 
| Al Viro | f19d4a8 | 2009-06-08 19:50:45 -0400 | [diff] [blame] | 24 | #include <linux/posix_acl.h> | 
| Heiko Carstens | 9ce6e0b | 2011-05-22 18:54:21 +0200 | [diff] [blame] | 25 | #include <linux/prefetch.h> | 
| Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 26 | #include <linux/ima.h> | 
| Serge E. Hallyn | e795b71 | 2011-03-23 16:43:25 -0700 | [diff] [blame] | 27 | #include <linux/cred.h> | 
| Christoph Hellwig | 4b4563d | 2011-05-27 09:28:01 -0400 | [diff] [blame] | 28 | #include <linux/buffer_head.h> /* for inode_has_buffers */ | 
| Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 29 | #include "internal.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
 | 31 | /* | 
| Christoph Hellwig | 4b4563d | 2011-05-27 09:28:01 -0400 | [diff] [blame] | 32 |  * Inode locking rules: | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 33 |  * | 
 | 34 |  * inode->i_lock protects: | 
 | 35 |  *   inode->i_state, inode->i_hash, __iget() | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 36 |  * inode_lru_lock protects: | 
 | 37 |  *   inode_lru, inode->i_lru | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 38 |  * inode_sb_list_lock protects: | 
 | 39 |  *   sb->s_inodes, inode->i_sb_list | 
| Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 40 |  * inode_wb_list_lock protects: | 
 | 41 |  *   bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 42 |  * inode_hash_lock protects: | 
 | 43 |  *   inode_hashtable, inode->i_hash | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 44 |  * | 
 | 45 |  * Lock ordering: | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 46 |  * | 
 | 47 |  * inode_sb_list_lock | 
 | 48 |  *   inode->i_lock | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 49 |  *     inode_lru_lock | 
| Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 50 |  * | 
 | 51 |  * inode_wb_list_lock | 
 | 52 |  *   inode->i_lock | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 53 |  * | 
 | 54 |  * inode_hash_lock | 
 | 55 |  *   inode_sb_list_lock | 
 | 56 |  *   inode->i_lock | 
 | 57 |  * | 
 | 58 |  * iunique_lock | 
 | 59 |  *   inode_hash_lock | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 60 |  */ | 
 | 61 |  | 
| Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 62 | static unsigned int i_hash_mask __read_mostly; | 
 | 63 | static unsigned int i_hash_shift __read_mostly; | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 64 | static struct hlist_head *inode_hashtable __read_mostly; | 
 | 65 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 67 | static LIST_HEAD(inode_lru); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |  | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 70 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock); | 
| Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 71 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock); | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 72 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* | 
| Christoph Hellwig | bab1d94 | 2011-03-15 21:51:24 +0100 | [diff] [blame] | 74 |  * iprune_sem provides exclusion between the icache shrinking and the | 
 | 75 |  * umount path. | 
| Nick Piggin | 88e0fbc | 2009-09-22 16:43:50 -0700 | [diff] [blame] | 76 |  * | 
| Christoph Hellwig | bab1d94 | 2011-03-15 21:51:24 +0100 | [diff] [blame] | 77 |  * We don't actually need it to protect anything in the umount path, | 
 | 78 |  * but only need to cycle through it to make sure any inode that | 
 | 79 |  * prune_icache took off the LRU list has been fully torn down by the | 
 | 80 |  * time we are past evict_inodes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 |  */ | 
| Nick Piggin | 88e0fbc | 2009-09-22 16:43:50 -0700 | [diff] [blame] | 82 | static DECLARE_RWSEM(iprune_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  | 
 | 84 | /* | 
| Jens Axboe | 7dcda1c | 2011-04-05 23:51:48 +0200 | [diff] [blame] | 85 |  * Empty aops. Can be used for the cases where the user does not | 
 | 86 |  * define any of the address_space operations. | 
 | 87 |  */ | 
 | 88 | const struct address_space_operations empty_aops = { | 
 | 89 | }; | 
 | 90 | EXPORT_SYMBOL(empty_aops); | 
 | 91 |  | 
 | 92 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  * Statistics gathering.. | 
 | 94 |  */ | 
 | 95 | struct inodes_stat_t inodes_stat; | 
 | 96 |  | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 97 | static DEFINE_PER_CPU(unsigned int, nr_inodes); | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 98 |  | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 99 | static struct kmem_cache *inode_cachep __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 |  | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 101 | static int get_nr_inodes(void) | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 102 | { | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 103 | 	int i; | 
 | 104 | 	int sum = 0; | 
 | 105 | 	for_each_possible_cpu(i) | 
 | 106 | 		sum += per_cpu(nr_inodes, i); | 
 | 107 | 	return sum < 0 ? 0 : sum; | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 108 | } | 
 | 109 |  | 
 | 110 | static inline int get_nr_inodes_unused(void) | 
 | 111 | { | 
| Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 112 | 	return inodes_stat.nr_unused; | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 113 | } | 
 | 114 |  | 
 | 115 | int get_nr_dirty_inodes(void) | 
 | 116 | { | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 117 | 	/* not actually dirty inodes, but a wild approximation */ | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 118 | 	int nr_dirty = get_nr_inodes() - get_nr_inodes_unused(); | 
 | 119 | 	return nr_dirty > 0 ? nr_dirty : 0; | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 120 | } | 
 | 121 |  | 
 | 122 | /* | 
 | 123 |  * Handle nr_inode sysctl | 
 | 124 |  */ | 
 | 125 | #ifdef CONFIG_SYSCTL | 
 | 126 | int proc_nr_inodes(ctl_table *table, int write, | 
 | 127 | 		   void __user *buffer, size_t *lenp, loff_t *ppos) | 
 | 128 | { | 
 | 129 | 	inodes_stat.nr_inodes = get_nr_inodes(); | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 130 | 	return proc_dointvec(table, write, buffer, lenp, ppos); | 
 | 131 | } | 
 | 132 | #endif | 
 | 133 |  | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 134 | /** | 
 | 135 |  * inode_init_always - perform inode structure intialisation | 
| Randy Dunlap | 0bc02f3 | 2009-01-06 14:41:13 -0800 | [diff] [blame] | 136 |  * @sb: superblock inode belongs to | 
 | 137 |  * @inode: inode to initialise | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 138 |  * | 
 | 139 |  * These are initializations that need to be done on every inode | 
 | 140 |  * allocation as the fields are not initialised by slab allocation. | 
 | 141 |  */ | 
| Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 142 | int inode_init_always(struct super_block *sb, struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { | 
| Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 144 | 	static const struct inode_operations empty_iops; | 
| Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 145 | 	static const struct file_operations empty_fops; | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 146 | 	struct address_space *const mapping = &inode->i_data; | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 147 |  | 
 | 148 | 	inode->i_sb = sb; | 
 | 149 | 	inode->i_blkbits = sb->s_blocksize_bits; | 
 | 150 | 	inode->i_flags = 0; | 
 | 151 | 	atomic_set(&inode->i_count, 1); | 
 | 152 | 	inode->i_op = &empty_iops; | 
 | 153 | 	inode->i_fop = &empty_fops; | 
 | 154 | 	inode->i_nlink = 1; | 
| Al Viro | 56ff5ef | 2008-12-09 09:34:39 -0500 | [diff] [blame] | 155 | 	inode->i_uid = 0; | 
 | 156 | 	inode->i_gid = 0; | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 157 | 	atomic_set(&inode->i_writecount, 0); | 
 | 158 | 	inode->i_size = 0; | 
 | 159 | 	inode->i_blocks = 0; | 
 | 160 | 	inode->i_bytes = 0; | 
 | 161 | 	inode->i_generation = 0; | 
 | 162 | #ifdef CONFIG_QUOTA | 
 | 163 | 	memset(&inode->i_dquot, 0, sizeof(inode->i_dquot)); | 
 | 164 | #endif | 
 | 165 | 	inode->i_pipe = NULL; | 
 | 166 | 	inode->i_bdev = NULL; | 
 | 167 | 	inode->i_cdev = NULL; | 
 | 168 | 	inode->i_rdev = 0; | 
 | 169 | 	inode->dirtied_when = 0; | 
| Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 170 |  | 
 | 171 | 	if (security_inode_alloc(inode)) | 
| Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 172 | 		goto out; | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 173 | 	spin_lock_init(&inode->i_lock); | 
 | 174 | 	lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); | 
 | 175 |  | 
 | 176 | 	mutex_init(&inode->i_mutex); | 
 | 177 | 	lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key); | 
 | 178 |  | 
 | 179 | 	init_rwsem(&inode->i_alloc_sem); | 
 | 180 | 	lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key); | 
 | 181 |  | 
 | 182 | 	mapping->a_ops = &empty_aops; | 
 | 183 | 	mapping->host = inode; | 
 | 184 | 	mapping->flags = 0; | 
| Hugh Dickins | 3c1d437 | 2009-01-06 14:39:23 -0800 | [diff] [blame] | 185 | 	mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE); | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 186 | 	mapping->assoc_mapping = NULL; | 
 | 187 | 	mapping->backing_dev_info = &default_backing_dev_info; | 
 | 188 | 	mapping->writeback_index = 0; | 
 | 189 |  | 
 | 190 | 	/* | 
 | 191 | 	 * If the block_device provides a backing_dev_info for client | 
 | 192 | 	 * inodes then use that.  Otherwise the inode share the bdev's | 
 | 193 | 	 * backing_dev_info. | 
 | 194 | 	 */ | 
 | 195 | 	if (sb->s_bdev) { | 
 | 196 | 		struct backing_dev_info *bdi; | 
 | 197 |  | 
| Jens Axboe | 2c96ce9 | 2009-09-15 09:43:56 +0200 | [diff] [blame] | 198 | 		bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info; | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 199 | 		mapping->backing_dev_info = bdi; | 
 | 200 | 	} | 
 | 201 | 	inode->i_private = NULL; | 
 | 202 | 	inode->i_mapping = mapping; | 
| Al Viro | f19d4a8 | 2009-06-08 19:50:45 -0400 | [diff] [blame] | 203 | #ifdef CONFIG_FS_POSIX_ACL | 
 | 204 | 	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; | 
 | 205 | #endif | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 206 |  | 
| Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 207 | #ifdef CONFIG_FSNOTIFY | 
 | 208 | 	inode->i_fsnotify_mask = 0; | 
 | 209 | #endif | 
 | 210 |  | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 211 | 	this_cpu_inc(nr_inodes); | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 212 |  | 
| Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 213 | 	return 0; | 
| Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 214 | out: | 
 | 215 | 	return -ENOMEM; | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 216 | } | 
 | 217 | EXPORT_SYMBOL(inode_init_always); | 
 | 218 |  | 
 | 219 | static struct inode *alloc_inode(struct super_block *sb) | 
 | 220 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	struct inode *inode; | 
 | 222 |  | 
 | 223 | 	if (sb->s_op->alloc_inode) | 
 | 224 | 		inode = sb->s_op->alloc_inode(sb); | 
 | 225 | 	else | 
| David Chinner | 2cb1599 | 2008-10-30 17:32:23 +1100 | [diff] [blame] | 226 | 		inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 |  | 
| Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 228 | 	if (!inode) | 
 | 229 | 		return NULL; | 
 | 230 |  | 
 | 231 | 	if (unlikely(inode_init_always(sb, inode))) { | 
 | 232 | 		if (inode->i_sb->s_op->destroy_inode) | 
 | 233 | 			inode->i_sb->s_op->destroy_inode(inode); | 
 | 234 | 		else | 
 | 235 | 			kmem_cache_free(inode_cachep, inode); | 
 | 236 | 		return NULL; | 
 | 237 | 	} | 
 | 238 |  | 
 | 239 | 	return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } | 
 | 241 |  | 
| Nick Piggin | ff0c7d1 | 2011-01-07 17:49:50 +1100 | [diff] [blame] | 242 | void free_inode_nonrcu(struct inode *inode) | 
 | 243 | { | 
 | 244 | 	kmem_cache_free(inode_cachep, inode); | 
 | 245 | } | 
 | 246 | EXPORT_SYMBOL(free_inode_nonrcu); | 
 | 247 |  | 
| Christoph Hellwig | 2e00c97 | 2009-08-07 14:38:29 -0300 | [diff] [blame] | 248 | void __destroy_inode(struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { | 
| Eric Sesterhenn | b7542f8 | 2006-04-02 13:38:18 +0200 | [diff] [blame] | 250 | 	BUG_ON(inode_has_buffers(inode)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | 	security_inode_free(inode); | 
| Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 252 | 	fsnotify_inode_delete(inode); | 
| Al Viro | f19d4a8 | 2009-06-08 19:50:45 -0400 | [diff] [blame] | 253 | #ifdef CONFIG_FS_POSIX_ACL | 
 | 254 | 	if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED) | 
 | 255 | 		posix_acl_release(inode->i_acl); | 
 | 256 | 	if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED) | 
 | 257 | 		posix_acl_release(inode->i_default_acl); | 
 | 258 | #endif | 
| Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 259 | 	this_cpu_dec(nr_inodes); | 
| Christoph Hellwig | 2e00c97 | 2009-08-07 14:38:29 -0300 | [diff] [blame] | 260 | } | 
 | 261 | EXPORT_SYMBOL(__destroy_inode); | 
 | 262 |  | 
| Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 263 | static void i_callback(struct rcu_head *head) | 
 | 264 | { | 
 | 265 | 	struct inode *inode = container_of(head, struct inode, i_rcu); | 
 | 266 | 	INIT_LIST_HEAD(&inode->i_dentry); | 
 | 267 | 	kmem_cache_free(inode_cachep, inode); | 
 | 268 | } | 
 | 269 |  | 
| Christoph Hellwig | 56b0dac | 2010-10-06 10:48:55 +0200 | [diff] [blame] | 270 | static void destroy_inode(struct inode *inode) | 
| Christoph Hellwig | 2e00c97 | 2009-08-07 14:38:29 -0300 | [diff] [blame] | 271 | { | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 272 | 	BUG_ON(!list_empty(&inode->i_lru)); | 
| Christoph Hellwig | 2e00c97 | 2009-08-07 14:38:29 -0300 | [diff] [blame] | 273 | 	__destroy_inode(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | 	if (inode->i_sb->s_op->destroy_inode) | 
 | 275 | 		inode->i_sb->s_op->destroy_inode(inode); | 
 | 276 | 	else | 
| Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 277 | 		call_rcu(&inode->i_rcu, i_callback); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 |  | 
| Miklos Szeredi | 2aa1589 | 2011-02-23 13:49:47 +0100 | [diff] [blame] | 280 | void address_space_init_once(struct address_space *mapping) | 
 | 281 | { | 
 | 282 | 	memset(mapping, 0, sizeof(*mapping)); | 
 | 283 | 	INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC); | 
 | 284 | 	spin_lock_init(&mapping->tree_lock); | 
| Peter Zijlstra | 3d48ae4 | 2011-05-24 17:12:06 -0700 | [diff] [blame] | 285 | 	mutex_init(&mapping->i_mmap_mutex); | 
| Miklos Szeredi | 2aa1589 | 2011-02-23 13:49:47 +0100 | [diff] [blame] | 286 | 	INIT_LIST_HEAD(&mapping->private_list); | 
 | 287 | 	spin_lock_init(&mapping->private_lock); | 
 | 288 | 	INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap); | 
 | 289 | 	INIT_LIST_HEAD(&mapping->i_mmap_nonlinear); | 
| Miklos Szeredi | 2aa1589 | 2011-02-23 13:49:47 +0100 | [diff] [blame] | 290 | } | 
 | 291 | EXPORT_SYMBOL(address_space_init_once); | 
 | 292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* | 
 | 294 |  * These are initializations that only need to be done | 
 | 295 |  * once, because the fields are idempotent across use | 
 | 296 |  * of the inode, so let the slab aware of that. | 
 | 297 |  */ | 
 | 298 | void inode_init_once(struct inode *inode) | 
 | 299 | { | 
 | 300 | 	memset(inode, 0, sizeof(*inode)); | 
 | 301 | 	INIT_HLIST_NODE(&inode->i_hash); | 
 | 302 | 	INIT_LIST_HEAD(&inode->i_dentry); | 
 | 303 | 	INIT_LIST_HEAD(&inode->i_devices); | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 304 | 	INIT_LIST_HEAD(&inode->i_wb_list); | 
 | 305 | 	INIT_LIST_HEAD(&inode->i_lru); | 
| Miklos Szeredi | 2aa1589 | 2011-02-23 13:49:47 +0100 | [diff] [blame] | 306 | 	address_space_init_once(&inode->i_data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 	i_size_ordered_init(inode); | 
| Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 308 | #ifdef CONFIG_FSNOTIFY | 
| Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 309 | 	INIT_HLIST_HEAD(&inode->i_fsnotify_marks); | 
| Eric Paris | 3be25f4 | 2009-05-21 17:01:26 -0400 | [diff] [blame] | 310 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | EXPORT_SYMBOL(inode_init_once); | 
 | 313 |  | 
| Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 314 | static void init_once(void *foo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 316 | 	struct inode *inode = (struct inode *) foo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 |  | 
| Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 318 | 	inode_init_once(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } | 
 | 320 |  | 
 | 321 | /* | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 322 |  * inode->i_lock must be held | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 |  */ | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 324 | void __iget(struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 326 | 	atomic_inc(&inode->i_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } | 
 | 328 |  | 
| Al Viro | 7de9c6e | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 329 | /* | 
 | 330 |  * get additional reference to inode; caller must already hold one. | 
 | 331 |  */ | 
 | 332 | void ihold(struct inode *inode) | 
 | 333 | { | 
 | 334 | 	WARN_ON(atomic_inc_return(&inode->i_count) < 2); | 
 | 335 | } | 
 | 336 | EXPORT_SYMBOL(ihold); | 
 | 337 |  | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 338 | static void inode_lru_list_add(struct inode *inode) | 
 | 339 | { | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 340 | 	spin_lock(&inode_lru_lock); | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 341 | 	if (list_empty(&inode->i_lru)) { | 
 | 342 | 		list_add(&inode->i_lru, &inode_lru); | 
| Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 343 | 		inodes_stat.nr_unused++; | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 344 | 	} | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 345 | 	spin_unlock(&inode_lru_lock); | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 346 | } | 
 | 347 |  | 
 | 348 | static void inode_lru_list_del(struct inode *inode) | 
 | 349 | { | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 350 | 	spin_lock(&inode_lru_lock); | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 351 | 	if (!list_empty(&inode->i_lru)) { | 
 | 352 | 		list_del_init(&inode->i_lru); | 
| Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 353 | 		inodes_stat.nr_unused--; | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 354 | 	} | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 355 | 	spin_unlock(&inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } | 
 | 357 |  | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 358 | /** | 
 | 359 |  * inode_sb_list_add - add inode to the superblock list of inodes | 
 | 360 |  * @inode: inode to add | 
 | 361 |  */ | 
 | 362 | void inode_sb_list_add(struct inode *inode) | 
 | 363 | { | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 364 | 	spin_lock(&inode_sb_list_lock); | 
 | 365 | 	list_add(&inode->i_sb_list, &inode->i_sb->s_inodes); | 
 | 366 | 	spin_unlock(&inode_sb_list_lock); | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 367 | } | 
 | 368 | EXPORT_SYMBOL_GPL(inode_sb_list_add); | 
 | 369 |  | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 370 | static inline void inode_sb_list_del(struct inode *inode) | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 371 | { | 
| Eric Dumazet | 88b3f3d | 2011-07-26 11:36:34 +0200 | [diff] [blame] | 372 | 	if (!list_empty(&inode->i_sb_list)) { | 
 | 373 | 		spin_lock(&inode_sb_list_lock); | 
 | 374 | 		list_del_init(&inode->i_sb_list); | 
 | 375 | 		spin_unlock(&inode_sb_list_lock); | 
 | 376 | 	} | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 377 | } | 
 | 378 |  | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 379 | static unsigned long hash(struct super_block *sb, unsigned long hashval) | 
 | 380 | { | 
 | 381 | 	unsigned long tmp; | 
 | 382 |  | 
 | 383 | 	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / | 
 | 384 | 			L1_CACHE_BYTES; | 
| Christoph Hellwig | 4b4563d | 2011-05-27 09:28:01 -0400 | [diff] [blame] | 385 | 	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift); | 
 | 386 | 	return tmp & i_hash_mask; | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 387 | } | 
 | 388 |  | 
 | 389 | /** | 
 | 390 |  *	__insert_inode_hash - hash an inode | 
 | 391 |  *	@inode: unhashed inode | 
 | 392 |  *	@hashval: unsigned long value used to locate this object in the | 
 | 393 |  *		inode_hashtable. | 
 | 394 |  * | 
 | 395 |  *	Add an inode to the inode hash for this superblock. | 
 | 396 |  */ | 
 | 397 | void __insert_inode_hash(struct inode *inode, unsigned long hashval) | 
 | 398 | { | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 399 | 	struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); | 
 | 400 |  | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 401 | 	spin_lock(&inode_hash_lock); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 402 | 	spin_lock(&inode->i_lock); | 
| Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 403 | 	hlist_add_head(&inode->i_hash, b); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 404 | 	spin_unlock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 405 | 	spin_unlock(&inode_hash_lock); | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 406 | } | 
 | 407 | EXPORT_SYMBOL(__insert_inode_hash); | 
 | 408 |  | 
 | 409 | /** | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 410 |  *	remove_inode_hash - remove an inode from the hash | 
 | 411 |  *	@inode: inode to unhash | 
 | 412 |  * | 
 | 413 |  *	Remove an inode from the superblock. | 
 | 414 |  */ | 
 | 415 | void remove_inode_hash(struct inode *inode) | 
 | 416 | { | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 417 | 	spin_lock(&inode_hash_lock); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 418 | 	spin_lock(&inode->i_lock); | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 419 | 	hlist_del_init(&inode->i_hash); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 420 | 	spin_unlock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 421 | 	spin_unlock(&inode_hash_lock); | 
| Dave Chinner | 4c51acb | 2010-10-23 06:58:09 -0400 | [diff] [blame] | 422 | } | 
 | 423 | EXPORT_SYMBOL(remove_inode_hash); | 
 | 424 |  | 
| Al Viro | b0683aa | 2010-06-04 20:55:25 -0400 | [diff] [blame] | 425 | void end_writeback(struct inode *inode) | 
 | 426 | { | 
 | 427 | 	might_sleep(); | 
| Jan Kara | 0814257 | 2011-06-27 16:18:10 -0700 | [diff] [blame] | 428 | 	/* | 
 | 429 | 	 * We have to cycle tree_lock here because reclaim can be still in the | 
 | 430 | 	 * process of removing the last page (in __delete_from_page_cache()) | 
 | 431 | 	 * and we must not free mapping under it. | 
 | 432 | 	 */ | 
 | 433 | 	spin_lock_irq(&inode->i_data.tree_lock); | 
| Al Viro | b0683aa | 2010-06-04 20:55:25 -0400 | [diff] [blame] | 434 | 	BUG_ON(inode->i_data.nrpages); | 
| Jan Kara | 0814257 | 2011-06-27 16:18:10 -0700 | [diff] [blame] | 435 | 	spin_unlock_irq(&inode->i_data.tree_lock); | 
| Al Viro | b0683aa | 2010-06-04 20:55:25 -0400 | [diff] [blame] | 436 | 	BUG_ON(!list_empty(&inode->i_data.private_list)); | 
 | 437 | 	BUG_ON(!(inode->i_state & I_FREEING)); | 
 | 438 | 	BUG_ON(inode->i_state & I_CLEAR); | 
 | 439 | 	inode_sync_wait(inode); | 
| Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 440 | 	/* don't need i_lock here, no concurrent mods to i_state */ | 
| Al Viro | b0683aa | 2010-06-04 20:55:25 -0400 | [diff] [blame] | 441 | 	inode->i_state = I_FREEING | I_CLEAR; | 
 | 442 | } | 
 | 443 | EXPORT_SYMBOL(end_writeback); | 
 | 444 |  | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 445 | /* | 
 | 446 |  * Free the inode passed in, removing it from the lists it is still connected | 
 | 447 |  * to. We remove any pages still attached to the inode and wait for any IO that | 
 | 448 |  * is still in progress before finally destroying the inode. | 
 | 449 |  * | 
 | 450 |  * An inode must already be marked I_FREEING so that we avoid the inode being | 
 | 451 |  * moved back onto lists if we race with other code that manipulates the lists | 
 | 452 |  * (e.g. writeback_single_inode). The caller is responsible for setting this. | 
 | 453 |  * | 
 | 454 |  * An inode must already be removed from the LRU list before being evicted from | 
 | 455 |  * the cache. This should occur atomically with setting the I_FREEING state | 
 | 456 |  * flag, so no inodes here should ever be on the LRU when being evicted. | 
 | 457 |  */ | 
| Al Viro | 644da59 | 2010-06-07 13:21:05 -0400 | [diff] [blame] | 458 | static void evict(struct inode *inode) | 
| Al Viro | b4272d4 | 2010-06-04 19:33:20 -0400 | [diff] [blame] | 459 | { | 
 | 460 | 	const struct super_operations *op = inode->i_sb->s_op; | 
 | 461 |  | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 462 | 	BUG_ON(!(inode->i_state & I_FREEING)); | 
 | 463 | 	BUG_ON(!list_empty(&inode->i_lru)); | 
 | 464 |  | 
| Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 465 | 	inode_wb_list_del(inode); | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 466 | 	inode_sb_list_del(inode); | 
 | 467 |  | 
| Al Viro | be7ce41 | 2010-06-04 19:40:39 -0400 | [diff] [blame] | 468 | 	if (op->evict_inode) { | 
 | 469 | 		op->evict_inode(inode); | 
| Al Viro | b4272d4 | 2010-06-04 19:33:20 -0400 | [diff] [blame] | 470 | 	} else { | 
 | 471 | 		if (inode->i_data.nrpages) | 
 | 472 | 			truncate_inode_pages(&inode->i_data, 0); | 
| Al Viro | 3014083 | 2010-06-07 13:23:20 -0400 | [diff] [blame] | 473 | 		end_writeback(inode); | 
| Al Viro | b4272d4 | 2010-06-04 19:33:20 -0400 | [diff] [blame] | 474 | 	} | 
| Al Viro | 661074e | 2010-06-04 20:19:55 -0400 | [diff] [blame] | 475 | 	if (S_ISBLK(inode->i_mode) && inode->i_bdev) | 
 | 476 | 		bd_forget(inode); | 
 | 477 | 	if (S_ISCHR(inode->i_mode) && inode->i_cdev) | 
 | 478 | 		cd_forget(inode); | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 479 |  | 
 | 480 | 	remove_inode_hash(inode); | 
 | 481 |  | 
 | 482 | 	spin_lock(&inode->i_lock); | 
 | 483 | 	wake_up_bit(&inode->i_state, __I_NEW); | 
 | 484 | 	BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); | 
 | 485 | 	spin_unlock(&inode->i_lock); | 
 | 486 |  | 
 | 487 | 	destroy_inode(inode); | 
| Al Viro | b4272d4 | 2010-06-04 19:33:20 -0400 | [diff] [blame] | 488 | } | 
 | 489 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* | 
 | 491 |  * dispose_list - dispose of the contents of a local list | 
 | 492 |  * @head: the head of the list to free | 
 | 493 |  * | 
 | 494 |  * Dispose-list gets a local list with local inodes in it, so it doesn't | 
 | 495 |  * need to worry about list corruption and SMP locks. | 
 | 496 |  */ | 
 | 497 | static void dispose_list(struct list_head *head) | 
 | 498 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | 	while (!list_empty(head)) { | 
 | 500 | 		struct inode *inode; | 
 | 501 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 502 | 		inode = list_first_entry(head, struct inode, i_lru); | 
 | 503 | 		list_del_init(&inode->i_lru); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 |  | 
| Al Viro | 644da59 | 2010-06-07 13:21:05 -0400 | [diff] [blame] | 505 | 		evict(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } | 
 | 508 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /** | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 510 |  * evict_inodes	- evict all evictable inodes for a superblock | 
 | 511 |  * @sb:		superblock to operate on | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 |  * | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 513 |  * Make sure that no inodes with zero refcount are retained.  This is | 
 | 514 |  * called by superblock shutdown after having MS_ACTIVE flag removed, | 
 | 515 |  * so any inode reaching zero refcount during or after that call will | 
 | 516 |  * be immediately evicted. | 
 | 517 |  */ | 
 | 518 | void evict_inodes(struct super_block *sb) | 
 | 519 | { | 
 | 520 | 	struct inode *inode, *next; | 
 | 521 | 	LIST_HEAD(dispose); | 
 | 522 |  | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 523 | 	spin_lock(&inode_sb_list_lock); | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 524 | 	list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { | 
 | 525 | 		if (atomic_read(&inode->i_count)) | 
 | 526 | 			continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 527 |  | 
 | 528 | 		spin_lock(&inode->i_lock); | 
 | 529 | 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { | 
 | 530 | 			spin_unlock(&inode->i_lock); | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 531 | 			continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 532 | 		} | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 533 |  | 
 | 534 | 		inode->i_state |= I_FREEING; | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 535 | 		inode_lru_list_del(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 536 | 		spin_unlock(&inode->i_lock); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 537 | 		list_add(&inode->i_lru, &dispose); | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 538 | 	} | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 539 | 	spin_unlock(&inode_sb_list_lock); | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 540 |  | 
 | 541 | 	dispose_list(&dispose); | 
| Christoph Hellwig | bab1d94 | 2011-03-15 21:51:24 +0100 | [diff] [blame] | 542 |  | 
 | 543 | 	/* | 
 | 544 | 	 * Cycle through iprune_sem to make sure any inode that prune_icache | 
 | 545 | 	 * moved off the list before we took the lock has been fully torn | 
 | 546 | 	 * down. | 
 | 547 | 	 */ | 
 | 548 | 	down_write(&iprune_sem); | 
| Al Viro | 63997e9 | 2010-10-25 20:49:35 -0400 | [diff] [blame] | 549 | 	up_write(&iprune_sem); | 
 | 550 | } | 
 | 551 |  | 
 | 552 | /** | 
| Christoph Hellwig | a031878 | 2010-10-24 19:40:33 +0200 | [diff] [blame] | 553 |  * invalidate_inodes	- attempt to free all inodes on a superblock | 
 | 554 |  * @sb:		superblock to operate on | 
| NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 555 |  * @kill_dirty: flag to guide handling of dirty inodes | 
| Christoph Hellwig | a031878 | 2010-10-24 19:40:33 +0200 | [diff] [blame] | 556 |  * | 
 | 557 |  * Attempts to free all inodes for a given superblock.  If there were any | 
 | 558 |  * busy inodes return a non-zero value, else zero. | 
| NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 559 |  * If @kill_dirty is set, discard dirty inodes too, otherwise treat | 
 | 560 |  * them as busy. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 |  */ | 
| NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 562 | int invalidate_inodes(struct super_block *sb, bool kill_dirty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 564 | 	int busy = 0; | 
| Christoph Hellwig | a031878 | 2010-10-24 19:40:33 +0200 | [diff] [blame] | 565 | 	struct inode *inode, *next; | 
 | 566 | 	LIST_HEAD(dispose); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 |  | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 568 | 	spin_lock(&inode_sb_list_lock); | 
| Christoph Hellwig | a031878 | 2010-10-24 19:40:33 +0200 | [diff] [blame] | 569 | 	list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 570 | 		spin_lock(&inode->i_lock); | 
 | 571 | 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { | 
 | 572 | 			spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 			continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 574 | 		} | 
| NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 575 | 		if (inode->i_state & I_DIRTY && !kill_dirty) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 576 | 			spin_unlock(&inode->i_lock); | 
| NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 577 | 			busy = 1; | 
 | 578 | 			continue; | 
 | 579 | 		} | 
| Christoph Hellwig | 99a3891 | 2010-10-23 19:07:20 +0200 | [diff] [blame] | 580 | 		if (atomic_read(&inode->i_count)) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 581 | 			spin_unlock(&inode->i_lock); | 
| Christoph Hellwig | 99a3891 | 2010-10-23 19:07:20 +0200 | [diff] [blame] | 582 | 			busy = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | 			continue; | 
 | 584 | 		} | 
| Christoph Hellwig | 99a3891 | 2010-10-23 19:07:20 +0200 | [diff] [blame] | 585 |  | 
| Christoph Hellwig | 99a3891 | 2010-10-23 19:07:20 +0200 | [diff] [blame] | 586 | 		inode->i_state |= I_FREEING; | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 587 | 		inode_lru_list_del(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 588 | 		spin_unlock(&inode->i_lock); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 589 | 		list_add(&inode->i_lru, &dispose); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | 	} | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 591 | 	spin_unlock(&inode_sb_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 |  | 
| Christoph Hellwig | a031878 | 2010-10-24 19:40:33 +0200 | [diff] [blame] | 593 | 	dispose_list(&dispose); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 |  | 
 | 595 | 	return busy; | 
 | 596 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 |  | 
 | 598 | static int can_unuse(struct inode *inode) | 
 | 599 | { | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 600 | 	if (inode->i_state & ~I_REFERENCED) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | 		return 0; | 
 | 602 | 	if (inode_has_buffers(inode)) | 
 | 603 | 		return 0; | 
 | 604 | 	if (atomic_read(&inode->i_count)) | 
 | 605 | 		return 0; | 
 | 606 | 	if (inode->i_data.nrpages) | 
 | 607 | 		return 0; | 
 | 608 | 	return 1; | 
 | 609 | } | 
 | 610 |  | 
 | 611 | /* | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 612 |  * Scan `goal' inodes on the unused list for freeable ones. They are moved to a | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 613 |  * temporary list and then are freed outside inode_lru_lock by dispose_list(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 |  * | 
 | 615 |  * Any inodes which are pinned purely because of attached pagecache have their | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 616 |  * pagecache removed.  If the inode has metadata buffers attached to | 
 | 617 |  * mapping->private_list then try to remove them. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 |  * | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 619 |  * If the inode has the I_REFERENCED flag set, then it means that it has been | 
 | 620 |  * used recently - the flag is set in iput_final(). When we encounter such an | 
 | 621 |  * inode, clear the flag and move it to the back of the LRU so it gets another | 
 | 622 |  * pass through the LRU before it gets reclaimed. This is necessary because of | 
 | 623 |  * the fact we are doing lazy LRU updates to minimise lock contention so the | 
 | 624 |  * LRU does not have strict ordering. Hence we don't want to reclaim inodes | 
 | 625 |  * with this flag set because they are the inodes that are out of order. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 |  */ | 
 | 627 | static void prune_icache(int nr_to_scan) | 
 | 628 | { | 
 | 629 | 	LIST_HEAD(freeable); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | 	int nr_scanned; | 
 | 631 | 	unsigned long reap = 0; | 
 | 632 |  | 
| Nick Piggin | 88e0fbc | 2009-09-22 16:43:50 -0700 | [diff] [blame] | 633 | 	down_read(&iprune_sem); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 634 | 	spin_lock(&inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | 	for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { | 
 | 636 | 		struct inode *inode; | 
 | 637 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 638 | 		if (list_empty(&inode_lru)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | 			break; | 
 | 640 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 641 | 		inode = list_entry(inode_lru.prev, struct inode, i_lru); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 |  | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 643 | 		/* | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 644 | 		 * we are inverting the inode_lru_lock/inode->i_lock here, | 
 | 645 | 		 * so use a trylock. If we fail to get the lock, just move the | 
 | 646 | 		 * inode to the back of the list so we don't spin on it. | 
 | 647 | 		 */ | 
 | 648 | 		if (!spin_trylock(&inode->i_lock)) { | 
 | 649 | 			list_move(&inode->i_lru, &inode_lru); | 
 | 650 | 			continue; | 
 | 651 | 		} | 
 | 652 |  | 
 | 653 | 		/* | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 654 | 		 * Referenced or dirty inodes are still in use. Give them | 
 | 655 | 		 * another pass through the LRU as we canot reclaim them now. | 
 | 656 | 		 */ | 
 | 657 | 		if (atomic_read(&inode->i_count) || | 
 | 658 | 		    (inode->i_state & ~I_REFERENCED)) { | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 659 | 			list_del_init(&inode->i_lru); | 
| Dave Chinner | f283c86 | 2011-03-22 22:23:39 +1100 | [diff] [blame] | 660 | 			spin_unlock(&inode->i_lock); | 
| Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 661 | 			inodes_stat.nr_unused--; | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 662 | 			continue; | 
 | 663 | 		} | 
 | 664 |  | 
 | 665 | 		/* recently referenced inodes get one more pass */ | 
 | 666 | 		if (inode->i_state & I_REFERENCED) { | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 667 | 			inode->i_state &= ~I_REFERENCED; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 668 | 			list_move(&inode->i_lru, &inode_lru); | 
| Dave Chinner | f283c86 | 2011-03-22 22:23:39 +1100 | [diff] [blame] | 669 | 			spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | 			continue; | 
 | 671 | 		} | 
 | 672 | 		if (inode_has_buffers(inode) || inode->i_data.nrpages) { | 
 | 673 | 			__iget(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 674 | 			spin_unlock(&inode->i_lock); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 675 | 			spin_unlock(&inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 			if (remove_inode_buffers(inode)) | 
| Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 677 | 				reap += invalidate_mapping_pages(&inode->i_data, | 
 | 678 | 								0, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | 			iput(inode); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 680 | 			spin_lock(&inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 682 | 			if (inode != list_entry(inode_lru.next, | 
 | 683 | 						struct inode, i_lru)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | 				continue;	/* wrong inode or list_empty */ | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 685 | 			/* avoid lock inversions with trylock */ | 
 | 686 | 			if (!spin_trylock(&inode->i_lock)) | 
 | 687 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 688 | 			if (!can_unuse(inode)) { | 
 | 689 | 				spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 691 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | 		} | 
| Nick Piggin | 7ef0d73 | 2009-03-12 14:31:38 -0700 | [diff] [blame] | 693 | 		WARN_ON(inode->i_state & I_NEW); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | 		inode->i_state |= I_FREEING; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 695 | 		spin_unlock(&inode->i_lock); | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 696 |  | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 697 | 		list_move(&inode->i_lru, &freeable); | 
| Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 698 | 		inodes_stat.nr_unused--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | 	} | 
| Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 700 | 	if (current_is_kswapd()) | 
 | 701 | 		__count_vm_events(KSWAPD_INODESTEAL, reap); | 
 | 702 | 	else | 
 | 703 | 		__count_vm_events(PGINODESTEAL, reap); | 
| Dave Chinner | 02afc41 | 2011-03-22 22:23:38 +1100 | [diff] [blame] | 704 | 	spin_unlock(&inode_lru_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 |  | 
 | 706 | 	dispose_list(&freeable); | 
| Nick Piggin | 88e0fbc | 2009-09-22 16:43:50 -0700 | [diff] [blame] | 707 | 	up_read(&iprune_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } | 
 | 709 |  | 
 | 710 | /* | 
 | 711 |  * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here, | 
 | 712 |  * "unused" means that no dentries are referring to the inodes: the files are | 
 | 713 |  * not open and the dcache references to those inodes have already been | 
 | 714 |  * reclaimed. | 
 | 715 |  * | 
 | 716 |  * This function is passed the number of inodes to scan, and it returns the | 
 | 717 |  * total number of remaining possibly-reclaimable inodes. | 
 | 718 |  */ | 
| Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 719 | static int shrink_icache_memory(struct shrinker *shrink, | 
 | 720 | 				struct shrink_control *sc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { | 
| Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 722 | 	int nr = sc->nr_to_scan; | 
 | 723 | 	gfp_t gfp_mask = sc->gfp_mask; | 
 | 724 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | 	if (nr) { | 
 | 726 | 		/* | 
 | 727 | 		 * Nasty deadlock avoidance.  We may hold various FS locks, | 
 | 728 | 		 * and we don't want to recurse into the FS that called us | 
 | 729 | 		 * in clear_inode() and friends.. | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 730 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | 		if (!(gfp_mask & __GFP_FS)) | 
 | 732 | 			return -1; | 
 | 733 | 		prune_icache(nr); | 
 | 734 | 	} | 
| Dave Chinner | cffbc8a | 2010-10-23 05:03:02 -0400 | [diff] [blame] | 735 | 	return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } | 
 | 737 |  | 
| Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 738 | static struct shrinker icache_shrinker = { | 
 | 739 | 	.shrink = shrink_icache_memory, | 
 | 740 | 	.seeks = DEFAULT_SEEKS, | 
 | 741 | }; | 
 | 742 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | static void __wait_on_freeing_inode(struct inode *inode); | 
 | 744 | /* | 
 | 745 |  * Called with the inode lock held. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 |  */ | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 747 | static struct inode *find_inode(struct super_block *sb, | 
 | 748 | 				struct hlist_head *head, | 
 | 749 | 				int (*test)(struct inode *, void *), | 
 | 750 | 				void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { | 
 | 752 | 	struct hlist_node *node; | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 753 | 	struct inode *inode = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 |  | 
 | 755 | repeat: | 
| Matthias Kaehlcke | c5c8be3 | 2008-04-29 00:59:40 -0700 | [diff] [blame] | 756 | 	hlist_for_each_entry(inode, node, head, i_hash) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 757 | 		spin_lock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 758 | 		if (inode->i_sb != sb) { | 
 | 759 | 			spin_unlock(&inode->i_lock); | 
 | 760 | 			continue; | 
 | 761 | 		} | 
 | 762 | 		if (!test(inode, data)) { | 
 | 763 | 			spin_unlock(&inode->i_lock); | 
 | 764 | 			continue; | 
 | 765 | 		} | 
| Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 766 | 		if (inode->i_state & (I_FREEING|I_WILL_FREE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | 			__wait_on_freeing_inode(inode); | 
 | 768 | 			goto repeat; | 
 | 769 | 		} | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 770 | 		__iget(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 771 | 		spin_unlock(&inode->i_lock); | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 772 | 		return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | 	} | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 774 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } | 
 | 776 |  | 
 | 777 | /* | 
 | 778 |  * find_inode_fast is the fast path version of find_inode, see the comment at | 
 | 779 |  * iget_locked for details. | 
 | 780 |  */ | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 781 | static struct inode *find_inode_fast(struct super_block *sb, | 
 | 782 | 				struct hlist_head *head, unsigned long ino) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { | 
 | 784 | 	struct hlist_node *node; | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 785 | 	struct inode *inode = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 |  | 
 | 787 | repeat: | 
| Matthias Kaehlcke | c5c8be3 | 2008-04-29 00:59:40 -0700 | [diff] [blame] | 788 | 	hlist_for_each_entry(inode, node, head, i_hash) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 789 | 		spin_lock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 790 | 		if (inode->i_ino != ino) { | 
 | 791 | 			spin_unlock(&inode->i_lock); | 
 | 792 | 			continue; | 
 | 793 | 		} | 
 | 794 | 		if (inode->i_sb != sb) { | 
 | 795 | 			spin_unlock(&inode->i_lock); | 
 | 796 | 			continue; | 
 | 797 | 		} | 
| Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 798 | 		if (inode->i_state & (I_FREEING|I_WILL_FREE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | 			__wait_on_freeing_inode(inode); | 
 | 800 | 			goto repeat; | 
 | 801 | 		} | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 802 | 		__iget(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 803 | 		spin_unlock(&inode->i_lock); | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 804 | 		return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 	} | 
| Christoph Hellwig | f7899bd | 2010-10-23 07:09:06 -0400 | [diff] [blame] | 806 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } | 
 | 808 |  | 
| Eric Dumazet | f991bd2 | 2010-10-23 11:18:01 -0400 | [diff] [blame] | 809 | /* | 
 | 810 |  * Each cpu owns a range of LAST_INO_BATCH numbers. | 
 | 811 |  * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations, | 
 | 812 |  * to renew the exhausted range. | 
| David Chinner | 8290c35 | 2008-10-30 17:35:24 +1100 | [diff] [blame] | 813 |  * | 
| Eric Dumazet | f991bd2 | 2010-10-23 11:18:01 -0400 | [diff] [blame] | 814 |  * This does not significantly increase overflow rate because every CPU can | 
 | 815 |  * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is | 
 | 816 |  * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the | 
 | 817 |  * 2^32 range, and is a worst-case. Even a 50% wastage would only increase | 
 | 818 |  * overflow rate by 2x, which does not seem too significant. | 
 | 819 |  * | 
 | 820 |  * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW | 
 | 821 |  * error if st_ino won't fit in target struct field. Use 32bit counter | 
 | 822 |  * here to attempt to avoid that. | 
| David Chinner | 8290c35 | 2008-10-30 17:35:24 +1100 | [diff] [blame] | 823 |  */ | 
| Eric Dumazet | f991bd2 | 2010-10-23 11:18:01 -0400 | [diff] [blame] | 824 | #define LAST_INO_BATCH 1024 | 
 | 825 | static DEFINE_PER_CPU(unsigned int, last_ino); | 
| David Chinner | 8290c35 | 2008-10-30 17:35:24 +1100 | [diff] [blame] | 826 |  | 
| Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 827 | unsigned int get_next_ino(void) | 
| Eric Dumazet | f991bd2 | 2010-10-23 11:18:01 -0400 | [diff] [blame] | 828 | { | 
 | 829 | 	unsigned int *p = &get_cpu_var(last_ino); | 
 | 830 | 	unsigned int res = *p; | 
 | 831 |  | 
 | 832 | #ifdef CONFIG_SMP | 
 | 833 | 	if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) { | 
 | 834 | 		static atomic_t shared_last_ino; | 
 | 835 | 		int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino); | 
 | 836 |  | 
 | 837 | 		res = next - LAST_INO_BATCH; | 
 | 838 | 	} | 
 | 839 | #endif | 
 | 840 |  | 
 | 841 | 	*p = ++res; | 
 | 842 | 	put_cpu_var(last_ino); | 
 | 843 | 	return res; | 
| David Chinner | 8290c35 | 2008-10-30 17:35:24 +1100 | [diff] [blame] | 844 | } | 
| Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 845 | EXPORT_SYMBOL(get_next_ino); | 
| David Chinner | 8290c35 | 2008-10-30 17:35:24 +1100 | [diff] [blame] | 846 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | /** | 
| Eric Dumazet | 88b3f3d | 2011-07-26 11:36:34 +0200 | [diff] [blame] | 848 |  *	new_inode_pseudo 	- obtain an inode | 
 | 849 |  *	@sb: superblock | 
 | 850 |  * | 
 | 851 |  *	Allocates a new inode for given superblock. | 
 | 852 |  *	Inode wont be chained in superblock s_inodes list | 
 | 853 |  *	This means : | 
 | 854 |  *	- fs can't be unmount | 
 | 855 |  *	- quotas, fsnotify, writeback can't work | 
 | 856 |  */ | 
 | 857 | struct inode *new_inode_pseudo(struct super_block *sb) | 
 | 858 | { | 
 | 859 | 	struct inode *inode = alloc_inode(sb); | 
 | 860 |  | 
 | 861 | 	if (inode) { | 
 | 862 | 		spin_lock(&inode->i_lock); | 
 | 863 | 		inode->i_state = 0; | 
 | 864 | 		spin_unlock(&inode->i_lock); | 
 | 865 | 		INIT_LIST_HEAD(&inode->i_sb_list); | 
 | 866 | 	} | 
 | 867 | 	return inode; | 
 | 868 | } | 
 | 869 |  | 
 | 870 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 |  *	new_inode 	- obtain an inode | 
 | 872 |  *	@sb: superblock | 
 | 873 |  * | 
| Mel Gorman | 769848c | 2007-07-17 04:03:05 -0700 | [diff] [blame] | 874 |  *	Allocates a new inode for given superblock. The default gfp_mask | 
| Hugh Dickins | 3c1d437 | 2009-01-06 14:39:23 -0800 | [diff] [blame] | 875 |  *	for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE. | 
| Mel Gorman | 769848c | 2007-07-17 04:03:05 -0700 | [diff] [blame] | 876 |  *	If HIGHMEM pages are unsuitable or it is known that pages allocated | 
 | 877 |  *	for the page cache are not reclaimable or migratable, | 
 | 878 |  *	mapping_set_gfp_mask() must be called with suitable flags on the | 
 | 879 |  *	newly created inode's mapping | 
 | 880 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 |  */ | 
 | 882 | struct inode *new_inode(struct super_block *sb) | 
 | 883 | { | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 884 | 	struct inode *inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 |  | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 886 | 	spin_lock_prefetch(&inode_sb_list_lock); | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 887 |  | 
| Eric Dumazet | 88b3f3d | 2011-07-26 11:36:34 +0200 | [diff] [blame] | 888 | 	inode = new_inode_pseudo(sb); | 
 | 889 | 	if (inode) | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 890 | 		inode_sb_list_add(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | 	return inode; | 
 | 892 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | EXPORT_SYMBOL(new_inode); | 
 | 894 |  | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 895 | /** | 
 | 896 |  * unlock_new_inode - clear the I_NEW state and wake up any waiters | 
 | 897 |  * @inode:	new inode to unlock | 
 | 898 |  * | 
 | 899 |  * Called when the inode is fully initialised to clear the new state of the | 
 | 900 |  * inode and wake up anyone waiting for the inode to finish initialisation. | 
 | 901 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | void unlock_new_inode(struct inode *inode) | 
 | 903 | { | 
| Peter Zijlstra | 14358e6 | 2007-10-14 01:38:33 +0200 | [diff] [blame] | 904 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | 
| Namhyung Kim | a3314a0 | 2010-10-11 22:38:00 +0900 | [diff] [blame] | 905 | 	if (S_ISDIR(inode->i_mode)) { | 
| Peter Zijlstra | 1e89a5e | 2007-10-16 06:47:54 +0200 | [diff] [blame] | 906 | 		struct file_system_type *type = inode->i_sb->s_type; | 
 | 907 |  | 
| Jan Kara | 9a7aa12 | 2009-06-04 15:26:49 +0200 | [diff] [blame] | 908 | 		/* Set new key only if filesystem hasn't already changed it */ | 
 | 909 | 		if (!lockdep_match_class(&inode->i_mutex, | 
 | 910 | 		    &type->i_mutex_key)) { | 
 | 911 | 			/* | 
 | 912 | 			 * ensure nobody is actually holding i_mutex | 
 | 913 | 			 */ | 
 | 914 | 			mutex_destroy(&inode->i_mutex); | 
 | 915 | 			mutex_init(&inode->i_mutex); | 
 | 916 | 			lockdep_set_class(&inode->i_mutex, | 
 | 917 | 					  &type->i_mutex_dir_key); | 
 | 918 | 		} | 
| Peter Zijlstra | 1e89a5e | 2007-10-16 06:47:54 +0200 | [diff] [blame] | 919 | 	} | 
| Peter Zijlstra | 14358e6 | 2007-10-14 01:38:33 +0200 | [diff] [blame] | 920 | #endif | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 921 | 	spin_lock(&inode->i_lock); | 
| Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 922 | 	WARN_ON(!(inode->i_state & I_NEW)); | 
 | 923 | 	inode->i_state &= ~I_NEW; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 924 | 	wake_up_bit(&inode->i_state, __I_NEW); | 
 | 925 | 	spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | EXPORT_SYMBOL(unlock_new_inode); | 
 | 928 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 929 | /** | 
 | 930 |  * iget5_locked - obtain an inode from a mounted file system | 
 | 931 |  * @sb:		super block of file system | 
 | 932 |  * @hashval:	hash value (usually inode number) to get | 
 | 933 |  * @test:	callback used for comparisons between inodes | 
 | 934 |  * @set:	callback used to initialize a new struct inode | 
 | 935 |  * @data:	opaque data pointer to pass to @test and @set | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 937 |  * Search for the inode specified by @hashval and @data in the inode cache, | 
 | 938 |  * and if present it is return it with an increased reference count. This is | 
 | 939 |  * a generalized version of iget_locked() for file systems where the inode | 
 | 940 |  * number is not sufficient for unique identification of an inode. | 
 | 941 |  * | 
 | 942 |  * If the inode is not in cache, allocate a new inode and return it locked, | 
 | 943 |  * hashed, and with the I_NEW flag set. The file system gets to fill it in | 
 | 944 |  * before unlocking it via unlock_new_inode(). | 
 | 945 |  * | 
 | 946 |  * Note both @test and @set are called with the inode_hash_lock held, so can't | 
 | 947 |  * sleep. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 |  */ | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 949 | struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, | 
 | 950 | 		int (*test)(struct inode *, void *), | 
 | 951 | 		int (*set)(struct inode *, void *), void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 953 | 	struct hlist_head *head = inode_hashtable + hash(sb, hashval); | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 954 | 	struct inode *inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 956 | 	spin_lock(&inode_hash_lock); | 
 | 957 | 	inode = find_inode(sb, head, test, data); | 
 | 958 | 	spin_unlock(&inode_hash_lock); | 
 | 959 |  | 
 | 960 | 	if (inode) { | 
 | 961 | 		wait_on_inode(inode); | 
 | 962 | 		return inode; | 
 | 963 | 	} | 
 | 964 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | 	inode = alloc_inode(sb); | 
 | 966 | 	if (inode) { | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 967 | 		struct inode *old; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 |  | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 969 | 		spin_lock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | 		/* We released the lock, so.. */ | 
 | 971 | 		old = find_inode(sb, head, test, data); | 
 | 972 | 		if (!old) { | 
 | 973 | 			if (set(inode, data)) | 
 | 974 | 				goto set_failed; | 
 | 975 |  | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 976 | 			spin_lock(&inode->i_lock); | 
| Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 977 | 			inode->i_state = I_NEW; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 978 | 			hlist_add_head(&inode->i_hash, head); | 
 | 979 | 			spin_unlock(&inode->i_lock); | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 980 | 			inode_sb_list_add(inode); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 981 | 			spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 |  | 
 | 983 | 			/* Return the locked inode with I_NEW set, the | 
 | 984 | 			 * caller is responsible for filling in the contents | 
 | 985 | 			 */ | 
 | 986 | 			return inode; | 
 | 987 | 		} | 
 | 988 |  | 
 | 989 | 		/* | 
 | 990 | 		 * Uhhuh, somebody else created the same inode under | 
 | 991 | 		 * us. Use the old inode instead of the one we just | 
 | 992 | 		 * allocated. | 
 | 993 | 		 */ | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 994 | 		spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | 		destroy_inode(inode); | 
 | 996 | 		inode = old; | 
 | 997 | 		wait_on_inode(inode); | 
 | 998 | 	} | 
 | 999 | 	return inode; | 
 | 1000 |  | 
 | 1001 | set_failed: | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1002 | 	spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | 	destroy_inode(inode); | 
 | 1004 | 	return NULL; | 
 | 1005 | } | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1006 | EXPORT_SYMBOL(iget5_locked); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1008 | /** | 
 | 1009 |  * iget_locked - obtain an inode from a mounted file system | 
 | 1010 |  * @sb:		super block of file system | 
 | 1011 |  * @ino:	inode number to get | 
 | 1012 |  * | 
 | 1013 |  * Search for the inode specified by @ino in the inode cache and if present | 
 | 1014 |  * return it with an increased reference count. This is for file systems | 
 | 1015 |  * where the inode number is sufficient for unique identification of an inode. | 
 | 1016 |  * | 
 | 1017 |  * If the inode is not in cache, allocate a new inode and return it locked, | 
 | 1018 |  * hashed, and with the I_NEW flag set.  The file system gets to fill it in | 
 | 1019 |  * before unlocking it via unlock_new_inode(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 |  */ | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1021 | struct inode *iget_locked(struct super_block *sb, unsigned long ino) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | { | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1023 | 	struct hlist_head *head = inode_hashtable + hash(sb, ino); | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 1024 | 	struct inode *inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1026 | 	spin_lock(&inode_hash_lock); | 
 | 1027 | 	inode = find_inode_fast(sb, head, ino); | 
 | 1028 | 	spin_unlock(&inode_hash_lock); | 
 | 1029 | 	if (inode) { | 
 | 1030 | 		wait_on_inode(inode); | 
 | 1031 | 		return inode; | 
 | 1032 | 	} | 
 | 1033 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | 	inode = alloc_inode(sb); | 
 | 1035 | 	if (inode) { | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 1036 | 		struct inode *old; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 |  | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1038 | 		spin_lock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | 		/* We released the lock, so.. */ | 
 | 1040 | 		old = find_inode_fast(sb, head, ino); | 
 | 1041 | 		if (!old) { | 
 | 1042 | 			inode->i_ino = ino; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1043 | 			spin_lock(&inode->i_lock); | 
| Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 1044 | 			inode->i_state = I_NEW; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1045 | 			hlist_add_head(&inode->i_hash, head); | 
 | 1046 | 			spin_unlock(&inode->i_lock); | 
| Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 1047 | 			inode_sb_list_add(inode); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1048 | 			spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 |  | 
 | 1050 | 			/* Return the locked inode with I_NEW set, the | 
 | 1051 | 			 * caller is responsible for filling in the contents | 
 | 1052 | 			 */ | 
 | 1053 | 			return inode; | 
 | 1054 | 		} | 
 | 1055 |  | 
 | 1056 | 		/* | 
 | 1057 | 		 * Uhhuh, somebody else created the same inode under | 
 | 1058 | 		 * us. Use the old inode instead of the one we just | 
 | 1059 | 		 * allocated. | 
 | 1060 | 		 */ | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1061 | 		spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | 		destroy_inode(inode); | 
 | 1063 | 		inode = old; | 
 | 1064 | 		wait_on_inode(inode); | 
 | 1065 | 	} | 
 | 1066 | 	return inode; | 
 | 1067 | } | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1068 | EXPORT_SYMBOL(iget_locked); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 |  | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1070 | /* | 
 | 1071 |  * search the inode cache for a matching inode number. | 
 | 1072 |  * If we find one, then the inode number we are trying to | 
 | 1073 |  * allocate is not unique and so we should not use it. | 
 | 1074 |  * | 
 | 1075 |  * Returns 1 if the inode number is unique, 0 if it is not. | 
 | 1076 |  */ | 
 | 1077 | static int test_inode_iunique(struct super_block *sb, unsigned long ino) | 
 | 1078 | { | 
 | 1079 | 	struct hlist_head *b = inode_hashtable + hash(sb, ino); | 
 | 1080 | 	struct hlist_node *node; | 
 | 1081 | 	struct inode *inode; | 
 | 1082 |  | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1083 | 	spin_lock(&inode_hash_lock); | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1084 | 	hlist_for_each_entry(inode, node, b, i_hash) { | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1085 | 		if (inode->i_ino == ino && inode->i_sb == sb) { | 
 | 1086 | 			spin_unlock(&inode_hash_lock); | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1087 | 			return 0; | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1088 | 		} | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1089 | 	} | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1090 | 	spin_unlock(&inode_hash_lock); | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1091 |  | 
 | 1092 | 	return 1; | 
 | 1093 | } | 
 | 1094 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | /** | 
 | 1096 |  *	iunique - get a unique inode number | 
 | 1097 |  *	@sb: superblock | 
 | 1098 |  *	@max_reserved: highest reserved inode number | 
 | 1099 |  * | 
 | 1100 |  *	Obtain an inode number that is unique on the system for a given | 
 | 1101 |  *	superblock. This is used by file systems that have no natural | 
 | 1102 |  *	permanent inode numbering system. An inode number is returned that | 
 | 1103 |  *	is higher than the reserved limit but unique. | 
 | 1104 |  * | 
 | 1105 |  *	BUGS: | 
 | 1106 |  *	With a large number of inodes live on the file system this function | 
 | 1107 |  *	currently becomes quite slow. | 
 | 1108 |  */ | 
 | 1109 | ino_t iunique(struct super_block *sb, ino_t max_reserved) | 
 | 1110 | { | 
| Jeff Layton | 866b04f | 2007-05-08 00:32:29 -0700 | [diff] [blame] | 1111 | 	/* | 
 | 1112 | 	 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW | 
 | 1113 | 	 * error if st_ino won't fit in target struct field. Use 32bit counter | 
 | 1114 | 	 * here to attempt to avoid that. | 
 | 1115 | 	 */ | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1116 | 	static DEFINE_SPINLOCK(iunique_lock); | 
| Jeff Layton | 866b04f | 2007-05-08 00:32:29 -0700 | [diff] [blame] | 1117 | 	static unsigned int counter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | 	ino_t res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 |  | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1120 | 	spin_lock(&iunique_lock); | 
| Jeffrey Layton | 3361c7b | 2007-05-08 00:29:48 -0700 | [diff] [blame] | 1121 | 	do { | 
 | 1122 | 		if (counter <= max_reserved) | 
 | 1123 | 			counter = max_reserved + 1; | 
 | 1124 | 		res = counter++; | 
| Christoph Hellwig | ad5e195 | 2010-10-23 07:00:16 -0400 | [diff] [blame] | 1125 | 	} while (!test_inode_iunique(sb, res)); | 
 | 1126 | 	spin_unlock(&iunique_lock); | 
| Jeffrey Layton | 3361c7b | 2007-05-08 00:29:48 -0700 | [diff] [blame] | 1127 |  | 
 | 1128 | 	return res; | 
 | 1129 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | EXPORT_SYMBOL(iunique); | 
 | 1131 |  | 
 | 1132 | struct inode *igrab(struct inode *inode) | 
 | 1133 | { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1134 | 	spin_lock(&inode->i_lock); | 
 | 1135 | 	if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | 		__iget(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1137 | 		spin_unlock(&inode->i_lock); | 
 | 1138 | 	} else { | 
 | 1139 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | 		/* | 
 | 1141 | 		 * Handle the case where s_op->clear_inode is not been | 
 | 1142 | 		 * called yet, and somebody is calling igrab | 
 | 1143 | 		 * while the inode is getting freed. | 
 | 1144 | 		 */ | 
 | 1145 | 		inode = NULL; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1146 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | 	return inode; | 
 | 1148 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | EXPORT_SYMBOL(igrab); | 
 | 1150 |  | 
 | 1151 | /** | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1152 |  * ilookup5_nowait - search for an inode in the inode cache | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 |  * @sb:		super block of file system to search | 
 | 1154 |  * @hashval:	hash value (usually inode number) to search for | 
 | 1155 |  * @test:	callback used for comparisons between inodes | 
 | 1156 |  * @data:	opaque data pointer to pass to @test | 
 | 1157 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1158 |  * Search for the inode specified by @hashval and @data in the inode cache. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 |  * If the inode is in the cache, the inode is returned with an incremented | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1160 |  * reference count. | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1161 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1162 |  * Note: I_NEW is not waited upon so you have to be very careful what you do | 
 | 1163 |  * with the returned inode.  You probably should be using ilookup5() instead. | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1164 |  * | 
| Randy Dunlap | b6d0ad6 | 2011-03-26 13:27:47 -0700 | [diff] [blame] | 1165 |  * Note2: @test is called with the inode_hash_lock held, so can't sleep. | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1166 |  */ | 
 | 1167 | struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, | 
 | 1168 | 		int (*test)(struct inode *, void *), void *data) | 
 | 1169 | { | 
 | 1170 | 	struct hlist_head *head = inode_hashtable + hash(sb, hashval); | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1171 | 	struct inode *inode; | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1172 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1173 | 	spin_lock(&inode_hash_lock); | 
 | 1174 | 	inode = find_inode(sb, head, test, data); | 
 | 1175 | 	spin_unlock(&inode_hash_lock); | 
 | 1176 |  | 
 | 1177 | 	return inode; | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1178 | } | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1179 | EXPORT_SYMBOL(ilookup5_nowait); | 
 | 1180 |  | 
 | 1181 | /** | 
 | 1182 |  * ilookup5 - search for an inode in the inode cache | 
 | 1183 |  * @sb:		super block of file system to search | 
 | 1184 |  * @hashval:	hash value (usually inode number) to search for | 
 | 1185 |  * @test:	callback used for comparisons between inodes | 
 | 1186 |  * @data:	opaque data pointer to pass to @test | 
 | 1187 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1188 |  * Search for the inode specified by @hashval and @data in the inode cache, | 
 | 1189 |  * and if the inode is in the cache, return the inode with an incremented | 
 | 1190 |  * reference count.  Waits on I_NEW before returning the inode. | 
| Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 1191 |  * returned with an incremented reference count. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1193 |  * This is a generalized version of ilookup() for file systems where the | 
 | 1194 |  * inode number is not sufficient for unique identification of an inode. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1196 |  * Note: @test is called with the inode_hash_lock held, so can't sleep. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 |  */ | 
 | 1198 | struct inode *ilookup5(struct super_block *sb, unsigned long hashval, | 
 | 1199 | 		int (*test)(struct inode *, void *), void *data) | 
 | 1200 | { | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1201 | 	struct inode *inode = ilookup5_nowait(sb, hashval, test, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1203 | 	if (inode) | 
 | 1204 | 		wait_on_inode(inode); | 
 | 1205 | 	return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | EXPORT_SYMBOL(ilookup5); | 
 | 1208 |  | 
 | 1209 | /** | 
 | 1210 |  * ilookup - search for an inode in the inode cache | 
 | 1211 |  * @sb:		super block of file system to search | 
 | 1212 |  * @ino:	inode number to search for | 
 | 1213 |  * | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1214 |  * Search for the inode @ino in the inode cache, and if the inode is in the | 
 | 1215 |  * cache, the inode is returned with an incremented reference count. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 |  */ | 
 | 1217 | struct inode *ilookup(struct super_block *sb, unsigned long ino) | 
 | 1218 | { | 
 | 1219 | 	struct hlist_head *head = inode_hashtable + hash(sb, ino); | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1220 | 	struct inode *inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 |  | 
| Christoph Hellwig | 0b2d072 | 2011-03-23 15:03:28 -0400 | [diff] [blame] | 1222 | 	spin_lock(&inode_hash_lock); | 
 | 1223 | 	inode = find_inode_fast(sb, head, ino); | 
 | 1224 | 	spin_unlock(&inode_hash_lock); | 
 | 1225 |  | 
 | 1226 | 	if (inode) | 
 | 1227 | 		wait_on_inode(inode); | 
 | 1228 | 	return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | EXPORT_SYMBOL(ilookup); | 
 | 1231 |  | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1232 | int insert_inode_locked(struct inode *inode) | 
 | 1233 | { | 
 | 1234 | 	struct super_block *sb = inode->i_sb; | 
 | 1235 | 	ino_t ino = inode->i_ino; | 
 | 1236 | 	struct hlist_head *head = inode_hashtable + hash(sb, ino); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1237 |  | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1238 | 	while (1) { | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1239 | 		struct hlist_node *node; | 
 | 1240 | 		struct inode *old = NULL; | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1241 | 		spin_lock(&inode_hash_lock); | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1242 | 		hlist_for_each_entry(old, node, head, i_hash) { | 
 | 1243 | 			if (old->i_ino != ino) | 
 | 1244 | 				continue; | 
 | 1245 | 			if (old->i_sb != sb) | 
 | 1246 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1247 | 			spin_lock(&old->i_lock); | 
 | 1248 | 			if (old->i_state & (I_FREEING|I_WILL_FREE)) { | 
 | 1249 | 				spin_unlock(&old->i_lock); | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1250 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1251 | 			} | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1252 | 			break; | 
 | 1253 | 		} | 
 | 1254 | 		if (likely(!node)) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1255 | 			spin_lock(&inode->i_lock); | 
 | 1256 | 			inode->i_state |= I_NEW; | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1257 | 			hlist_add_head(&inode->i_hash, head); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1258 | 			spin_unlock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1259 | 			spin_unlock(&inode_hash_lock); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1260 | 			return 0; | 
 | 1261 | 		} | 
 | 1262 | 		__iget(old); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1263 | 		spin_unlock(&old->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1264 | 		spin_unlock(&inode_hash_lock); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1265 | 		wait_on_inode(old); | 
| Al Viro | 1d3382c | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 1266 | 		if (unlikely(!inode_unhashed(old))) { | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1267 | 			iput(old); | 
 | 1268 | 			return -EBUSY; | 
 | 1269 | 		} | 
 | 1270 | 		iput(old); | 
 | 1271 | 	} | 
 | 1272 | } | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1273 | EXPORT_SYMBOL(insert_inode_locked); | 
 | 1274 |  | 
 | 1275 | int insert_inode_locked4(struct inode *inode, unsigned long hashval, | 
 | 1276 | 		int (*test)(struct inode *, void *), void *data) | 
 | 1277 | { | 
 | 1278 | 	struct super_block *sb = inode->i_sb; | 
 | 1279 | 	struct hlist_head *head = inode_hashtable + hash(sb, hashval); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1280 |  | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1281 | 	while (1) { | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1282 | 		struct hlist_node *node; | 
 | 1283 | 		struct inode *old = NULL; | 
 | 1284 |  | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1285 | 		spin_lock(&inode_hash_lock); | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1286 | 		hlist_for_each_entry(old, node, head, i_hash) { | 
 | 1287 | 			if (old->i_sb != sb) | 
 | 1288 | 				continue; | 
 | 1289 | 			if (!test(old, data)) | 
 | 1290 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1291 | 			spin_lock(&old->i_lock); | 
 | 1292 | 			if (old->i_state & (I_FREEING|I_WILL_FREE)) { | 
 | 1293 | 				spin_unlock(&old->i_lock); | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1294 | 				continue; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1295 | 			} | 
| Al Viro | 72a43d6 | 2009-05-13 19:13:40 +0100 | [diff] [blame] | 1296 | 			break; | 
 | 1297 | 		} | 
 | 1298 | 		if (likely(!node)) { | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1299 | 			spin_lock(&inode->i_lock); | 
 | 1300 | 			inode->i_state |= I_NEW; | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1301 | 			hlist_add_head(&inode->i_hash, head); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1302 | 			spin_unlock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1303 | 			spin_unlock(&inode_hash_lock); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1304 | 			return 0; | 
 | 1305 | 		} | 
 | 1306 | 		__iget(old); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1307 | 		spin_unlock(&old->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1308 | 		spin_unlock(&inode_hash_lock); | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1309 | 		wait_on_inode(old); | 
| Al Viro | 1d3382c | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 1310 | 		if (unlikely(!inode_unhashed(old))) { | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1311 | 			iput(old); | 
 | 1312 | 			return -EBUSY; | 
 | 1313 | 		} | 
 | 1314 | 		iput(old); | 
 | 1315 | 	} | 
 | 1316 | } | 
| Al Viro | 261bca8 | 2008-12-30 01:48:21 -0500 | [diff] [blame] | 1317 | EXPORT_SYMBOL(insert_inode_locked4); | 
 | 1318 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 |  | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1320 | int generic_delete_inode(struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | { | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1322 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | EXPORT_SYMBOL(generic_delete_inode); | 
 | 1325 |  | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1326 | /* | 
 | 1327 |  * Normal UNIX filesystem behaviour: delete the | 
 | 1328 |  * inode when the usage count drops to zero, and | 
 | 1329 |  * i_nlink is zero. | 
| Jan Kara | 22fe4042 | 2009-09-18 13:05:44 -0700 | [diff] [blame] | 1330 |  */ | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1331 | int generic_drop_inode(struct inode *inode) | 
 | 1332 | { | 
| Al Viro | 1d3382c | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 1333 | 	return !inode->i_nlink || inode_unhashed(inode); | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1334 | } | 
 | 1335 | EXPORT_SYMBOL_GPL(generic_drop_inode); | 
 | 1336 |  | 
 | 1337 | /* | 
 | 1338 |  * Called when we're dropping the last reference | 
 | 1339 |  * to an inode. | 
 | 1340 |  * | 
 | 1341 |  * Call the FS "drop_inode()" function, defaulting to | 
 | 1342 |  * the legacy UNIX filesystem behaviour.  If it tells | 
 | 1343 |  * us to evict inode, do so.  Otherwise, retain inode | 
 | 1344 |  * in cache if fs is alive, sync and evict if fs is | 
 | 1345 |  * shutting down. | 
 | 1346 |  */ | 
 | 1347 | static void iput_final(struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | { | 
 | 1349 | 	struct super_block *sb = inode->i_sb; | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1350 | 	const struct super_operations *op = inode->i_sb->s_op; | 
 | 1351 | 	int drop; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 |  | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1353 | 	WARN_ON(inode->i_state & I_NEW); | 
 | 1354 |  | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1355 | 	if (op && op->drop_inode) | 
 | 1356 | 		drop = op->drop_inode(inode); | 
 | 1357 | 	else | 
 | 1358 | 		drop = generic_drop_inode(inode); | 
 | 1359 |  | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 1360 | 	if (!drop && (sb->s_flags & MS_ACTIVE)) { | 
 | 1361 | 		inode->i_state |= I_REFERENCED; | 
 | 1362 | 		if (!(inode->i_state & (I_DIRTY|I_SYNC))) | 
 | 1363 | 			inode_lru_list_add(inode); | 
 | 1364 | 		spin_unlock(&inode->i_lock); | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 1365 | 		return; | 
 | 1366 | 	} | 
 | 1367 |  | 
| Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1368 | 	if (!drop) { | 
| Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1369 | 		inode->i_state |= I_WILL_FREE; | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1370 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | 		write_inode_now(inode, 1); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1372 | 		spin_lock(&inode->i_lock); | 
| Nick Piggin | 7ef0d73 | 2009-03-12 14:31:38 -0700 | [diff] [blame] | 1373 | 		WARN_ON(inode->i_state & I_NEW); | 
| Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1374 | 		inode->i_state &= ~I_WILL_FREE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | 	} | 
| Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 1376 |  | 
| Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1377 | 	inode->i_state |= I_FREEING; | 
| Nick Piggin | 9e38d86 | 2010-10-23 06:55:17 -0400 | [diff] [blame] | 1378 | 	inode_lru_list_del(inode); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1379 | 	spin_unlock(&inode->i_lock); | 
| Dave Chinner | b2b2af8 | 2011-03-22 22:23:37 +1100 | [diff] [blame] | 1380 |  | 
 | 1381 | 	evict(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } | 
 | 1383 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | /** | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 1385 |  *	iput	- put an inode | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 |  *	@inode: inode to put | 
 | 1387 |  * | 
 | 1388 |  *	Puts an inode, dropping its usage count. If the inode use count hits | 
 | 1389 |  *	zero, the inode is then freed and may also be destroyed. | 
 | 1390 |  * | 
 | 1391 |  *	Consequently, iput() can sleep. | 
 | 1392 |  */ | 
 | 1393 | void iput(struct inode *inode) | 
 | 1394 | { | 
 | 1395 | 	if (inode) { | 
| Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 1396 | 		BUG_ON(inode->i_state & I_CLEAR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 |  | 
| Dave Chinner | f283c86 | 2011-03-22 22:23:39 +1100 | [diff] [blame] | 1398 | 		if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | 			iput_final(inode); | 
 | 1400 | 	} | 
 | 1401 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | EXPORT_SYMBOL(iput); | 
 | 1403 |  | 
 | 1404 | /** | 
 | 1405 |  *	bmap	- find a block number in a file | 
 | 1406 |  *	@inode: inode of file | 
 | 1407 |  *	@block: block to find | 
 | 1408 |  * | 
 | 1409 |  *	Returns the block number on the device holding the inode that | 
 | 1410 |  *	is the disk block number for the block of the file requested. | 
 | 1411 |  *	That is, asked for block 4 of inode 1 the function will return the | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 1412 |  *	disk block relative to the disk start that holds that block of the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 |  *	file. | 
 | 1414 |  */ | 
| Manish Katiyar | 6b3304b | 2009-03-31 19:35:54 +0530 | [diff] [blame] | 1415 | sector_t bmap(struct inode *inode, sector_t block) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | { | 
 | 1417 | 	sector_t res = 0; | 
 | 1418 | 	if (inode->i_mapping->a_ops->bmap) | 
 | 1419 | 		res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); | 
 | 1420 | 	return res; | 
 | 1421 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | EXPORT_SYMBOL(bmap); | 
 | 1423 |  | 
| Matthew Garrett | 11ff6f05 | 2009-03-26 17:32:14 +0000 | [diff] [blame] | 1424 | /* | 
 | 1425 |  * With relative atime, only update atime if the previous atime is | 
 | 1426 |  * earlier than either the ctime or mtime or if at least a day has | 
 | 1427 |  * passed since the last atime update. | 
 | 1428 |  */ | 
 | 1429 | static int relatime_need_update(struct vfsmount *mnt, struct inode *inode, | 
 | 1430 | 			     struct timespec now) | 
 | 1431 | { | 
 | 1432 |  | 
 | 1433 | 	if (!(mnt->mnt_flags & MNT_RELATIME)) | 
 | 1434 | 		return 1; | 
 | 1435 | 	/* | 
 | 1436 | 	 * Is mtime younger than atime? If yes, update atime: | 
 | 1437 | 	 */ | 
 | 1438 | 	if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0) | 
 | 1439 | 		return 1; | 
 | 1440 | 	/* | 
 | 1441 | 	 * Is ctime younger than atime? If yes, update atime: | 
 | 1442 | 	 */ | 
 | 1443 | 	if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0) | 
 | 1444 | 		return 1; | 
 | 1445 |  | 
 | 1446 | 	/* | 
 | 1447 | 	 * Is the previous atime value older than a day? If yes, | 
 | 1448 | 	 * update atime: | 
 | 1449 | 	 */ | 
 | 1450 | 	if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60) | 
 | 1451 | 		return 1; | 
 | 1452 | 	/* | 
 | 1453 | 	 * Good, we can skip the atime update: | 
 | 1454 | 	 */ | 
 | 1455 | 	return 0; | 
 | 1456 | } | 
 | 1457 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | /** | 
| Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1459 |  *	touch_atime	-	update the access time | 
 | 1460 |  *	@mnt: mount the inode is accessed on | 
| Martin Waitz | 7045f37 | 2006-02-01 03:06:57 -0800 | [diff] [blame] | 1461 |  *	@dentry: dentry accessed | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 |  * | 
 | 1463 |  *	Update the accessed time on an inode and mark it for writeback. | 
 | 1464 |  *	This function automatically handles read only file systems and media, | 
 | 1465 |  *	as well as the "noatime" flag and inode specific "noatime" markers. | 
 | 1466 |  */ | 
| Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1467 | void touch_atime(struct vfsmount *mnt, struct dentry *dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | { | 
| Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1469 | 	struct inode *inode = dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | 	struct timespec now; | 
 | 1471 |  | 
| Andrew Morton | b227613 | 2006-12-13 00:34:33 -0800 | [diff] [blame] | 1472 | 	if (inode->i_flags & S_NOATIME) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1473 | 		return; | 
| Eric Dumazet | 37756ce | 2007-02-10 01:44:49 -0800 | [diff] [blame] | 1474 | 	if (IS_NOATIME(inode)) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1475 | 		return; | 
| Andrew Morton | b227613 | 2006-12-13 00:34:33 -0800 | [diff] [blame] | 1476 | 	if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1477 | 		return; | 
| Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 1478 |  | 
| Dave Hansen | cdb70f3 | 2008-02-15 14:37:41 -0800 | [diff] [blame] | 1479 | 	if (mnt->mnt_flags & MNT_NOATIME) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1480 | 		return; | 
| Dave Hansen | cdb70f3 | 2008-02-15 14:37:41 -0800 | [diff] [blame] | 1481 | 	if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1482 | 		return; | 
| Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 1483 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | 	now = current_fs_time(inode->i_sb); | 
| Matthew Garrett | 11ff6f05 | 2009-03-26 17:32:14 +0000 | [diff] [blame] | 1485 |  | 
 | 1486 | 	if (!relatime_need_update(mnt, inode, now)) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1487 | 		return; | 
| Matthew Garrett | 11ff6f05 | 2009-03-26 17:32:14 +0000 | [diff] [blame] | 1488 |  | 
| Valerie Henson | 47ae32d | 2006-12-13 00:34:34 -0800 | [diff] [blame] | 1489 | 	if (timespec_equal(&inode->i_atime, &now)) | 
| Andi Kleen | b12536c | 2009-09-18 13:05:47 -0700 | [diff] [blame] | 1490 | 		return; | 
 | 1491 |  | 
 | 1492 | 	if (mnt_want_write(mnt)) | 
 | 1493 | 		return; | 
| Valerie Henson | 47ae32d | 2006-12-13 00:34:34 -0800 | [diff] [blame] | 1494 |  | 
 | 1495 | 	inode->i_atime = now; | 
 | 1496 | 	mark_inode_dirty_sync(inode); | 
| Dave Hansen | cdb70f3 | 2008-02-15 14:37:41 -0800 | [diff] [blame] | 1497 | 	mnt_drop_write(mnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } | 
| Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1499 | EXPORT_SYMBOL(touch_atime); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 |  | 
 | 1501 | /** | 
| Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1502 |  *	file_update_time	-	update mtime and ctime time | 
 | 1503 |  *	@file: file accessed | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 |  * | 
| Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1505 |  *	Update the mtime and ctime members of an inode and mark the inode | 
 | 1506 |  *	for writeback.  Note that this function is meant exclusively for | 
 | 1507 |  *	usage in the file write path of filesystems, and filesystems may | 
 | 1508 |  *	choose to explicitly ignore update via this function with the | 
| Wolfram Sang | 2eadfc0 | 2009-04-02 15:23:37 +0200 | [diff] [blame] | 1509 |  *	S_NOCMTIME inode flag, e.g. for network filesystem where these | 
| Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1510 |  *	timestamps are handled by the server. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 |  */ | 
 | 1512 |  | 
| Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1513 | void file_update_time(struct file *file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | { | 
| Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1515 | 	struct inode *inode = file->f_path.dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | 	struct timespec now; | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1517 | 	enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 |  | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1519 | 	/* First try to exhaust all avenues to not sync */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | 	if (IS_NOCMTIME(inode)) | 
 | 1521 | 		return; | 
| Dave Hansen | 20ddee2 | 2008-02-15 14:37:43 -0800 | [diff] [blame] | 1522 |  | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1523 | 	now = current_fs_time(inode->i_sb); | 
 | 1524 | 	if (!timespec_equal(&inode->i_mtime, &now)) | 
 | 1525 | 		sync_it = S_MTIME; | 
 | 1526 |  | 
 | 1527 | 	if (!timespec_equal(&inode->i_ctime, &now)) | 
 | 1528 | 		sync_it |= S_CTIME; | 
 | 1529 |  | 
 | 1530 | 	if (IS_I_VERSION(inode)) | 
 | 1531 | 		sync_it |= S_VERSION; | 
 | 1532 |  | 
 | 1533 | 	if (!sync_it) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | 		return; | 
 | 1535 |  | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1536 | 	/* Finally allowed to write? Takes lock. */ | 
 | 1537 | 	if (mnt_want_write_file(file)) | 
 | 1538 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 |  | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1540 | 	/* Only change inode inside the lock region */ | 
 | 1541 | 	if (sync_it & S_VERSION) | 
| Jean Noel Cordenner | 7a22422 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1542 | 		inode_inc_iversion(inode); | 
| Andi Kleen | ce06e0b | 2009-09-18 13:05:48 -0700 | [diff] [blame] | 1543 | 	if (sync_it & S_CTIME) | 
 | 1544 | 		inode->i_ctime = now; | 
 | 1545 | 	if (sync_it & S_MTIME) | 
 | 1546 | 		inode->i_mtime = now; | 
 | 1547 | 	mark_inode_dirty_sync(inode); | 
| Dave Hansen | 20ddee2 | 2008-02-15 14:37:43 -0800 | [diff] [blame] | 1548 | 	mnt_drop_write(file->f_path.mnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | } | 
| Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1550 | EXPORT_SYMBOL(file_update_time); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 |  | 
 | 1552 | int inode_needs_sync(struct inode *inode) | 
 | 1553 | { | 
 | 1554 | 	if (IS_SYNC(inode)) | 
 | 1555 | 		return 1; | 
 | 1556 | 	if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) | 
 | 1557 | 		return 1; | 
 | 1558 | 	return 0; | 
 | 1559 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | EXPORT_SYMBOL(inode_needs_sync); | 
 | 1561 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | int inode_wait(void *word) | 
 | 1563 | { | 
 | 1564 | 	schedule(); | 
 | 1565 | 	return 0; | 
 | 1566 | } | 
| Stephen Rothwell | d44dab8 | 2008-11-10 17:06:05 +1100 | [diff] [blame] | 1567 | EXPORT_SYMBOL(inode_wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 |  | 
 | 1569 | /* | 
| Miklos Szeredi | 168a9fd | 2005-07-12 13:58:10 -0700 | [diff] [blame] | 1570 |  * If we try to find an inode in the inode hash while it is being | 
 | 1571 |  * deleted, we have to wait until the filesystem completes its | 
 | 1572 |  * deletion before reporting that it isn't found.  This function waits | 
 | 1573 |  * until the deletion _might_ have completed.  Callers are responsible | 
 | 1574 |  * to recheck inode state. | 
 | 1575 |  * | 
| Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 1576 |  * It doesn't matter if I_NEW is not set initially, a call to | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1577 |  * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list | 
 | 1578 |  * will DTRT. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 |  */ | 
 | 1580 | static void __wait_on_freeing_inode(struct inode *inode) | 
 | 1581 | { | 
 | 1582 | 	wait_queue_head_t *wq; | 
| Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 1583 | 	DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW); | 
 | 1584 | 	wq = bit_waitqueue(&inode->i_state, __I_NEW); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | 	prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | 
| Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 1586 | 	spin_unlock(&inode->i_lock); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1587 | 	spin_unlock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | 	schedule(); | 
 | 1589 | 	finish_wait(wq, &wait.wait); | 
| Dave Chinner | 67a23c4 | 2011-03-22 22:23:42 +1100 | [diff] [blame] | 1590 | 	spin_lock(&inode_hash_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } | 
 | 1592 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | static __initdata unsigned long ihash_entries; | 
 | 1594 | static int __init set_ihash_entries(char *str) | 
 | 1595 | { | 
 | 1596 | 	if (!str) | 
 | 1597 | 		return 0; | 
 | 1598 | 	ihash_entries = simple_strtoul(str, &str, 0); | 
 | 1599 | 	return 1; | 
 | 1600 | } | 
 | 1601 | __setup("ihash_entries=", set_ihash_entries); | 
 | 1602 |  | 
 | 1603 | /* | 
 | 1604 |  * Initialize the waitqueues and inode hash table. | 
 | 1605 |  */ | 
 | 1606 | void __init inode_init_early(void) | 
 | 1607 | { | 
 | 1608 | 	int loop; | 
 | 1609 |  | 
 | 1610 | 	/* If hashes are distributed across NUMA nodes, defer | 
 | 1611 | 	 * hash allocation until vmalloc space is available. | 
 | 1612 | 	 */ | 
 | 1613 | 	if (hashdist) | 
 | 1614 | 		return; | 
 | 1615 |  | 
 | 1616 | 	inode_hashtable = | 
 | 1617 | 		alloc_large_system_hash("Inode-cache", | 
 | 1618 | 					sizeof(struct hlist_head), | 
 | 1619 | 					ihash_entries, | 
 | 1620 | 					14, | 
 | 1621 | 					HASH_EARLY, | 
 | 1622 | 					&i_hash_shift, | 
 | 1623 | 					&i_hash_mask, | 
 | 1624 | 					0); | 
 | 1625 |  | 
 | 1626 | 	for (loop = 0; loop < (1 << i_hash_shift); loop++) | 
 | 1627 | 		INIT_HLIST_HEAD(&inode_hashtable[loop]); | 
 | 1628 | } | 
 | 1629 |  | 
| Denis Cheng | 74bf17c | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 1630 | void __init inode_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | { | 
 | 1632 | 	int loop; | 
 | 1633 |  | 
 | 1634 | 	/* inode slab cache */ | 
| Paul Jackson | b019600 | 2006-03-24 03:16:09 -0800 | [diff] [blame] | 1635 | 	inode_cachep = kmem_cache_create("inode_cache", | 
 | 1636 | 					 sizeof(struct inode), | 
 | 1637 | 					 0, | 
 | 1638 | 					 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| | 
 | 1639 | 					 SLAB_MEM_SPREAD), | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1640 | 					 init_once); | 
| Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 1641 | 	register_shrinker(&icache_shrinker); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 |  | 
 | 1643 | 	/* Hash may have been set up in inode_init_early */ | 
 | 1644 | 	if (!hashdist) | 
 | 1645 | 		return; | 
 | 1646 |  | 
 | 1647 | 	inode_hashtable = | 
 | 1648 | 		alloc_large_system_hash("Inode-cache", | 
 | 1649 | 					sizeof(struct hlist_head), | 
 | 1650 | 					ihash_entries, | 
 | 1651 | 					14, | 
 | 1652 | 					0, | 
 | 1653 | 					&i_hash_shift, | 
 | 1654 | 					&i_hash_mask, | 
 | 1655 | 					0); | 
 | 1656 |  | 
 | 1657 | 	for (loop = 0; loop < (1 << i_hash_shift); loop++) | 
 | 1658 | 		INIT_HLIST_HEAD(&inode_hashtable[loop]); | 
 | 1659 | } | 
 | 1660 |  | 
 | 1661 | void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) | 
 | 1662 | { | 
 | 1663 | 	inode->i_mode = mode; | 
 | 1664 | 	if (S_ISCHR(mode)) { | 
 | 1665 | 		inode->i_fop = &def_chr_fops; | 
 | 1666 | 		inode->i_rdev = rdev; | 
 | 1667 | 	} else if (S_ISBLK(mode)) { | 
 | 1668 | 		inode->i_fop = &def_blk_fops; | 
 | 1669 | 		inode->i_rdev = rdev; | 
 | 1670 | 	} else if (S_ISFIFO(mode)) | 
 | 1671 | 		inode->i_fop = &def_fifo_fops; | 
 | 1672 | 	else if (S_ISSOCK(mode)) | 
 | 1673 | 		inode->i_fop = &bad_sock_fops; | 
 | 1674 | 	else | 
| Manish Katiyar | af0d9ae | 2009-09-18 13:05:43 -0700 | [diff] [blame] | 1675 | 		printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for" | 
 | 1676 | 				  " inode %s:%lu\n", mode, inode->i_sb->s_id, | 
 | 1677 | 				  inode->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | } | 
 | 1679 | EXPORT_SYMBOL(init_special_inode); | 
| Dmitry Monakhov | a1bd120 | 2010-03-04 17:29:14 +0300 | [diff] [blame] | 1680 |  | 
 | 1681 | /** | 
| Ben Hutchings | eaae668 | 2011-02-15 12:48:09 +0000 | [diff] [blame] | 1682 |  * inode_init_owner - Init uid,gid,mode for new inode according to posix standards | 
| Dmitry Monakhov | a1bd120 | 2010-03-04 17:29:14 +0300 | [diff] [blame] | 1683 |  * @inode: New inode | 
 | 1684 |  * @dir: Directory inode | 
 | 1685 |  * @mode: mode of the new inode | 
 | 1686 |  */ | 
 | 1687 | void inode_init_owner(struct inode *inode, const struct inode *dir, | 
 | 1688 | 			mode_t mode) | 
 | 1689 | { | 
 | 1690 | 	inode->i_uid = current_fsuid(); | 
 | 1691 | 	if (dir && dir->i_mode & S_ISGID) { | 
 | 1692 | 		inode->i_gid = dir->i_gid; | 
 | 1693 | 		if (S_ISDIR(mode)) | 
 | 1694 | 			mode |= S_ISGID; | 
 | 1695 | 	} else | 
 | 1696 | 		inode->i_gid = current_fsgid(); | 
 | 1697 | 	inode->i_mode = mode; | 
 | 1698 | } | 
 | 1699 | EXPORT_SYMBOL(inode_init_owner); | 
| Serge E. Hallyn | e795b71 | 2011-03-23 16:43:25 -0700 | [diff] [blame] | 1700 |  | 
| Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 1701 | /** | 
 | 1702 |  * inode_owner_or_capable - check current task permissions to inode | 
 | 1703 |  * @inode: inode being checked | 
 | 1704 |  * | 
 | 1705 |  * Return true if current either has CAP_FOWNER to the inode, or | 
 | 1706 |  * owns the file. | 
| Serge E. Hallyn | e795b71 | 2011-03-23 16:43:25 -0700 | [diff] [blame] | 1707 |  */ | 
| Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 1708 | bool inode_owner_or_capable(const struct inode *inode) | 
| Serge E. Hallyn | e795b71 | 2011-03-23 16:43:25 -0700 | [diff] [blame] | 1709 | { | 
 | 1710 | 	struct user_namespace *ns = inode_userns(inode); | 
 | 1711 |  | 
 | 1712 | 	if (current_user_ns() == ns && current_fsuid() == inode->i_uid) | 
 | 1713 | 		return true; | 
 | 1714 | 	if (ns_capable(ns, CAP_FOWNER)) | 
 | 1715 | 		return true; | 
 | 1716 | 	return false; | 
 | 1717 | } | 
| Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 1718 | EXPORT_SYMBOL(inode_owner_or_capable); |