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