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