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