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