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