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