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