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