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