1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * (C) 1997 Linus Torvalds 2 * (C) 1997 Linus Torvalds 4 * (C) 1999 Andrea Arcangeli <andrea@suse.de> 3 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation) 5 */ 4 */ 6 #include <linux/export.h> 5 #include <linux/export.h> 7 #include <linux/fs.h> 6 #include <linux/fs.h> 8 #include <linux/filelock.h> << 9 #include <linux/mm.h> 7 #include <linux/mm.h> 10 #include <linux/backing-dev.h> 8 #include <linux/backing-dev.h> 11 #include <linux/hash.h> 9 #include <linux/hash.h> 12 #include <linux/swap.h> 10 #include <linux/swap.h> 13 #include <linux/security.h> 11 #include <linux/security.h> 14 #include <linux/cdev.h> 12 #include <linux/cdev.h> 15 #include <linux/memblock.h> !! 13 #include <linux/bootmem.h> 16 #include <linux/fsnotify.h> 14 #include <linux/fsnotify.h> 17 #include <linux/mount.h> 15 #include <linux/mount.h> 18 #include <linux/posix_acl.h> 16 #include <linux/posix_acl.h> >> 17 #include <linux/prefetch.h> 19 #include <linux/buffer_head.h> /* for inode_ha 18 #include <linux/buffer_head.h> /* for inode_has_buffers */ 20 #include <linux/ratelimit.h> 19 #include <linux/ratelimit.h> 21 #include <linux/list_lru.h> 20 #include <linux/list_lru.h> 22 #include <linux/iversion.h> 21 #include <linux/iversion.h> 23 #include <linux/rw_hint.h> << 24 #include <trace/events/writeback.h> 22 #include <trace/events/writeback.h> 25 #include "internal.h" 23 #include "internal.h" 26 24 27 /* 25 /* 28 * Inode locking rules: 26 * Inode locking rules: 29 * 27 * 30 * inode->i_lock protects: 28 * inode->i_lock protects: 31 * inode->i_state, inode->i_hash, __iget(), !! 29 * inode->i_state, inode->i_hash, __iget() 32 * Inode LRU list locks protect: 30 * Inode LRU list locks protect: 33 * inode->i_sb->s_inode_lru, inode->i_lru 31 * inode->i_sb->s_inode_lru, inode->i_lru 34 * inode->i_sb->s_inode_list_lock protects: 32 * inode->i_sb->s_inode_list_lock protects: 35 * inode->i_sb->s_inodes, inode->i_sb_list 33 * inode->i_sb->s_inodes, inode->i_sb_list 36 * bdi->wb.list_lock protects: 34 * bdi->wb.list_lock protects: 37 * bdi->wb.b_{dirty,io,more_io,dirty_time}, 35 * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list 38 * inode_hash_lock protects: 36 * inode_hash_lock protects: 39 * inode_hashtable, inode->i_hash 37 * inode_hashtable, inode->i_hash 40 * 38 * 41 * Lock ordering: 39 * Lock ordering: 42 * 40 * 43 * inode->i_sb->s_inode_list_lock 41 * inode->i_sb->s_inode_list_lock 44 * inode->i_lock 42 * inode->i_lock 45 * Inode LRU list locks 43 * Inode LRU list locks 46 * 44 * 47 * bdi->wb.list_lock 45 * bdi->wb.list_lock 48 * inode->i_lock 46 * inode->i_lock 49 * 47 * 50 * inode_hash_lock 48 * inode_hash_lock 51 * inode->i_sb->s_inode_list_lock 49 * inode->i_sb->s_inode_list_lock 52 * inode->i_lock 50 * inode->i_lock 53 * 51 * 54 * iunique_lock 52 * iunique_lock 55 * inode_hash_lock 53 * inode_hash_lock 56 */ 54 */ 57 55 58 static unsigned int i_hash_mask __ro_after_ini !! 56 static unsigned int i_hash_mask __read_mostly; 59 static unsigned int i_hash_shift __ro_after_in !! 57 static unsigned int i_hash_shift __read_mostly; 60 static struct hlist_head *inode_hashtable __ro !! 58 static struct hlist_head *inode_hashtable __read_mostly; 61 static __cacheline_aligned_in_smp DEFINE_SPINL 59 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); 62 60 63 /* 61 /* 64 * Empty aops. Can be used for the cases where 62 * Empty aops. Can be used for the cases where the user does not 65 * define any of the address_space operations. 63 * define any of the address_space operations. 66 */ 64 */ 67 const struct address_space_operations empty_ao 65 const struct address_space_operations empty_aops = { 68 }; 66 }; 69 EXPORT_SYMBOL(empty_aops); 67 EXPORT_SYMBOL(empty_aops); 70 68 >> 69 /* >> 70 * Statistics gathering.. >> 71 */ >> 72 struct inodes_stat_t inodes_stat; >> 73 71 static DEFINE_PER_CPU(unsigned long, nr_inodes 74 static DEFINE_PER_CPU(unsigned long, nr_inodes); 72 static DEFINE_PER_CPU(unsigned long, nr_unused 75 static DEFINE_PER_CPU(unsigned long, nr_unused); 73 76 74 static struct kmem_cache *inode_cachep __ro_af !! 77 static struct kmem_cache *inode_cachep __read_mostly; 75 78 76 static long get_nr_inodes(void) 79 static long get_nr_inodes(void) 77 { 80 { 78 int i; 81 int i; 79 long sum = 0; 82 long sum = 0; 80 for_each_possible_cpu(i) 83 for_each_possible_cpu(i) 81 sum += per_cpu(nr_inodes, i); 84 sum += per_cpu(nr_inodes, i); 82 return sum < 0 ? 0 : sum; 85 return sum < 0 ? 0 : sum; 83 } 86 } 84 87 85 static inline long get_nr_inodes_unused(void) 88 static inline long get_nr_inodes_unused(void) 86 { 89 { 87 int i; 90 int i; 88 long sum = 0; 91 long sum = 0; 89 for_each_possible_cpu(i) 92 for_each_possible_cpu(i) 90 sum += per_cpu(nr_unused, i); 93 sum += per_cpu(nr_unused, i); 91 return sum < 0 ? 0 : sum; 94 return sum < 0 ? 0 : sum; 92 } 95 } 93 96 94 long get_nr_dirty_inodes(void) 97 long get_nr_dirty_inodes(void) 95 { 98 { 96 /* not actually dirty inodes, but a wi 99 /* not actually dirty inodes, but a wild approximation */ 97 long nr_dirty = get_nr_inodes() - get_ 100 long nr_dirty = get_nr_inodes() - get_nr_inodes_unused(); 98 return nr_dirty > 0 ? nr_dirty : 0; 101 return nr_dirty > 0 ? nr_dirty : 0; 99 } 102 } 100 103 101 /* 104 /* 102 * Handle nr_inode sysctl 105 * Handle nr_inode sysctl 103 */ 106 */ 104 #ifdef CONFIG_SYSCTL 107 #ifdef CONFIG_SYSCTL 105 /* !! 108 int proc_nr_inodes(struct ctl_table *table, int write, 106 * Statistics gathering.. !! 109 void __user *buffer, size_t *lenp, loff_t *ppos) 107 */ << 108 static struct inodes_stat_t inodes_stat; << 109 << 110 static int proc_nr_inodes(const struct ctl_tab << 111 size_t *lenp, loff_t << 112 { 110 { 113 inodes_stat.nr_inodes = get_nr_inodes( 111 inodes_stat.nr_inodes = get_nr_inodes(); 114 inodes_stat.nr_unused = get_nr_inodes_ 112 inodes_stat.nr_unused = get_nr_inodes_unused(); 115 return proc_doulongvec_minmax(table, w 113 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 116 } 114 } 117 << 118 static struct ctl_table inodes_sysctls[] = { << 119 { << 120 .procname = "inode-nr", << 121 .data = &inodes_stat << 122 .maxlen = 2*sizeof(lon << 123 .mode = 0444, << 124 .proc_handler = proc_nr_inod << 125 }, << 126 { << 127 .procname = "inode-state << 128 .data = &inodes_stat << 129 .maxlen = 7*sizeof(lon << 130 .mode = 0444, << 131 .proc_handler = proc_nr_inod << 132 }, << 133 }; << 134 << 135 static int __init init_fs_inode_sysctls(void) << 136 { << 137 register_sysctl_init("fs", inodes_sysc << 138 return 0; << 139 } << 140 early_initcall(init_fs_inode_sysctls); << 141 #endif 115 #endif 142 116 143 static int no_open(struct inode *inode, struct 117 static int no_open(struct inode *inode, struct file *file) 144 { 118 { 145 return -ENXIO; 119 return -ENXIO; 146 } 120 } 147 121 148 /** 122 /** 149 * inode_init_always_gfp - perform inode struc !! 123 * inode_init_always - perform inode structure initialisation 150 * @sb: superblock inode belongs to 124 * @sb: superblock inode belongs to 151 * @inode: inode to initialise 125 * @inode: inode to initialise 152 * @gfp: allocation flags << 153 * 126 * 154 * These are initializations that need to be d 127 * These are initializations that need to be done on every inode 155 * allocation as the fields are not initialise 128 * allocation as the fields are not initialised by slab allocation. 156 * If there are additional allocations require << 157 */ 129 */ 158 int inode_init_always_gfp(struct super_block * !! 130 int inode_init_always(struct super_block *sb, struct inode *inode) 159 { 131 { 160 static const struct inode_operations e 132 static const struct inode_operations empty_iops; 161 static const struct file_operations no 133 static const struct file_operations no_open_fops = {.open = no_open}; 162 struct address_space *const mapping = 134 struct address_space *const mapping = &inode->i_data; 163 135 164 inode->i_sb = sb; 136 inode->i_sb = sb; 165 inode->i_blkbits = sb->s_blocksize_bit 137 inode->i_blkbits = sb->s_blocksize_bits; 166 inode->i_flags = 0; 138 inode->i_flags = 0; 167 inode->i_state = 0; << 168 atomic64_set(&inode->i_sequence, 0); << 169 atomic_set(&inode->i_count, 1); 139 atomic_set(&inode->i_count, 1); 170 inode->i_op = &empty_iops; 140 inode->i_op = &empty_iops; 171 inode->i_fop = &no_open_fops; 141 inode->i_fop = &no_open_fops; 172 inode->i_ino = 0; << 173 inode->__i_nlink = 1; 142 inode->__i_nlink = 1; 174 inode->i_opflags = 0; 143 inode->i_opflags = 0; 175 if (sb->s_xattr) 144 if (sb->s_xattr) 176 inode->i_opflags |= IOP_XATTR; 145 inode->i_opflags |= IOP_XATTR; 177 i_uid_write(inode, 0); 146 i_uid_write(inode, 0); 178 i_gid_write(inode, 0); 147 i_gid_write(inode, 0); 179 atomic_set(&inode->i_writecount, 0); 148 atomic_set(&inode->i_writecount, 0); 180 inode->i_size = 0; 149 inode->i_size = 0; 181 inode->i_write_hint = WRITE_LIFE_NOT_S 150 inode->i_write_hint = WRITE_LIFE_NOT_SET; 182 inode->i_blocks = 0; 151 inode->i_blocks = 0; 183 inode->i_bytes = 0; 152 inode->i_bytes = 0; 184 inode->i_generation = 0; 153 inode->i_generation = 0; 185 inode->i_pipe = NULL; 154 inode->i_pipe = NULL; >> 155 inode->i_bdev = NULL; 186 inode->i_cdev = NULL; 156 inode->i_cdev = NULL; 187 inode->i_link = NULL; 157 inode->i_link = NULL; 188 inode->i_dir_seq = 0; 158 inode->i_dir_seq = 0; 189 inode->i_rdev = 0; 159 inode->i_rdev = 0; 190 inode->dirtied_when = 0; 160 inode->dirtied_when = 0; 191 161 192 #ifdef CONFIG_CGROUP_WRITEBACK 162 #ifdef CONFIG_CGROUP_WRITEBACK 193 inode->i_wb_frn_winner = 0; 163 inode->i_wb_frn_winner = 0; 194 inode->i_wb_frn_avg_time = 0; 164 inode->i_wb_frn_avg_time = 0; 195 inode->i_wb_frn_history = 0; 165 inode->i_wb_frn_history = 0; 196 #endif 166 #endif 197 167 >> 168 if (security_inode_alloc(inode)) >> 169 goto out; 198 spin_lock_init(&inode->i_lock); 170 spin_lock_init(&inode->i_lock); 199 lockdep_set_class(&inode->i_lock, &sb- 171 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); 200 172 201 init_rwsem(&inode->i_rwsem); 173 init_rwsem(&inode->i_rwsem); 202 lockdep_set_class(&inode->i_rwsem, &sb 174 lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key); 203 175 204 atomic_set(&inode->i_dio_count, 0); 176 atomic_set(&inode->i_dio_count, 0); 205 177 206 mapping->a_ops = &empty_aops; 178 mapping->a_ops = &empty_aops; 207 mapping->host = inode; 179 mapping->host = inode; 208 mapping->flags = 0; 180 mapping->flags = 0; 209 mapping->wb_err = 0; 181 mapping->wb_err = 0; 210 atomic_set(&mapping->i_mmap_writable, 182 atomic_set(&mapping->i_mmap_writable, 0); 211 #ifdef CONFIG_READ_ONLY_THP_FOR_FS << 212 atomic_set(&mapping->nr_thps, 0); << 213 #endif << 214 mapping_set_gfp_mask(mapping, GFP_HIGH 183 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE); 215 mapping->i_private_data = NULL; !! 184 mapping->private_data = NULL; 216 mapping->writeback_index = 0; 185 mapping->writeback_index = 0; 217 init_rwsem(&mapping->invalidate_lock); << 218 lockdep_set_class_and_name(&mapping->i << 219 &sb->s_type << 220 "mapping.in << 221 if (sb->s_iflags & SB_I_STABLE_WRITES) << 222 mapping_set_stable_writes(mapp << 223 inode->i_private = NULL; 186 inode->i_private = NULL; 224 inode->i_mapping = mapping; 187 inode->i_mapping = mapping; 225 INIT_HLIST_HEAD(&inode->i_dentry); 188 INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */ 226 #ifdef CONFIG_FS_POSIX_ACL 189 #ifdef CONFIG_FS_POSIX_ACL 227 inode->i_acl = inode->i_default_acl = 190 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; 228 #endif 191 #endif 229 192 230 #ifdef CONFIG_FSNOTIFY 193 #ifdef CONFIG_FSNOTIFY 231 inode->i_fsnotify_mask = 0; 194 inode->i_fsnotify_mask = 0; 232 #endif 195 #endif 233 inode->i_flctx = NULL; 196 inode->i_flctx = NULL; 234 << 235 if (unlikely(security_inode_alloc(inod << 236 return -ENOMEM; << 237 << 238 this_cpu_inc(nr_inodes); 197 this_cpu_inc(nr_inodes); 239 198 240 return 0; 199 return 0; >> 200 out: >> 201 return -ENOMEM; 241 } 202 } 242 EXPORT_SYMBOL(inode_init_always_gfp); !! 203 EXPORT_SYMBOL(inode_init_always); 243 << 244 void free_inode_nonrcu(struct inode *inode) << 245 { << 246 kmem_cache_free(inode_cachep, inode); << 247 } << 248 EXPORT_SYMBOL(free_inode_nonrcu); << 249 << 250 static void i_callback(struct rcu_head *head) << 251 { << 252 struct inode *inode = container_of(hea << 253 if (inode->free_inode) << 254 inode->free_inode(inode); << 255 else << 256 free_inode_nonrcu(inode); << 257 } << 258 204 259 static struct inode *alloc_inode(struct super_ 205 static struct inode *alloc_inode(struct super_block *sb) 260 { 206 { 261 const struct super_operations *ops = s << 262 struct inode *inode; 207 struct inode *inode; 263 208 264 if (ops->alloc_inode) !! 209 if (sb->s_op->alloc_inode) 265 inode = ops->alloc_inode(sb); !! 210 inode = sb->s_op->alloc_inode(sb); 266 else 211 else 267 inode = alloc_inode_sb(sb, ino !! 212 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL); 268 213 269 if (!inode) 214 if (!inode) 270 return NULL; 215 return NULL; 271 216 272 if (unlikely(inode_init_always(sb, ino 217 if (unlikely(inode_init_always(sb, inode))) { 273 if (ops->destroy_inode) { !! 218 if (inode->i_sb->s_op->destroy_inode) 274 ops->destroy_inode(ino !! 219 inode->i_sb->s_op->destroy_inode(inode); 275 if (!ops->free_inode) !! 220 else 276 return NULL; !! 221 kmem_cache_free(inode_cachep, inode); 277 } << 278 inode->free_inode = ops->free_ << 279 i_callback(&inode->i_rcu); << 280 return NULL; 222 return NULL; 281 } 223 } 282 224 283 return inode; 225 return inode; 284 } 226 } 285 227 >> 228 void free_inode_nonrcu(struct inode *inode) >> 229 { >> 230 kmem_cache_free(inode_cachep, inode); >> 231 } >> 232 EXPORT_SYMBOL(free_inode_nonrcu); >> 233 286 void __destroy_inode(struct inode *inode) 234 void __destroy_inode(struct inode *inode) 287 { 235 { 288 BUG_ON(inode_has_buffers(inode)); 236 BUG_ON(inode_has_buffers(inode)); 289 inode_detach_wb(inode); 237 inode_detach_wb(inode); 290 security_inode_free(inode); 238 security_inode_free(inode); 291 fsnotify_inode_delete(inode); 239 fsnotify_inode_delete(inode); 292 locks_free_lock_context(inode); 240 locks_free_lock_context(inode); 293 if (!inode->i_nlink) { 241 if (!inode->i_nlink) { 294 WARN_ON(atomic_long_read(&inod 242 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0); 295 atomic_long_dec(&inode->i_sb-> 243 atomic_long_dec(&inode->i_sb->s_remove_count); 296 } 244 } 297 245 298 #ifdef CONFIG_FS_POSIX_ACL 246 #ifdef CONFIG_FS_POSIX_ACL 299 if (inode->i_acl && !is_uncached_acl(i 247 if (inode->i_acl && !is_uncached_acl(inode->i_acl)) 300 posix_acl_release(inode->i_acl 248 posix_acl_release(inode->i_acl); 301 if (inode->i_default_acl && !is_uncach 249 if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl)) 302 posix_acl_release(inode->i_def 250 posix_acl_release(inode->i_default_acl); 303 #endif 251 #endif 304 this_cpu_dec(nr_inodes); 252 this_cpu_dec(nr_inodes); 305 } 253 } 306 EXPORT_SYMBOL(__destroy_inode); 254 EXPORT_SYMBOL(__destroy_inode); 307 255 308 static void destroy_inode(struct inode *inode) !! 256 static void i_callback(struct rcu_head *head) 309 { 257 { 310 const struct super_operations *ops = i !! 258 struct inode *inode = container_of(head, struct inode, i_rcu); >> 259 kmem_cache_free(inode_cachep, inode); >> 260 } 311 261 >> 262 static void destroy_inode(struct inode *inode) >> 263 { 312 BUG_ON(!list_empty(&inode->i_lru)); 264 BUG_ON(!list_empty(&inode->i_lru)); 313 __destroy_inode(inode); 265 __destroy_inode(inode); 314 if (ops->destroy_inode) { !! 266 if (inode->i_sb->s_op->destroy_inode) 315 ops->destroy_inode(inode); !! 267 inode->i_sb->s_op->destroy_inode(inode); 316 if (!ops->free_inode) !! 268 else 317 return; !! 269 call_rcu(&inode->i_rcu, i_callback); 318 } << 319 inode->free_inode = ops->free_inode; << 320 call_rcu(&inode->i_rcu, i_callback); << 321 } 270 } 322 271 323 /** 272 /** 324 * drop_nlink - directly drop an inode's link 273 * drop_nlink - directly drop an inode's link count 325 * @inode: inode 274 * @inode: inode 326 * 275 * 327 * This is a low-level filesystem helper to re 276 * This is a low-level filesystem helper to replace any 328 * direct filesystem manipulation of i_nlink. 277 * direct filesystem manipulation of i_nlink. In cases 329 * where we are attempting to track writes to 278 * where we are attempting to track writes to the 330 * filesystem, a decrement to zero means an im 279 * filesystem, a decrement to zero means an imminent 331 * write when the file is truncated and actual 280 * write when the file is truncated and actually unlinked 332 * on the filesystem. 281 * on the filesystem. 333 */ 282 */ 334 void drop_nlink(struct inode *inode) 283 void drop_nlink(struct inode *inode) 335 { 284 { 336 WARN_ON(inode->i_nlink == 0); 285 WARN_ON(inode->i_nlink == 0); 337 inode->__i_nlink--; 286 inode->__i_nlink--; 338 if (!inode->i_nlink) 287 if (!inode->i_nlink) 339 atomic_long_inc(&inode->i_sb-> 288 atomic_long_inc(&inode->i_sb->s_remove_count); 340 } 289 } 341 EXPORT_SYMBOL(drop_nlink); 290 EXPORT_SYMBOL(drop_nlink); 342 291 343 /** 292 /** 344 * clear_nlink - directly zero an inode's link 293 * clear_nlink - directly zero an inode's link count 345 * @inode: inode 294 * @inode: inode 346 * 295 * 347 * This is a low-level filesystem helper to re 296 * This is a low-level filesystem helper to replace any 348 * direct filesystem manipulation of i_nlink. 297 * direct filesystem manipulation of i_nlink. See 349 * drop_nlink() for why we care about i_nlink 298 * drop_nlink() for why we care about i_nlink hitting zero. 350 */ 299 */ 351 void clear_nlink(struct inode *inode) 300 void clear_nlink(struct inode *inode) 352 { 301 { 353 if (inode->i_nlink) { 302 if (inode->i_nlink) { 354 inode->__i_nlink = 0; 303 inode->__i_nlink = 0; 355 atomic_long_inc(&inode->i_sb-> 304 atomic_long_inc(&inode->i_sb->s_remove_count); 356 } 305 } 357 } 306 } 358 EXPORT_SYMBOL(clear_nlink); 307 EXPORT_SYMBOL(clear_nlink); 359 308 360 /** 309 /** 361 * set_nlink - directly set an inode's link co 310 * set_nlink - directly set an inode's link count 362 * @inode: inode 311 * @inode: inode 363 * @nlink: new nlink (should be non-zero) 312 * @nlink: new nlink (should be non-zero) 364 * 313 * 365 * This is a low-level filesystem helper to re 314 * This is a low-level filesystem helper to replace any 366 * direct filesystem manipulation of i_nlink. 315 * direct filesystem manipulation of i_nlink. 367 */ 316 */ 368 void set_nlink(struct inode *inode, unsigned i 317 void set_nlink(struct inode *inode, unsigned int nlink) 369 { 318 { 370 if (!nlink) { 319 if (!nlink) { 371 clear_nlink(inode); 320 clear_nlink(inode); 372 } else { 321 } else { 373 /* Yes, some filesystems do ch 322 /* Yes, some filesystems do change nlink from zero to one */ 374 if (inode->i_nlink == 0) 323 if (inode->i_nlink == 0) 375 atomic_long_dec(&inode 324 atomic_long_dec(&inode->i_sb->s_remove_count); 376 325 377 inode->__i_nlink = nlink; 326 inode->__i_nlink = nlink; 378 } 327 } 379 } 328 } 380 EXPORT_SYMBOL(set_nlink); 329 EXPORT_SYMBOL(set_nlink); 381 330 382 /** 331 /** 383 * inc_nlink - directly increment an inode's l 332 * inc_nlink - directly increment an inode's link count 384 * @inode: inode 333 * @inode: inode 385 * 334 * 386 * This is a low-level filesystem helper to re 335 * This is a low-level filesystem helper to replace any 387 * direct filesystem manipulation of i_nlink. 336 * direct filesystem manipulation of i_nlink. Currently, 388 * it is only here for parity with dec_nlink() 337 * it is only here for parity with dec_nlink(). 389 */ 338 */ 390 void inc_nlink(struct inode *inode) 339 void inc_nlink(struct inode *inode) 391 { 340 { 392 if (unlikely(inode->i_nlink == 0)) { 341 if (unlikely(inode->i_nlink == 0)) { 393 WARN_ON(!(inode->i_state & I_L 342 WARN_ON(!(inode->i_state & I_LINKABLE)); 394 atomic_long_dec(&inode->i_sb-> 343 atomic_long_dec(&inode->i_sb->s_remove_count); 395 } 344 } 396 345 397 inode->__i_nlink++; 346 inode->__i_nlink++; 398 } 347 } 399 EXPORT_SYMBOL(inc_nlink); 348 EXPORT_SYMBOL(inc_nlink); 400 349 401 static void __address_space_init_once(struct a 350 static void __address_space_init_once(struct address_space *mapping) 402 { 351 { 403 xa_init_flags(&mapping->i_pages, XA_FL !! 352 INIT_RADIX_TREE(&mapping->i_pages, GFP_ATOMIC | __GFP_ACCOUNT); 404 init_rwsem(&mapping->i_mmap_rwsem); 353 init_rwsem(&mapping->i_mmap_rwsem); 405 INIT_LIST_HEAD(&mapping->i_private_lis !! 354 INIT_LIST_HEAD(&mapping->private_list); 406 spin_lock_init(&mapping->i_private_loc !! 355 spin_lock_init(&mapping->private_lock); 407 mapping->i_mmap = RB_ROOT_CACHED; 356 mapping->i_mmap = RB_ROOT_CACHED; 408 } 357 } 409 358 410 void address_space_init_once(struct address_sp 359 void address_space_init_once(struct address_space *mapping) 411 { 360 { 412 memset(mapping, 0, sizeof(*mapping)); 361 memset(mapping, 0, sizeof(*mapping)); 413 __address_space_init_once(mapping); 362 __address_space_init_once(mapping); 414 } 363 } 415 EXPORT_SYMBOL(address_space_init_once); 364 EXPORT_SYMBOL(address_space_init_once); 416 365 417 /* 366 /* 418 * These are initializations that only need to 367 * These are initializations that only need to be done 419 * once, because the fields are idempotent acr 368 * once, because the fields are idempotent across use 420 * of the inode, so let the slab aware of that 369 * of the inode, so let the slab aware of that. 421 */ 370 */ 422 void inode_init_once(struct inode *inode) 371 void inode_init_once(struct inode *inode) 423 { 372 { 424 memset(inode, 0, sizeof(*inode)); 373 memset(inode, 0, sizeof(*inode)); 425 INIT_HLIST_NODE(&inode->i_hash); 374 INIT_HLIST_NODE(&inode->i_hash); 426 INIT_LIST_HEAD(&inode->i_devices); 375 INIT_LIST_HEAD(&inode->i_devices); 427 INIT_LIST_HEAD(&inode->i_io_list); 376 INIT_LIST_HEAD(&inode->i_io_list); 428 INIT_LIST_HEAD(&inode->i_wb_list); 377 INIT_LIST_HEAD(&inode->i_wb_list); 429 INIT_LIST_HEAD(&inode->i_lru); 378 INIT_LIST_HEAD(&inode->i_lru); 430 INIT_LIST_HEAD(&inode->i_sb_list); << 431 __address_space_init_once(&inode->i_da 379 __address_space_init_once(&inode->i_data); 432 i_size_ordered_init(inode); 380 i_size_ordered_init(inode); 433 } 381 } 434 EXPORT_SYMBOL(inode_init_once); 382 EXPORT_SYMBOL(inode_init_once); 435 383 436 static void init_once(void *foo) 384 static void init_once(void *foo) 437 { 385 { 438 struct inode *inode = (struct inode *) 386 struct inode *inode = (struct inode *) foo; 439 387 440 inode_init_once(inode); 388 inode_init_once(inode); 441 } 389 } 442 390 443 /* 391 /* >> 392 * inode->i_lock must be held >> 393 */ >> 394 void __iget(struct inode *inode) >> 395 { >> 396 atomic_inc(&inode->i_count); >> 397 } >> 398 >> 399 /* 444 * get additional reference to inode; caller m 400 * get additional reference to inode; caller must already hold one. 445 */ 401 */ 446 void ihold(struct inode *inode) 402 void ihold(struct inode *inode) 447 { 403 { 448 WARN_ON(atomic_inc_return(&inode->i_co 404 WARN_ON(atomic_inc_return(&inode->i_count) < 2); 449 } 405 } 450 EXPORT_SYMBOL(ihold); 406 EXPORT_SYMBOL(ihold); 451 407 452 static void __inode_add_lru(struct inode *inod !! 408 static void inode_lru_list_add(struct inode *inode) 453 { 409 { 454 if (inode->i_state & (I_DIRTY_ALL | I_ !! 410 if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru)) 455 return; << 456 if (atomic_read(&inode->i_count)) << 457 return; << 458 if (!(inode->i_sb->s_flags & SB_ACTIVE << 459 return; << 460 if (!mapping_shrinkable(&inode->i_data << 461 return; << 462 << 463 if (list_lru_add_obj(&inode->i_sb->s_i << 464 this_cpu_inc(nr_unused); 411 this_cpu_inc(nr_unused); 465 else if (rotate) !! 412 else 466 inode->i_state |= I_REFERENCED 413 inode->i_state |= I_REFERENCED; 467 } 414 } 468 415 469 struct wait_queue_head *inode_bit_waitqueue(st << 470 st << 471 { << 472 void *bit_address; << 473 << 474 bit_address = inode_state_wait_address << 475 init_wait_var_entry(wqe, bit_address, << 476 return __var_waitqueue(bit_address); << 477 } << 478 EXPORT_SYMBOL(inode_bit_waitqueue); << 479 << 480 /* 416 /* 481 * Add inode to LRU if needed (inode is unused 417 * Add inode to LRU if needed (inode is unused and clean). 482 * 418 * 483 * Needs inode->i_lock held. 419 * Needs inode->i_lock held. 484 */ 420 */ 485 void inode_add_lru(struct inode *inode) 421 void inode_add_lru(struct inode *inode) 486 { 422 { 487 __inode_add_lru(inode, false); !! 423 if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC | >> 424 I_FREEING | I_WILL_FREE)) && >> 425 !atomic_read(&inode->i_count) && inode->i_sb->s_flags & SB_ACTIVE) >> 426 inode_lru_list_add(inode); 488 } 427 } 489 428 490 static void inode_lru_list_del(struct inode *i << 491 { << 492 if (list_lru_del_obj(&inode->i_sb->s_i << 493 this_cpu_dec(nr_unused); << 494 } << 495 << 496 static void inode_pin_lru_isolating(struct ino << 497 { << 498 lockdep_assert_held(&inode->i_lock); << 499 WARN_ON(inode->i_state & (I_LRU_ISOLAT << 500 inode->i_state |= I_LRU_ISOLATING; << 501 } << 502 429 503 static void inode_unpin_lru_isolating(struct i !! 430 static void inode_lru_list_del(struct inode *inode) 504 { << 505 spin_lock(&inode->i_lock); << 506 WARN_ON(!(inode->i_state & I_LRU_ISOLA << 507 inode->i_state &= ~I_LRU_ISOLATING; << 508 /* Called with inode->i_lock which ens << 509 inode_wake_up_bit(inode, __I_LRU_ISOLA << 510 spin_unlock(&inode->i_lock); << 511 } << 512 << 513 static void inode_wait_for_lru_isolating(struc << 514 { 431 { 515 struct wait_bit_queue_entry wqe; << 516 struct wait_queue_head *wq_head; << 517 432 518 lockdep_assert_held(&inode->i_lock); !! 433 if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru)) 519 if (!(inode->i_state & I_LRU_ISOLATING !! 434 this_cpu_dec(nr_unused); 520 return; << 521 << 522 wq_head = inode_bit_waitqueue(&wqe, in << 523 for (;;) { << 524 prepare_to_wait_event(wq_head, << 525 /* << 526 * Checking I_LRU_ISOLATING wi << 527 * memory ordering. << 528 */ << 529 if (!(inode->i_state & I_LRU_I << 530 break; << 531 spin_unlock(&inode->i_lock); << 532 schedule(); << 533 spin_lock(&inode->i_lock); << 534 } << 535 finish_wait(wq_head, &wqe.wq_entry); << 536 WARN_ON(inode->i_state & I_LRU_ISOLATI << 537 } 435 } 538 436 539 /** 437 /** 540 * inode_sb_list_add - add inode to the superb 438 * inode_sb_list_add - add inode to the superblock list of inodes 541 * @inode: inode to add 439 * @inode: inode to add 542 */ 440 */ 543 void inode_sb_list_add(struct inode *inode) 441 void inode_sb_list_add(struct inode *inode) 544 { 442 { 545 spin_lock(&inode->i_sb->s_inode_list_l 443 spin_lock(&inode->i_sb->s_inode_list_lock); 546 list_add(&inode->i_sb_list, &inode->i_ 444 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes); 547 spin_unlock(&inode->i_sb->s_inode_list 445 spin_unlock(&inode->i_sb->s_inode_list_lock); 548 } 446 } 549 EXPORT_SYMBOL_GPL(inode_sb_list_add); 447 EXPORT_SYMBOL_GPL(inode_sb_list_add); 550 448 551 static inline void inode_sb_list_del(struct in 449 static inline void inode_sb_list_del(struct inode *inode) 552 { 450 { 553 if (!list_empty(&inode->i_sb_list)) { 451 if (!list_empty(&inode->i_sb_list)) { 554 spin_lock(&inode->i_sb->s_inod 452 spin_lock(&inode->i_sb->s_inode_list_lock); 555 list_del_init(&inode->i_sb_lis 453 list_del_init(&inode->i_sb_list); 556 spin_unlock(&inode->i_sb->s_in 454 spin_unlock(&inode->i_sb->s_inode_list_lock); 557 } 455 } 558 } 456 } 559 457 560 static unsigned long hash(struct super_block * 458 static unsigned long hash(struct super_block *sb, unsigned long hashval) 561 { 459 { 562 unsigned long tmp; 460 unsigned long tmp; 563 461 564 tmp = (hashval * (unsigned long)sb) ^ 462 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / 565 L1_CACHE_BYTES; 463 L1_CACHE_BYTES; 566 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME 464 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift); 567 return tmp & i_hash_mask; 465 return tmp & i_hash_mask; 568 } 466 } 569 467 570 /** 468 /** 571 * __insert_inode_hash - hash an inode 469 * __insert_inode_hash - hash an inode 572 * @inode: unhashed inode 470 * @inode: unhashed inode 573 * @hashval: unsigned long value used to 471 * @hashval: unsigned long value used to locate this object in the 574 * inode_hashtable. 472 * inode_hashtable. 575 * 473 * 576 * Add an inode to the inode hash for thi 474 * Add an inode to the inode hash for this superblock. 577 */ 475 */ 578 void __insert_inode_hash(struct inode *inode, 476 void __insert_inode_hash(struct inode *inode, unsigned long hashval) 579 { 477 { 580 struct hlist_head *b = inode_hashtable 478 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); 581 479 582 spin_lock(&inode_hash_lock); 480 spin_lock(&inode_hash_lock); 583 spin_lock(&inode->i_lock); 481 spin_lock(&inode->i_lock); 584 hlist_add_head_rcu(&inode->i_hash, b); !! 482 hlist_add_head(&inode->i_hash, b); 585 spin_unlock(&inode->i_lock); 483 spin_unlock(&inode->i_lock); 586 spin_unlock(&inode_hash_lock); 484 spin_unlock(&inode_hash_lock); 587 } 485 } 588 EXPORT_SYMBOL(__insert_inode_hash); 486 EXPORT_SYMBOL(__insert_inode_hash); 589 487 590 /** 488 /** 591 * __remove_inode_hash - remove an inode 489 * __remove_inode_hash - remove an inode from the hash 592 * @inode: inode to unhash 490 * @inode: inode to unhash 593 * 491 * 594 * Remove an inode from the superblock. 492 * Remove an inode from the superblock. 595 */ 493 */ 596 void __remove_inode_hash(struct inode *inode) 494 void __remove_inode_hash(struct inode *inode) 597 { 495 { 598 spin_lock(&inode_hash_lock); 496 spin_lock(&inode_hash_lock); 599 spin_lock(&inode->i_lock); 497 spin_lock(&inode->i_lock); 600 hlist_del_init_rcu(&inode->i_hash); !! 498 hlist_del_init(&inode->i_hash); 601 spin_unlock(&inode->i_lock); 499 spin_unlock(&inode->i_lock); 602 spin_unlock(&inode_hash_lock); 500 spin_unlock(&inode_hash_lock); 603 } 501 } 604 EXPORT_SYMBOL(__remove_inode_hash); 502 EXPORT_SYMBOL(__remove_inode_hash); 605 503 606 void dump_mapping(const struct address_space * << 607 { << 608 struct inode *host; << 609 const struct address_space_operations << 610 struct hlist_node *dentry_first; << 611 struct dentry *dentry_ptr; << 612 struct dentry dentry; << 613 char fname[64] = {}; << 614 unsigned long ino; << 615 << 616 /* << 617 * If mapping is an invalid pointer, w << 618 * accessing it, so probe everything d << 619 */ << 620 if (get_kernel_nofault(host, &mapping- << 621 get_kernel_nofault(a_ops, &mapping << 622 pr_warn("invalid mapping:%px\n << 623 return; << 624 } << 625 << 626 if (!host) { << 627 pr_warn("aops:%ps\n", a_ops); << 628 return; << 629 } << 630 << 631 if (get_kernel_nofault(dentry_first, & << 632 get_kernel_nofault(ino, &host->i_i << 633 pr_warn("aops:%ps invalid inod << 634 return; << 635 } << 636 << 637 if (!dentry_first) { << 638 pr_warn("aops:%ps ino:%lx\n", << 639 return; << 640 } << 641 << 642 dentry_ptr = container_of(dentry_first << 643 if (get_kernel_nofault(dentry, dentry_ << 644 !dentry.d_parent || !dentry.d_name << 645 pr_warn("aops:%ps ino:%lx inva << 646 a_ops, ino, de << 647 return; << 648 } << 649 << 650 if (strncpy_from_kernel_nofault(fname, << 651 strscpy(fname, "<invalid>"); << 652 /* << 653 * Even if strncpy_from_kernel_nofault << 654 * the fname could be unreliable << 655 */ << 656 pr_warn("aops:%ps ino:%lx dentry name( << 657 a_ops, ino, fname); << 658 } << 659 << 660 void clear_inode(struct inode *inode) 504 void clear_inode(struct inode *inode) 661 { 505 { 662 /* 506 /* 663 * We have to cycle the i_pages lock h 507 * We have to cycle the i_pages lock here because reclaim can be in the 664 * process of removing the last page ( !! 508 * process of removing the last page (in __delete_from_page_cache()) 665 * and we must not free the mapping un 509 * and we must not free the mapping under it. 666 */ 510 */ 667 xa_lock_irq(&inode->i_data.i_pages); 511 xa_lock_irq(&inode->i_data.i_pages); 668 BUG_ON(inode->i_data.nrpages); 512 BUG_ON(inode->i_data.nrpages); 669 /* !! 513 BUG_ON(inode->i_data.nrexceptional); 670 * Almost always, mapping_empty(&inode << 671 * two known and long-standing ways in << 672 * (when deep radix-tree node allocati << 673 * collapse_file() failed). Until thos << 674 * or a cleanup function is called her << 675 * nor even WARN_ON(!mapping_empty). << 676 */ << 677 xa_unlock_irq(&inode->i_data.i_pages); 514 xa_unlock_irq(&inode->i_data.i_pages); 678 BUG_ON(!list_empty(&inode->i_data.i_pr !! 515 BUG_ON(!list_empty(&inode->i_data.private_list)); 679 BUG_ON(!(inode->i_state & I_FREEING)); 516 BUG_ON(!(inode->i_state & I_FREEING)); 680 BUG_ON(inode->i_state & I_CLEAR); 517 BUG_ON(inode->i_state & I_CLEAR); 681 BUG_ON(!list_empty(&inode->i_wb_list)) 518 BUG_ON(!list_empty(&inode->i_wb_list)); 682 /* don't need i_lock here, no concurre 519 /* don't need i_lock here, no concurrent mods to i_state */ 683 inode->i_state = I_FREEING | I_CLEAR; 520 inode->i_state = I_FREEING | I_CLEAR; 684 } 521 } 685 EXPORT_SYMBOL(clear_inode); 522 EXPORT_SYMBOL(clear_inode); 686 523 687 /* 524 /* 688 * Free the inode passed in, removing it from 525 * Free the inode passed in, removing it from the lists it is still connected 689 * to. We remove any pages still attached to t 526 * to. We remove any pages still attached to the inode and wait for any IO that 690 * is still in progress before finally destroy 527 * is still in progress before finally destroying the inode. 691 * 528 * 692 * An inode must already be marked I_FREEING s 529 * An inode must already be marked I_FREEING so that we avoid the inode being 693 * moved back onto lists if we race with other 530 * moved back onto lists if we race with other code that manipulates the lists 694 * (e.g. writeback_single_inode). The caller i 531 * (e.g. writeback_single_inode). The caller is responsible for setting this. 695 * 532 * 696 * An inode must already be removed from the L 533 * An inode must already be removed from the LRU list before being evicted from 697 * the cache. This should occur atomically wit 534 * the cache. This should occur atomically with setting the I_FREEING state 698 * flag, so no inodes here should ever be on t 535 * flag, so no inodes here should ever be on the LRU when being evicted. 699 */ 536 */ 700 static void evict(struct inode *inode) 537 static void evict(struct inode *inode) 701 { 538 { 702 const struct super_operations *op = in 539 const struct super_operations *op = inode->i_sb->s_op; 703 540 704 BUG_ON(!(inode->i_state & I_FREEING)); 541 BUG_ON(!(inode->i_state & I_FREEING)); 705 BUG_ON(!list_empty(&inode->i_lru)); 542 BUG_ON(!list_empty(&inode->i_lru)); 706 543 707 if (!list_empty(&inode->i_io_list)) 544 if (!list_empty(&inode->i_io_list)) 708 inode_io_list_del(inode); 545 inode_io_list_del(inode); 709 546 710 inode_sb_list_del(inode); 547 inode_sb_list_del(inode); 711 548 712 spin_lock(&inode->i_lock); << 713 inode_wait_for_lru_isolating(inode); << 714 << 715 /* 549 /* 716 * Wait for flusher thread to be done 550 * Wait for flusher thread to be done with the inode so that filesystem 717 * does not start destroying it while 551 * does not start destroying it while writeback is still running. Since 718 * the inode has I_FREEING set, flushe 552 * the inode has I_FREEING set, flusher thread won't start new work on 719 * the inode. We just have to wait fo 553 * the inode. We just have to wait for running writeback to finish. 720 */ 554 */ 721 inode_wait_for_writeback(inode); 555 inode_wait_for_writeback(inode); 722 spin_unlock(&inode->i_lock); << 723 556 724 if (op->evict_inode) { 557 if (op->evict_inode) { 725 op->evict_inode(inode); 558 op->evict_inode(inode); 726 } else { 559 } else { 727 truncate_inode_pages_final(&in 560 truncate_inode_pages_final(&inode->i_data); 728 clear_inode(inode); 561 clear_inode(inode); 729 } 562 } >> 563 if (S_ISBLK(inode->i_mode) && inode->i_bdev) >> 564 bd_forget(inode); 730 if (S_ISCHR(inode->i_mode) && inode->i 565 if (S_ISCHR(inode->i_mode) && inode->i_cdev) 731 cd_forget(inode); 566 cd_forget(inode); 732 567 733 remove_inode_hash(inode); 568 remove_inode_hash(inode); 734 569 735 /* << 736 * Wake up waiters in __wait_on_freein << 737 * << 738 * Lockless hash lookup may end up fin << 739 * it above, but only lock it *after* << 740 * In this case the potential waiter c << 741 * << 742 * The inode being unhashed after the << 743 * used as an indicator whether blocki << 744 */ << 745 spin_lock(&inode->i_lock); 570 spin_lock(&inode->i_lock); 746 /* !! 571 wake_up_bit(&inode->i_state, __I_NEW); 747 * Pairs with the barrier in prepare_t << 748 * ___wait_var_event() either sees the << 749 * waitqueue_active() check in wake_up << 750 */ << 751 smp_mb(); << 752 inode_wake_up_bit(inode, __I_NEW); << 753 BUG_ON(inode->i_state != (I_FREEING | 572 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); 754 spin_unlock(&inode->i_lock); 573 spin_unlock(&inode->i_lock); 755 574 756 destroy_inode(inode); 575 destroy_inode(inode); 757 } 576 } 758 577 759 /* 578 /* 760 * dispose_list - dispose of the contents of a 579 * dispose_list - dispose of the contents of a local list 761 * @head: the head of the list to free 580 * @head: the head of the list to free 762 * 581 * 763 * Dispose-list gets a local list with local i 582 * Dispose-list gets a local list with local inodes in it, so it doesn't 764 * need to worry about list corruption and SMP 583 * need to worry about list corruption and SMP locks. 765 */ 584 */ 766 static void dispose_list(struct list_head *hea 585 static void dispose_list(struct list_head *head) 767 { 586 { 768 while (!list_empty(head)) { 587 while (!list_empty(head)) { 769 struct inode *inode; 588 struct inode *inode; 770 589 771 inode = list_first_entry(head, 590 inode = list_first_entry(head, struct inode, i_lru); 772 list_del_init(&inode->i_lru); 591 list_del_init(&inode->i_lru); 773 592 774 evict(inode); 593 evict(inode); 775 cond_resched(); 594 cond_resched(); 776 } 595 } 777 } 596 } 778 597 779 /** 598 /** 780 * evict_inodes - evict all evictable inodes f 599 * evict_inodes - evict all evictable inodes for a superblock 781 * @sb: superblock to operate on 600 * @sb: superblock to operate on 782 * 601 * 783 * Make sure that no inodes with zero refcount 602 * Make sure that no inodes with zero refcount are retained. This is 784 * called by superblock shutdown after having 603 * called by superblock shutdown after having SB_ACTIVE flag removed, 785 * so any inode reaching zero refcount during 604 * so any inode reaching zero refcount during or after that call will 786 * be immediately evicted. 605 * be immediately evicted. 787 */ 606 */ 788 void evict_inodes(struct super_block *sb) 607 void evict_inodes(struct super_block *sb) 789 { 608 { 790 struct inode *inode, *next; 609 struct inode *inode, *next; 791 LIST_HEAD(dispose); 610 LIST_HEAD(dispose); 792 611 793 again: 612 again: 794 spin_lock(&sb->s_inode_list_lock); 613 spin_lock(&sb->s_inode_list_lock); 795 list_for_each_entry_safe(inode, next, 614 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { 796 if (atomic_read(&inode->i_coun 615 if (atomic_read(&inode->i_count)) 797 continue; 616 continue; 798 617 799 spin_lock(&inode->i_lock); 618 spin_lock(&inode->i_lock); 800 if (atomic_read(&inode->i_coun << 801 spin_unlock(&inode->i_ << 802 continue; << 803 } << 804 if (inode->i_state & (I_NEW | 619 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 805 spin_unlock(&inode->i_ 620 spin_unlock(&inode->i_lock); 806 continue; 621 continue; 807 } 622 } 808 623 809 inode->i_state |= I_FREEING; 624 inode->i_state |= I_FREEING; 810 inode_lru_list_del(inode); 625 inode_lru_list_del(inode); 811 spin_unlock(&inode->i_lock); 626 spin_unlock(&inode->i_lock); 812 list_add(&inode->i_lru, &dispo 627 list_add(&inode->i_lru, &dispose); 813 628 814 /* 629 /* 815 * We can have a ton of inodes 630 * We can have a ton of inodes to evict at unmount time given 816 * enough memory, check to see 631 * enough memory, check to see if we need to go to sleep for a 817 * bit so we don't livelock. 632 * bit so we don't livelock. 818 */ 633 */ 819 if (need_resched()) { 634 if (need_resched()) { 820 spin_unlock(&sb->s_ino 635 spin_unlock(&sb->s_inode_list_lock); 821 cond_resched(); 636 cond_resched(); 822 dispose_list(&dispose) 637 dispose_list(&dispose); 823 goto again; 638 goto again; 824 } 639 } 825 } 640 } 826 spin_unlock(&sb->s_inode_list_lock); 641 spin_unlock(&sb->s_inode_list_lock); 827 642 828 dispose_list(&dispose); 643 dispose_list(&dispose); 829 } 644 } 830 EXPORT_SYMBOL_GPL(evict_inodes); 645 EXPORT_SYMBOL_GPL(evict_inodes); 831 646 832 /** 647 /** 833 * invalidate_inodes - attempt to free all 648 * invalidate_inodes - attempt to free all inodes on a superblock 834 * @sb: superblock to operate on 649 * @sb: superblock to operate on >> 650 * @kill_dirty: flag to guide handling of dirty inodes 835 * 651 * 836 * Attempts to free all inodes (including dirt !! 652 * Attempts to free all inodes for a given superblock. If there were any >> 653 * busy inodes return a non-zero value, else zero. >> 654 * If @kill_dirty is set, discard dirty inodes too, otherwise treat >> 655 * them as busy. 837 */ 656 */ 838 void invalidate_inodes(struct super_block *sb) !! 657 int invalidate_inodes(struct super_block *sb, bool kill_dirty) 839 { 658 { >> 659 int busy = 0; 840 struct inode *inode, *next; 660 struct inode *inode, *next; 841 LIST_HEAD(dispose); 661 LIST_HEAD(dispose); 842 662 843 again: << 844 spin_lock(&sb->s_inode_list_lock); 663 spin_lock(&sb->s_inode_list_lock); 845 list_for_each_entry_safe(inode, next, 664 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { 846 spin_lock(&inode->i_lock); 665 spin_lock(&inode->i_lock); 847 if (inode->i_state & (I_NEW | 666 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 848 spin_unlock(&inode->i_ 667 spin_unlock(&inode->i_lock); 849 continue; 668 continue; 850 } 669 } >> 670 if (inode->i_state & I_DIRTY_ALL && !kill_dirty) { >> 671 spin_unlock(&inode->i_lock); >> 672 busy = 1; >> 673 continue; >> 674 } 851 if (atomic_read(&inode->i_coun 675 if (atomic_read(&inode->i_count)) { 852 spin_unlock(&inode->i_ 676 spin_unlock(&inode->i_lock); >> 677 busy = 1; 853 continue; 678 continue; 854 } 679 } 855 680 856 inode->i_state |= I_FREEING; 681 inode->i_state |= I_FREEING; 857 inode_lru_list_del(inode); 682 inode_lru_list_del(inode); 858 spin_unlock(&inode->i_lock); 683 spin_unlock(&inode->i_lock); 859 list_add(&inode->i_lru, &dispo 684 list_add(&inode->i_lru, &dispose); 860 if (need_resched()) { << 861 spin_unlock(&sb->s_ino << 862 cond_resched(); << 863 dispose_list(&dispose) << 864 goto again; << 865 } << 866 } 685 } 867 spin_unlock(&sb->s_inode_list_lock); 686 spin_unlock(&sb->s_inode_list_lock); 868 687 869 dispose_list(&dispose); 688 dispose_list(&dispose); >> 689 >> 690 return busy; 870 } 691 } 871 692 872 /* 693 /* 873 * Isolate the inode from the LRU in preparati 694 * Isolate the inode from the LRU in preparation for freeing it. 874 * 695 * >> 696 * Any inodes which are pinned purely because of attached pagecache have their >> 697 * pagecache removed. If the inode has metadata buffers attached to >> 698 * mapping->private_list then try to remove them. >> 699 * 875 * If the inode has the I_REFERENCED flag set, 700 * If the inode has the I_REFERENCED flag set, then it means that it has been 876 * used recently - the flag is set in iput_fin 701 * used recently - the flag is set in iput_final(). When we encounter such an 877 * inode, clear the flag and move it to the ba 702 * inode, clear the flag and move it to the back of the LRU so it gets another 878 * pass through the LRU before it gets reclaim 703 * pass through the LRU before it gets reclaimed. This is necessary because of 879 * the fact we are doing lazy LRU updates to m 704 * the fact we are doing lazy LRU updates to minimise lock contention so the 880 * LRU does not have strict ordering. Hence we 705 * LRU does not have strict ordering. Hence we don't want to reclaim inodes 881 * with this flag set because they are the ino 706 * with this flag set because they are the inodes that are out of order. 882 */ 707 */ 883 static enum lru_status inode_lru_isolate(struc 708 static enum lru_status inode_lru_isolate(struct list_head *item, 884 struct list_lru_one *lru, spin 709 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg) 885 { 710 { 886 struct list_head *freeable = arg; 711 struct list_head *freeable = arg; 887 struct inode *inode = container_of( 712 struct inode *inode = container_of(item, struct inode, i_lru); 888 713 889 /* 714 /* 890 * We are inverting the lru lock/inode !! 715 * we are inverting the lru lock/inode->i_lock here, so use a trylock. 891 * trylock. If we fail to get the lock !! 716 * If we fail to get the lock, just skip it. 892 */ 717 */ 893 if (!spin_trylock(&inode->i_lock)) 718 if (!spin_trylock(&inode->i_lock)) 894 return LRU_SKIP; 719 return LRU_SKIP; 895 720 896 /* 721 /* 897 * Inodes can get referenced, redirtie !! 722 * Referenced or dirty inodes are still in use. Give them another pass 898 * they're already on the LRU, and thi !! 723 * through the LRU as we canot reclaim them now. 899 * unreclaimable for a while. Remove t << 900 * sync, or the last page cache deleti << 901 */ 724 */ 902 if (atomic_read(&inode->i_count) || 725 if (atomic_read(&inode->i_count) || 903 (inode->i_state & ~I_REFERENCED) | !! 726 (inode->i_state & ~I_REFERENCED)) { 904 !mapping_shrinkable(&inode->i_data << 905 list_lru_isolate(lru, &inode-> 727 list_lru_isolate(lru, &inode->i_lru); 906 spin_unlock(&inode->i_lock); 728 spin_unlock(&inode->i_lock); 907 this_cpu_dec(nr_unused); 729 this_cpu_dec(nr_unused); 908 return LRU_REMOVED; 730 return LRU_REMOVED; 909 } 731 } 910 732 911 /* Recently referenced inodes get one !! 733 /* recently referenced inodes get one more pass */ 912 if (inode->i_state & I_REFERENCED) { 734 if (inode->i_state & I_REFERENCED) { 913 inode->i_state &= ~I_REFERENCE 735 inode->i_state &= ~I_REFERENCED; 914 spin_unlock(&inode->i_lock); 736 spin_unlock(&inode->i_lock); 915 return LRU_ROTATE; 737 return LRU_ROTATE; 916 } 738 } 917 739 918 /* !! 740 if (inode_has_buffers(inode) || inode->i_data.nrpages) { 919 * On highmem systems, mapping_shrinka !! 741 __iget(inode); 920 * page cache in order to free up stru << 921 * be under pressure before the cache << 922 */ << 923 if (inode_has_buffers(inode) || !mappi << 924 inode_pin_lru_isolating(inode) << 925 spin_unlock(&inode->i_lock); 742 spin_unlock(&inode->i_lock); 926 spin_unlock(lru_lock); 743 spin_unlock(lru_lock); 927 if (remove_inode_buffers(inode 744 if (remove_inode_buffers(inode)) { 928 unsigned long reap; 745 unsigned long reap; 929 reap = invalidate_mapp 746 reap = invalidate_mapping_pages(&inode->i_data, 0, -1); 930 if (current_is_kswapd( 747 if (current_is_kswapd()) 931 __count_vm_eve 748 __count_vm_events(KSWAPD_INODESTEAL, reap); 932 else 749 else 933 __count_vm_eve 750 __count_vm_events(PGINODESTEAL, reap); 934 mm_account_reclaimed_p !! 751 if (current->reclaim_state) >> 752 current->reclaim_state->reclaimed_slab += reap; 935 } 753 } 936 inode_unpin_lru_isolating(inod !! 754 iput(inode); 937 spin_lock(lru_lock); 755 spin_lock(lru_lock); 938 return LRU_RETRY; 756 return LRU_RETRY; 939 } 757 } 940 758 941 WARN_ON(inode->i_state & I_NEW); 759 WARN_ON(inode->i_state & I_NEW); 942 inode->i_state |= I_FREEING; 760 inode->i_state |= I_FREEING; 943 list_lru_isolate_move(lru, &inode->i_l 761 list_lru_isolate_move(lru, &inode->i_lru, freeable); 944 spin_unlock(&inode->i_lock); 762 spin_unlock(&inode->i_lock); 945 763 946 this_cpu_dec(nr_unused); 764 this_cpu_dec(nr_unused); 947 return LRU_REMOVED; 765 return LRU_REMOVED; 948 } 766 } 949 767 950 /* 768 /* 951 * Walk the superblock inode LRU for freeable 769 * Walk the superblock inode LRU for freeable inodes and attempt to free them. 952 * This is called from the superblock shrinker 770 * This is called from the superblock shrinker function with a number of inodes 953 * to trim from the LRU. Inodes to be freed ar 771 * to trim from the LRU. Inodes to be freed are moved to a temporary list and 954 * then are freed outside inode_lock by dispos 772 * then are freed outside inode_lock by dispose_list(). 955 */ 773 */ 956 long prune_icache_sb(struct super_block *sb, s 774 long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) 957 { 775 { 958 LIST_HEAD(freeable); 776 LIST_HEAD(freeable); 959 long freed; 777 long freed; 960 778 961 freed = list_lru_shrink_walk(&sb->s_in 779 freed = list_lru_shrink_walk(&sb->s_inode_lru, sc, 962 inode_lru 780 inode_lru_isolate, &freeable); 963 dispose_list(&freeable); 781 dispose_list(&freeable); 964 return freed; 782 return freed; 965 } 783 } 966 784 967 static void __wait_on_freeing_inode(struct ino !! 785 static void __wait_on_freeing_inode(struct inode *inode); 968 /* 786 /* 969 * Called with the inode lock held. 787 * Called with the inode lock held. 970 */ 788 */ 971 static struct inode *find_inode(struct super_b 789 static struct inode *find_inode(struct super_block *sb, 972 struct hlist_h 790 struct hlist_head *head, 973 int (*test)(st 791 int (*test)(struct inode *, void *), 974 void *data, bo !! 792 void *data) 975 { 793 { 976 struct inode *inode = NULL; 794 struct inode *inode = NULL; 977 795 978 if (is_inode_hash_locked) << 979 lockdep_assert_held(&inode_has << 980 else << 981 lockdep_assert_not_held(&inode << 982 << 983 rcu_read_lock(); << 984 repeat: 796 repeat: 985 hlist_for_each_entry_rcu(inode, head, !! 797 hlist_for_each_entry(inode, head, i_hash) { 986 if (inode->i_sb != sb) 798 if (inode->i_sb != sb) 987 continue; 799 continue; 988 if (!test(inode, data)) 800 if (!test(inode, data)) 989 continue; 801 continue; 990 spin_lock(&inode->i_lock); 802 spin_lock(&inode->i_lock); 991 if (inode->i_state & (I_FREEIN 803 if (inode->i_state & (I_FREEING|I_WILL_FREE)) { 992 __wait_on_freeing_inod !! 804 __wait_on_freeing_inode(inode); 993 goto repeat; 805 goto repeat; 994 } 806 } 995 if (unlikely(inode->i_state & 807 if (unlikely(inode->i_state & I_CREATING)) { 996 spin_unlock(&inode->i_ 808 spin_unlock(&inode->i_lock); 997 rcu_read_unlock(); << 998 return ERR_PTR(-ESTALE 809 return ERR_PTR(-ESTALE); 999 } 810 } 1000 __iget(inode); 811 __iget(inode); 1001 spin_unlock(&inode->i_lock); 812 spin_unlock(&inode->i_lock); 1002 rcu_read_unlock(); << 1003 return inode; 813 return inode; 1004 } 814 } 1005 rcu_read_unlock(); << 1006 return NULL; 815 return NULL; 1007 } 816 } 1008 817 1009 /* 818 /* 1010 * find_inode_fast is the fast path version o 819 * find_inode_fast is the fast path version of find_inode, see the comment at 1011 * iget_locked for details. 820 * iget_locked for details. 1012 */ 821 */ 1013 static struct inode *find_inode_fast(struct s 822 static struct inode *find_inode_fast(struct super_block *sb, 1014 struct hlist_ !! 823 struct hlist_head *head, unsigned long ino) 1015 bool is_inode << 1016 { 824 { 1017 struct inode *inode = NULL; 825 struct inode *inode = NULL; 1018 826 1019 if (is_inode_hash_locked) << 1020 lockdep_assert_held(&inode_ha << 1021 else << 1022 lockdep_assert_not_held(&inod << 1023 << 1024 rcu_read_lock(); << 1025 repeat: 827 repeat: 1026 hlist_for_each_entry_rcu(inode, head, !! 828 hlist_for_each_entry(inode, head, i_hash) { 1027 if (inode->i_ino != ino) 829 if (inode->i_ino != ino) 1028 continue; 830 continue; 1029 if (inode->i_sb != sb) 831 if (inode->i_sb != sb) 1030 continue; 832 continue; 1031 spin_lock(&inode->i_lock); 833 spin_lock(&inode->i_lock); 1032 if (inode->i_state & (I_FREEI 834 if (inode->i_state & (I_FREEING|I_WILL_FREE)) { 1033 __wait_on_freeing_ino !! 835 __wait_on_freeing_inode(inode); 1034 goto repeat; 836 goto repeat; 1035 } 837 } 1036 if (unlikely(inode->i_state & 838 if (unlikely(inode->i_state & I_CREATING)) { 1037 spin_unlock(&inode->i 839 spin_unlock(&inode->i_lock); 1038 rcu_read_unlock(); << 1039 return ERR_PTR(-ESTAL 840 return ERR_PTR(-ESTALE); 1040 } 841 } 1041 __iget(inode); 842 __iget(inode); 1042 spin_unlock(&inode->i_lock); 843 spin_unlock(&inode->i_lock); 1043 rcu_read_unlock(); << 1044 return inode; 844 return inode; 1045 } 845 } 1046 rcu_read_unlock(); << 1047 return NULL; 846 return NULL; 1048 } 847 } 1049 848 1050 /* 849 /* 1051 * Each cpu owns a range of LAST_INO_BATCH nu 850 * Each cpu owns a range of LAST_INO_BATCH numbers. 1052 * 'shared_last_ino' is dirtied only once out 851 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations, 1053 * to renew the exhausted range. 852 * to renew the exhausted range. 1054 * 853 * 1055 * This does not significantly increase overf 854 * This does not significantly increase overflow rate because every CPU can 1056 * consume at most LAST_INO_BATCH-1 unused in 855 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is 1057 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 409 856 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the 1058 * 2^32 range, and is a worst-case. Even a 50 857 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase 1059 * overflow rate by 2x, which does not seem t 858 * overflow rate by 2x, which does not seem too significant. 1060 * 859 * 1061 * On a 32bit, non LFS stat() call, glibc wil 860 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW 1062 * error if st_ino won't fit in target struct 861 * error if st_ino won't fit in target struct field. Use 32bit counter 1063 * here to attempt to avoid that. 862 * here to attempt to avoid that. 1064 */ 863 */ 1065 #define LAST_INO_BATCH 1024 864 #define LAST_INO_BATCH 1024 1066 static DEFINE_PER_CPU(unsigned int, last_ino) 865 static DEFINE_PER_CPU(unsigned int, last_ino); 1067 866 1068 unsigned int get_next_ino(void) 867 unsigned int get_next_ino(void) 1069 { 868 { 1070 unsigned int *p = &get_cpu_var(last_i 869 unsigned int *p = &get_cpu_var(last_ino); 1071 unsigned int res = *p; 870 unsigned int res = *p; 1072 871 1073 #ifdef CONFIG_SMP 872 #ifdef CONFIG_SMP 1074 if (unlikely((res & (LAST_INO_BATCH-1 873 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) { 1075 static atomic_t shared_last_i 874 static atomic_t shared_last_ino; 1076 int next = atomic_add_return( 875 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino); 1077 876 1078 res = next - LAST_INO_BATCH; 877 res = next - LAST_INO_BATCH; 1079 } 878 } 1080 #endif 879 #endif 1081 880 1082 res++; 881 res++; 1083 /* get_next_ino should not provide a 882 /* get_next_ino should not provide a 0 inode number */ 1084 if (unlikely(!res)) 883 if (unlikely(!res)) 1085 res++; 884 res++; 1086 *p = res; 885 *p = res; 1087 put_cpu_var(last_ino); 886 put_cpu_var(last_ino); 1088 return res; 887 return res; 1089 } 888 } 1090 EXPORT_SYMBOL(get_next_ino); 889 EXPORT_SYMBOL(get_next_ino); 1091 890 1092 /** 891 /** 1093 * new_inode_pseudo - obtain an i 892 * new_inode_pseudo - obtain an inode 1094 * @sb: superblock 893 * @sb: superblock 1095 * 894 * 1096 * Allocates a new inode for given super 895 * Allocates a new inode for given superblock. 1097 * Inode wont be chained in superblock s 896 * Inode wont be chained in superblock s_inodes list 1098 * This means : 897 * This means : 1099 * - fs can't be unmount 898 * - fs can't be unmount 1100 * - quotas, fsnotify, writeback can't w 899 * - quotas, fsnotify, writeback can't work 1101 */ 900 */ 1102 struct inode *new_inode_pseudo(struct super_b 901 struct inode *new_inode_pseudo(struct super_block *sb) 1103 { 902 { 1104 return alloc_inode(sb); !! 903 struct inode *inode = alloc_inode(sb); >> 904 >> 905 if (inode) { >> 906 spin_lock(&inode->i_lock); >> 907 inode->i_state = 0; >> 908 spin_unlock(&inode->i_lock); >> 909 INIT_LIST_HEAD(&inode->i_sb_list); >> 910 } >> 911 return inode; 1105 } 912 } 1106 913 1107 /** 914 /** 1108 * new_inode - obtain an inode 915 * new_inode - obtain an inode 1109 * @sb: superblock 916 * @sb: superblock 1110 * 917 * 1111 * Allocates a new inode for given super 918 * Allocates a new inode for given superblock. The default gfp_mask 1112 * for allocations related to inode->i_m 919 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE. 1113 * If HIGHMEM pages are unsuitable or it 920 * If HIGHMEM pages are unsuitable or it is known that pages allocated 1114 * for the page cache are not reclaimabl 921 * for the page cache are not reclaimable or migratable, 1115 * mapping_set_gfp_mask() must be called 922 * mapping_set_gfp_mask() must be called with suitable flags on the 1116 * newly created inode's mapping 923 * newly created inode's mapping 1117 * 924 * 1118 */ 925 */ 1119 struct inode *new_inode(struct super_block *s 926 struct inode *new_inode(struct super_block *sb) 1120 { 927 { 1121 struct inode *inode; 928 struct inode *inode; 1122 929 >> 930 spin_lock_prefetch(&sb->s_inode_list_lock); >> 931 1123 inode = new_inode_pseudo(sb); 932 inode = new_inode_pseudo(sb); 1124 if (inode) 933 if (inode) 1125 inode_sb_list_add(inode); 934 inode_sb_list_add(inode); 1126 return inode; 935 return inode; 1127 } 936 } 1128 EXPORT_SYMBOL(new_inode); 937 EXPORT_SYMBOL(new_inode); 1129 938 1130 #ifdef CONFIG_DEBUG_LOCK_ALLOC 939 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1131 void lockdep_annotate_inode_mutex_key(struct 940 void lockdep_annotate_inode_mutex_key(struct inode *inode) 1132 { 941 { 1133 if (S_ISDIR(inode->i_mode)) { 942 if (S_ISDIR(inode->i_mode)) { 1134 struct file_system_type *type 943 struct file_system_type *type = inode->i_sb->s_type; 1135 944 1136 /* Set new key only if filesy 945 /* Set new key only if filesystem hasn't already changed it */ 1137 if (lockdep_match_class(&inod 946 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) { 1138 /* 947 /* 1139 * ensure nobody is a 948 * ensure nobody is actually holding i_mutex 1140 */ 949 */ 1141 // mutex_destroy(&ino 950 // mutex_destroy(&inode->i_mutex); 1142 init_rwsem(&inode->i_ 951 init_rwsem(&inode->i_rwsem); 1143 lockdep_set_class(&in 952 lockdep_set_class(&inode->i_rwsem, 1144 &ty 953 &type->i_mutex_dir_key); 1145 } 954 } 1146 } 955 } 1147 } 956 } 1148 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_ke 957 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key); 1149 #endif 958 #endif 1150 959 1151 /** 960 /** 1152 * unlock_new_inode - clear the I_NEW state a 961 * unlock_new_inode - clear the I_NEW state and wake up any waiters 1153 * @inode: new inode to unlock 962 * @inode: new inode to unlock 1154 * 963 * 1155 * Called when the inode is fully initialised 964 * Called when the inode is fully initialised to clear the new state of the 1156 * inode and wake up anyone waiting for the i 965 * inode and wake up anyone waiting for the inode to finish initialisation. 1157 */ 966 */ 1158 void unlock_new_inode(struct inode *inode) 967 void unlock_new_inode(struct inode *inode) 1159 { 968 { 1160 lockdep_annotate_inode_mutex_key(inod 969 lockdep_annotate_inode_mutex_key(inode); 1161 spin_lock(&inode->i_lock); 970 spin_lock(&inode->i_lock); 1162 WARN_ON(!(inode->i_state & I_NEW)); 971 WARN_ON(!(inode->i_state & I_NEW)); 1163 inode->i_state &= ~I_NEW & ~I_CREATIN 972 inode->i_state &= ~I_NEW & ~I_CREATING; 1164 /* << 1165 * Pairs with the barrier in prepare_ << 1166 * ___wait_var_event() either sees th << 1167 * waitqueue_active() check in wake_u << 1168 */ << 1169 smp_mb(); 973 smp_mb(); 1170 inode_wake_up_bit(inode, __I_NEW); !! 974 wake_up_bit(&inode->i_state, __I_NEW); 1171 spin_unlock(&inode->i_lock); 975 spin_unlock(&inode->i_lock); 1172 } 976 } 1173 EXPORT_SYMBOL(unlock_new_inode); 977 EXPORT_SYMBOL(unlock_new_inode); 1174 978 1175 void discard_new_inode(struct inode *inode) 979 void discard_new_inode(struct inode *inode) 1176 { 980 { 1177 lockdep_annotate_inode_mutex_key(inod 981 lockdep_annotate_inode_mutex_key(inode); 1178 spin_lock(&inode->i_lock); 982 spin_lock(&inode->i_lock); 1179 WARN_ON(!(inode->i_state & I_NEW)); 983 WARN_ON(!(inode->i_state & I_NEW)); 1180 inode->i_state &= ~I_NEW; 984 inode->i_state &= ~I_NEW; 1181 /* << 1182 * Pairs with the barrier in prepare_ << 1183 * ___wait_var_event() either sees th << 1184 * waitqueue_active() check in wake_u << 1185 */ << 1186 smp_mb(); 985 smp_mb(); 1187 inode_wake_up_bit(inode, __I_NEW); !! 986 wake_up_bit(&inode->i_state, __I_NEW); 1188 spin_unlock(&inode->i_lock); 987 spin_unlock(&inode->i_lock); 1189 iput(inode); 988 iput(inode); 1190 } 989 } 1191 EXPORT_SYMBOL(discard_new_inode); 990 EXPORT_SYMBOL(discard_new_inode); 1192 991 1193 /** 992 /** 1194 * lock_two_nondirectories - take two i_mutex 993 * lock_two_nondirectories - take two i_mutexes on non-directory objects 1195 * 994 * 1196 * Lock any non-NULL argument. Passed objects !! 995 * Lock any non-NULL argument that is not a directory. 1197 * Zero, one or two objects may be locked by 996 * Zero, one or two objects may be locked by this function. 1198 * 997 * 1199 * @inode1: first inode to lock 998 * @inode1: first inode to lock 1200 * @inode2: second inode to lock 999 * @inode2: second inode to lock 1201 */ 1000 */ 1202 void lock_two_nondirectories(struct inode *in 1001 void lock_two_nondirectories(struct inode *inode1, struct inode *inode2) 1203 { 1002 { 1204 if (inode1) << 1205 WARN_ON_ONCE(S_ISDIR(inode1-> << 1206 if (inode2) << 1207 WARN_ON_ONCE(S_ISDIR(inode2-> << 1208 if (inode1 > inode2) 1003 if (inode1 > inode2) 1209 swap(inode1, inode2); 1004 swap(inode1, inode2); 1210 if (inode1) !! 1005 >> 1006 if (inode1 && !S_ISDIR(inode1->i_mode)) 1211 inode_lock(inode1); 1007 inode_lock(inode1); 1212 if (inode2 && inode2 != inode1) !! 1008 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1) 1213 inode_lock_nested(inode2, I_M 1009 inode_lock_nested(inode2, I_MUTEX_NONDIR2); 1214 } 1010 } 1215 EXPORT_SYMBOL(lock_two_nondirectories); 1011 EXPORT_SYMBOL(lock_two_nondirectories); 1216 1012 1217 /** 1013 /** 1218 * unlock_two_nondirectories - release locks 1014 * unlock_two_nondirectories - release locks from lock_two_nondirectories() 1219 * @inode1: first inode to unlock 1015 * @inode1: first inode to unlock 1220 * @inode2: second inode to unlock 1016 * @inode2: second inode to unlock 1221 */ 1017 */ 1222 void unlock_two_nondirectories(struct inode * 1018 void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2) 1223 { 1019 { 1224 if (inode1) { !! 1020 if (inode1 && !S_ISDIR(inode1->i_mode)) 1225 WARN_ON_ONCE(S_ISDIR(inode1-> << 1226 inode_unlock(inode1); 1021 inode_unlock(inode1); 1227 } !! 1022 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1) 1228 if (inode2 && inode2 != inode1) { << 1229 WARN_ON_ONCE(S_ISDIR(inode2-> << 1230 inode_unlock(inode2); 1023 inode_unlock(inode2); 1231 } << 1232 } 1024 } 1233 EXPORT_SYMBOL(unlock_two_nondirectories); 1025 EXPORT_SYMBOL(unlock_two_nondirectories); 1234 1026 1235 /** 1027 /** 1236 * inode_insert5 - obtain an inode from a mou 1028 * inode_insert5 - obtain an inode from a mounted file system 1237 * @inode: pre-allocated inode to use fo 1029 * @inode: pre-allocated inode to use for insert to cache 1238 * @hashval: hash value (usually inode num 1030 * @hashval: hash value (usually inode number) to get 1239 * @test: callback used for comparisons 1031 * @test: callback used for comparisons between inodes 1240 * @set: callback used to initialize a 1032 * @set: callback used to initialize a new struct inode 1241 * @data: opaque data pointer to pass t 1033 * @data: opaque data pointer to pass to @test and @set 1242 * 1034 * 1243 * Search for the inode specified by @hashval 1035 * Search for the inode specified by @hashval and @data in the inode cache, 1244 * and if present it is return it with an inc 1036 * and if present it is return it with an increased reference count. This is 1245 * a variant of iget5_locked() for callers th 1037 * a variant of iget5_locked() for callers that don't want to fail on memory 1246 * allocation of inode. 1038 * allocation of inode. 1247 * 1039 * 1248 * If the inode is not in cache, insert the p 1040 * If the inode is not in cache, insert the pre-allocated inode to cache and 1249 * return it locked, hashed, and with the I_N 1041 * return it locked, hashed, and with the I_NEW flag set. The file system gets 1250 * to fill it in before unlocking it via unlo 1042 * to fill it in before unlocking it via unlock_new_inode(). 1251 * 1043 * 1252 * Note both @test and @set are called with t 1044 * Note both @test and @set are called with the inode_hash_lock held, so can't 1253 * sleep. 1045 * sleep. 1254 */ 1046 */ 1255 struct inode *inode_insert5(struct inode *ino 1047 struct inode *inode_insert5(struct inode *inode, unsigned long hashval, 1256 int (*test)(struc 1048 int (*test)(struct inode *, void *), 1257 int (*set)(struct 1049 int (*set)(struct inode *, void *), void *data) 1258 { 1050 { 1259 struct hlist_head *head = inode_hasht 1051 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); 1260 struct inode *old; 1052 struct inode *old; >> 1053 bool creating = inode->i_state & I_CREATING; 1261 1054 1262 again: 1055 again: 1263 spin_lock(&inode_hash_lock); 1056 spin_lock(&inode_hash_lock); 1264 old = find_inode(inode->i_sb, head, t !! 1057 old = find_inode(inode->i_sb, head, test, data); 1265 if (unlikely(old)) { 1058 if (unlikely(old)) { 1266 /* 1059 /* 1267 * Uhhuh, somebody else creat 1060 * Uhhuh, somebody else created the same inode under us. 1268 * Use the old inode instead 1061 * Use the old inode instead of the preallocated one. 1269 */ 1062 */ 1270 spin_unlock(&inode_hash_lock) 1063 spin_unlock(&inode_hash_lock); 1271 if (IS_ERR(old)) 1064 if (IS_ERR(old)) 1272 return NULL; 1065 return NULL; 1273 wait_on_inode(old); 1066 wait_on_inode(old); 1274 if (unlikely(inode_unhashed(o 1067 if (unlikely(inode_unhashed(old))) { 1275 iput(old); 1068 iput(old); 1276 goto again; 1069 goto again; 1277 } 1070 } 1278 return old; 1071 return old; 1279 } 1072 } 1280 1073 1281 if (set && unlikely(set(inode, data)) 1074 if (set && unlikely(set(inode, data))) { 1282 inode = NULL; 1075 inode = NULL; 1283 goto unlock; 1076 goto unlock; 1284 } 1077 } 1285 1078 1286 /* 1079 /* 1287 * Return the locked inode with I_NEW 1080 * Return the locked inode with I_NEW set, the 1288 * caller is responsible for filling 1081 * caller is responsible for filling in the contents 1289 */ 1082 */ 1290 spin_lock(&inode->i_lock); 1083 spin_lock(&inode->i_lock); 1291 inode->i_state |= I_NEW; 1084 inode->i_state |= I_NEW; 1292 hlist_add_head_rcu(&inode->i_hash, he !! 1085 hlist_add_head(&inode->i_hash, head); 1293 spin_unlock(&inode->i_lock); 1086 spin_unlock(&inode->i_lock); 1294 !! 1087 if (!creating) 1295 /* << 1296 * Add inode to the sb list if it's n << 1297 * point, so it should be safe to tes << 1298 */ << 1299 if (list_empty(&inode->i_sb_list)) << 1300 inode_sb_list_add(inode); 1088 inode_sb_list_add(inode); 1301 unlock: 1089 unlock: 1302 spin_unlock(&inode_hash_lock); 1090 spin_unlock(&inode_hash_lock); 1303 1091 1304 return inode; 1092 return inode; 1305 } 1093 } 1306 EXPORT_SYMBOL(inode_insert5); 1094 EXPORT_SYMBOL(inode_insert5); 1307 1095 1308 /** 1096 /** 1309 * iget5_locked - obtain an inode from a moun 1097 * iget5_locked - obtain an inode from a mounted file system 1310 * @sb: super block of file system 1098 * @sb: super block of file system 1311 * @hashval: hash value (usually inode num 1099 * @hashval: hash value (usually inode number) to get 1312 * @test: callback used for comparisons 1100 * @test: callback used for comparisons between inodes 1313 * @set: callback used to initialize a 1101 * @set: callback used to initialize a new struct inode 1314 * @data: opaque data pointer to pass t 1102 * @data: opaque data pointer to pass to @test and @set 1315 * 1103 * 1316 * Search for the inode specified by @hashval 1104 * Search for the inode specified by @hashval and @data in the inode cache, 1317 * and if present it is return it with an inc 1105 * and if present it is return it with an increased reference count. This is 1318 * a generalized version of iget_locked() for 1106 * a generalized version of iget_locked() for file systems where the inode 1319 * number is not sufficient for unique identi 1107 * number is not sufficient for unique identification of an inode. 1320 * 1108 * 1321 * If the inode is not in cache, allocate a n 1109 * If the inode is not in cache, allocate a new inode and return it locked, 1322 * hashed, and with the I_NEW flag set. The f 1110 * hashed, and with the I_NEW flag set. The file system gets to fill it in 1323 * before unlocking it via unlock_new_inode() 1111 * before unlocking it via unlock_new_inode(). 1324 * 1112 * 1325 * Note both @test and @set are called with t 1113 * Note both @test and @set are called with the inode_hash_lock held, so can't 1326 * sleep. 1114 * sleep. 1327 */ 1115 */ 1328 struct inode *iget5_locked(struct super_block 1116 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, 1329 int (*test)(struct inode *, v 1117 int (*test)(struct inode *, void *), 1330 int (*set)(struct inode *, vo 1118 int (*set)(struct inode *, void *), void *data) 1331 { 1119 { 1332 struct inode *inode = ilookup5(sb, ha 1120 struct inode *inode = ilookup5(sb, hashval, test, data); 1333 1121 1334 if (!inode) { 1122 if (!inode) { 1335 struct inode *new = alloc_ino 1123 struct inode *new = alloc_inode(sb); 1336 1124 1337 if (new) { 1125 if (new) { >> 1126 new->i_state = 0; 1338 inode = inode_insert5 1127 inode = inode_insert5(new, hashval, test, set, data); 1339 if (unlikely(inode != 1128 if (unlikely(inode != new)) 1340 destroy_inode 1129 destroy_inode(new); 1341 } 1130 } 1342 } 1131 } 1343 return inode; 1132 return inode; 1344 } 1133 } 1345 EXPORT_SYMBOL(iget5_locked); 1134 EXPORT_SYMBOL(iget5_locked); 1346 1135 1347 /** 1136 /** 1348 * iget5_locked_rcu - obtain an inode from a << 1349 * @sb: super block of file system << 1350 * @hashval: hash value (usually inode num << 1351 * @test: callback used for comparisons << 1352 * @set: callback used to initialize a << 1353 * @data: opaque data pointer to pass t << 1354 * << 1355 * This is equivalent to iget5_locked, except << 1356 * tolerate the inode not being stable, inclu << 1357 */ << 1358 struct inode *iget5_locked_rcu(struct super_b << 1359 int (*test)(struct inode *, v << 1360 int (*set)(struct inode *, vo << 1361 { << 1362 struct hlist_head *head = inode_hasht << 1363 struct inode *inode, *new; << 1364 << 1365 again: << 1366 inode = find_inode(sb, head, test, da << 1367 if (inode) { << 1368 if (IS_ERR(inode)) << 1369 return NULL; << 1370 wait_on_inode(inode); << 1371 if (unlikely(inode_unhashed(i << 1372 iput(inode); << 1373 goto again; << 1374 } << 1375 return inode; << 1376 } << 1377 << 1378 new = alloc_inode(sb); << 1379 if (new) { << 1380 inode = inode_insert5(new, ha << 1381 if (unlikely(inode != new)) << 1382 destroy_inode(new); << 1383 } << 1384 return inode; << 1385 } << 1386 EXPORT_SYMBOL_GPL(iget5_locked_rcu); << 1387 << 1388 /** << 1389 * iget_locked - obtain an inode from a mount 1137 * iget_locked - obtain an inode from a mounted file system 1390 * @sb: super block of file system 1138 * @sb: super block of file system 1391 * @ino: inode number to get 1139 * @ino: inode number to get 1392 * 1140 * 1393 * Search for the inode specified by @ino in 1141 * Search for the inode specified by @ino in the inode cache and if present 1394 * return it with an increased reference coun 1142 * return it with an increased reference count. This is for file systems 1395 * where the inode number is sufficient for u 1143 * where the inode number is sufficient for unique identification of an inode. 1396 * 1144 * 1397 * If the inode is not in cache, allocate a n 1145 * If the inode is not in cache, allocate a new inode and return it locked, 1398 * hashed, and with the I_NEW flag set. The 1146 * hashed, and with the I_NEW flag set. The file system gets to fill it in 1399 * before unlocking it via unlock_new_inode() 1147 * before unlocking it via unlock_new_inode(). 1400 */ 1148 */ 1401 struct inode *iget_locked(struct super_block 1149 struct inode *iget_locked(struct super_block *sb, unsigned long ino) 1402 { 1150 { 1403 struct hlist_head *head = inode_hasht 1151 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1404 struct inode *inode; 1152 struct inode *inode; 1405 again: 1153 again: 1406 inode = find_inode_fast(sb, head, ino !! 1154 spin_lock(&inode_hash_lock); >> 1155 inode = find_inode_fast(sb, head, ino); >> 1156 spin_unlock(&inode_hash_lock); 1407 if (inode) { 1157 if (inode) { 1408 if (IS_ERR(inode)) 1158 if (IS_ERR(inode)) 1409 return NULL; 1159 return NULL; 1410 wait_on_inode(inode); 1160 wait_on_inode(inode); 1411 if (unlikely(inode_unhashed(i 1161 if (unlikely(inode_unhashed(inode))) { 1412 iput(inode); 1162 iput(inode); 1413 goto again; 1163 goto again; 1414 } 1164 } 1415 return inode; 1165 return inode; 1416 } 1166 } 1417 1167 1418 inode = alloc_inode(sb); 1168 inode = alloc_inode(sb); 1419 if (inode) { 1169 if (inode) { 1420 struct inode *old; 1170 struct inode *old; 1421 1171 1422 spin_lock(&inode_hash_lock); 1172 spin_lock(&inode_hash_lock); 1423 /* We released the lock, so.. 1173 /* We released the lock, so.. */ 1424 old = find_inode_fast(sb, hea !! 1174 old = find_inode_fast(sb, head, ino); 1425 if (!old) { 1175 if (!old) { 1426 inode->i_ino = ino; 1176 inode->i_ino = ino; 1427 spin_lock(&inode->i_l 1177 spin_lock(&inode->i_lock); 1428 inode->i_state = I_NE 1178 inode->i_state = I_NEW; 1429 hlist_add_head_rcu(&i !! 1179 hlist_add_head(&inode->i_hash, head); 1430 spin_unlock(&inode->i 1180 spin_unlock(&inode->i_lock); 1431 inode_sb_list_add(ino 1181 inode_sb_list_add(inode); 1432 spin_unlock(&inode_ha 1182 spin_unlock(&inode_hash_lock); 1433 1183 1434 /* Return the locked 1184 /* Return the locked inode with I_NEW set, the 1435 * caller is responsi 1185 * caller is responsible for filling in the contents 1436 */ 1186 */ 1437 return inode; 1187 return inode; 1438 } 1188 } 1439 1189 1440 /* 1190 /* 1441 * Uhhuh, somebody else creat 1191 * Uhhuh, somebody else created the same inode under 1442 * us. Use the old inode inst 1192 * us. Use the old inode instead of the one we just 1443 * allocated. 1193 * allocated. 1444 */ 1194 */ 1445 spin_unlock(&inode_hash_lock) 1195 spin_unlock(&inode_hash_lock); 1446 destroy_inode(inode); 1196 destroy_inode(inode); 1447 if (IS_ERR(old)) 1197 if (IS_ERR(old)) 1448 return NULL; 1198 return NULL; 1449 inode = old; 1199 inode = old; 1450 wait_on_inode(inode); 1200 wait_on_inode(inode); 1451 if (unlikely(inode_unhashed(i 1201 if (unlikely(inode_unhashed(inode))) { 1452 iput(inode); 1202 iput(inode); 1453 goto again; 1203 goto again; 1454 } 1204 } 1455 } 1205 } 1456 return inode; 1206 return inode; 1457 } 1207 } 1458 EXPORT_SYMBOL(iget_locked); 1208 EXPORT_SYMBOL(iget_locked); 1459 1209 1460 /* 1210 /* 1461 * search the inode cache for a matching inod 1211 * search the inode cache for a matching inode number. 1462 * If we find one, then the inode number we a 1212 * If we find one, then the inode number we are trying to 1463 * allocate is not unique and so we should no 1213 * allocate is not unique and so we should not use it. 1464 * 1214 * 1465 * Returns 1 if the inode number is unique, 0 1215 * Returns 1 if the inode number is unique, 0 if it is not. 1466 */ 1216 */ 1467 static int test_inode_iunique(struct super_bl 1217 static int test_inode_iunique(struct super_block *sb, unsigned long ino) 1468 { 1218 { 1469 struct hlist_head *b = inode_hashtabl 1219 struct hlist_head *b = inode_hashtable + hash(sb, ino); 1470 struct inode *inode; 1220 struct inode *inode; 1471 1221 1472 hlist_for_each_entry_rcu(inode, b, i_ !! 1222 spin_lock(&inode_hash_lock); 1473 if (inode->i_ino == ino && in !! 1223 hlist_for_each_entry(inode, b, i_hash) { >> 1224 if (inode->i_ino == ino && inode->i_sb == sb) { >> 1225 spin_unlock(&inode_hash_lock); 1474 return 0; 1226 return 0; >> 1227 } 1475 } 1228 } >> 1229 spin_unlock(&inode_hash_lock); >> 1230 1476 return 1; 1231 return 1; 1477 } 1232 } 1478 1233 1479 /** 1234 /** 1480 * iunique - get a unique inode number 1235 * iunique - get a unique inode number 1481 * @sb: superblock 1236 * @sb: superblock 1482 * @max_reserved: highest reserved inode 1237 * @max_reserved: highest reserved inode number 1483 * 1238 * 1484 * Obtain an inode number that is unique 1239 * Obtain an inode number that is unique on the system for a given 1485 * superblock. This is used by file syst 1240 * superblock. This is used by file systems that have no natural 1486 * permanent inode numbering system. An 1241 * permanent inode numbering system. An inode number is returned that 1487 * is higher than the reserved limit but 1242 * is higher than the reserved limit but unique. 1488 * 1243 * 1489 * BUGS: 1244 * BUGS: 1490 * With a large number of inodes live on 1245 * With a large number of inodes live on the file system this function 1491 * currently becomes quite slow. 1246 * currently becomes quite slow. 1492 */ 1247 */ 1493 ino_t iunique(struct super_block *sb, ino_t m 1248 ino_t iunique(struct super_block *sb, ino_t max_reserved) 1494 { 1249 { 1495 /* 1250 /* 1496 * On a 32bit, non LFS stat() call, g 1251 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW 1497 * error if st_ino won't fit in targe 1252 * error if st_ino won't fit in target struct field. Use 32bit counter 1498 * here to attempt to avoid that. 1253 * here to attempt to avoid that. 1499 */ 1254 */ 1500 static DEFINE_SPINLOCK(iunique_lock); 1255 static DEFINE_SPINLOCK(iunique_lock); 1501 static unsigned int counter; 1256 static unsigned int counter; 1502 ino_t res; 1257 ino_t res; 1503 1258 1504 rcu_read_lock(); << 1505 spin_lock(&iunique_lock); 1259 spin_lock(&iunique_lock); 1506 do { 1260 do { 1507 if (counter <= max_reserved) 1261 if (counter <= max_reserved) 1508 counter = max_reserve 1262 counter = max_reserved + 1; 1509 res = counter++; 1263 res = counter++; 1510 } while (!test_inode_iunique(sb, res) 1264 } while (!test_inode_iunique(sb, res)); 1511 spin_unlock(&iunique_lock); 1265 spin_unlock(&iunique_lock); 1512 rcu_read_unlock(); << 1513 1266 1514 return res; 1267 return res; 1515 } 1268 } 1516 EXPORT_SYMBOL(iunique); 1269 EXPORT_SYMBOL(iunique); 1517 1270 1518 struct inode *igrab(struct inode *inode) 1271 struct inode *igrab(struct inode *inode) 1519 { 1272 { 1520 spin_lock(&inode->i_lock); 1273 spin_lock(&inode->i_lock); 1521 if (!(inode->i_state & (I_FREEING|I_W 1274 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) { 1522 __iget(inode); 1275 __iget(inode); 1523 spin_unlock(&inode->i_lock); 1276 spin_unlock(&inode->i_lock); 1524 } else { 1277 } else { 1525 spin_unlock(&inode->i_lock); 1278 spin_unlock(&inode->i_lock); 1526 /* 1279 /* 1527 * Handle the case where s_op 1280 * Handle the case where s_op->clear_inode is not been 1528 * called yet, and somebody i 1281 * called yet, and somebody is calling igrab 1529 * while the inode is getting 1282 * while the inode is getting freed. 1530 */ 1283 */ 1531 inode = NULL; 1284 inode = NULL; 1532 } 1285 } 1533 return inode; 1286 return inode; 1534 } 1287 } 1535 EXPORT_SYMBOL(igrab); 1288 EXPORT_SYMBOL(igrab); 1536 1289 1537 /** 1290 /** 1538 * ilookup5_nowait - search for an inode in t 1291 * ilookup5_nowait - search for an inode in the inode cache 1539 * @sb: super block of file system to 1292 * @sb: super block of file system to search 1540 * @hashval: hash value (usually inode num 1293 * @hashval: hash value (usually inode number) to search for 1541 * @test: callback used for comparisons 1294 * @test: callback used for comparisons between inodes 1542 * @data: opaque data pointer to pass t 1295 * @data: opaque data pointer to pass to @test 1543 * 1296 * 1544 * Search for the inode specified by @hashval 1297 * Search for the inode specified by @hashval and @data in the inode cache. 1545 * If the inode is in the cache, the inode is 1298 * If the inode is in the cache, the inode is returned with an incremented 1546 * reference count. 1299 * reference count. 1547 * 1300 * 1548 * Note: I_NEW is not waited upon so you have 1301 * Note: I_NEW is not waited upon so you have to be very careful what you do 1549 * with the returned inode. You probably sho 1302 * with the returned inode. You probably should be using ilookup5() instead. 1550 * 1303 * 1551 * Note2: @test is called with the inode_hash 1304 * Note2: @test is called with the inode_hash_lock held, so can't sleep. 1552 */ 1305 */ 1553 struct inode *ilookup5_nowait(struct super_bl 1306 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, 1554 int (*test)(struct inode *, v 1307 int (*test)(struct inode *, void *), void *data) 1555 { 1308 { 1556 struct hlist_head *head = inode_hasht 1309 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1557 struct inode *inode; 1310 struct inode *inode; 1558 1311 1559 spin_lock(&inode_hash_lock); 1312 spin_lock(&inode_hash_lock); 1560 inode = find_inode(sb, head, test, da !! 1313 inode = find_inode(sb, head, test, data); 1561 spin_unlock(&inode_hash_lock); 1314 spin_unlock(&inode_hash_lock); 1562 1315 1563 return IS_ERR(inode) ? NULL : inode; 1316 return IS_ERR(inode) ? NULL : inode; 1564 } 1317 } 1565 EXPORT_SYMBOL(ilookup5_nowait); 1318 EXPORT_SYMBOL(ilookup5_nowait); 1566 1319 1567 /** 1320 /** 1568 * ilookup5 - search for an inode in the inod 1321 * ilookup5 - search for an inode in the inode cache 1569 * @sb: super block of file system to 1322 * @sb: super block of file system to search 1570 * @hashval: hash value (usually inode num 1323 * @hashval: hash value (usually inode number) to search for 1571 * @test: callback used for comparisons 1324 * @test: callback used for comparisons between inodes 1572 * @data: opaque data pointer to pass t 1325 * @data: opaque data pointer to pass to @test 1573 * 1326 * 1574 * Search for the inode specified by @hashval 1327 * Search for the inode specified by @hashval and @data in the inode cache, 1575 * and if the inode is in the cache, return t 1328 * and if the inode is in the cache, return the inode with an incremented 1576 * reference count. Waits on I_NEW before re 1329 * reference count. Waits on I_NEW before returning the inode. 1577 * returned with an incremented reference cou 1330 * returned with an incremented reference count. 1578 * 1331 * 1579 * This is a generalized version of ilookup() 1332 * This is a generalized version of ilookup() for file systems where the 1580 * inode number is not sufficient for unique 1333 * inode number is not sufficient for unique identification of an inode. 1581 * 1334 * 1582 * Note: @test is called with the inode_hash_ 1335 * Note: @test is called with the inode_hash_lock held, so can't sleep. 1583 */ 1336 */ 1584 struct inode *ilookup5(struct super_block *sb 1337 struct inode *ilookup5(struct super_block *sb, unsigned long hashval, 1585 int (*test)(struct inode *, v 1338 int (*test)(struct inode *, void *), void *data) 1586 { 1339 { 1587 struct inode *inode; 1340 struct inode *inode; 1588 again: 1341 again: 1589 inode = ilookup5_nowait(sb, hashval, 1342 inode = ilookup5_nowait(sb, hashval, test, data); 1590 if (inode) { 1343 if (inode) { 1591 wait_on_inode(inode); 1344 wait_on_inode(inode); 1592 if (unlikely(inode_unhashed(i 1345 if (unlikely(inode_unhashed(inode))) { 1593 iput(inode); 1346 iput(inode); 1594 goto again; 1347 goto again; 1595 } 1348 } 1596 } 1349 } 1597 return inode; 1350 return inode; 1598 } 1351 } 1599 EXPORT_SYMBOL(ilookup5); 1352 EXPORT_SYMBOL(ilookup5); 1600 1353 1601 /** 1354 /** 1602 * ilookup - search for an inode in the inode 1355 * ilookup - search for an inode in the inode cache 1603 * @sb: super block of file system to 1356 * @sb: super block of file system to search 1604 * @ino: inode number to search for 1357 * @ino: inode number to search for 1605 * 1358 * 1606 * Search for the inode @ino in the inode cac 1359 * Search for the inode @ino in the inode cache, and if the inode is in the 1607 * cache, the inode is returned with an incre 1360 * cache, the inode is returned with an incremented reference count. 1608 */ 1361 */ 1609 struct inode *ilookup(struct super_block *sb, 1362 struct inode *ilookup(struct super_block *sb, unsigned long ino) 1610 { 1363 { 1611 struct hlist_head *head = inode_hasht 1364 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1612 struct inode *inode; 1365 struct inode *inode; 1613 again: 1366 again: 1614 inode = find_inode_fast(sb, head, ino !! 1367 spin_lock(&inode_hash_lock); >> 1368 inode = find_inode_fast(sb, head, ino); >> 1369 spin_unlock(&inode_hash_lock); 1615 1370 1616 if (inode) { 1371 if (inode) { 1617 if (IS_ERR(inode)) 1372 if (IS_ERR(inode)) 1618 return NULL; 1373 return NULL; 1619 wait_on_inode(inode); 1374 wait_on_inode(inode); 1620 if (unlikely(inode_unhashed(i 1375 if (unlikely(inode_unhashed(inode))) { 1621 iput(inode); 1376 iput(inode); 1622 goto again; 1377 goto again; 1623 } 1378 } 1624 } 1379 } 1625 return inode; 1380 return inode; 1626 } 1381 } 1627 EXPORT_SYMBOL(ilookup); 1382 EXPORT_SYMBOL(ilookup); 1628 1383 1629 /** 1384 /** 1630 * find_inode_nowait - find an inode in the i 1385 * find_inode_nowait - find an inode in the inode cache 1631 * @sb: super block of file system to 1386 * @sb: super block of file system to search 1632 * @hashval: hash value (usually inode num 1387 * @hashval: hash value (usually inode number) to search for 1633 * @match: callback used for comparisons 1388 * @match: callback used for comparisons between inodes 1634 * @data: opaque data pointer to pass t 1389 * @data: opaque data pointer to pass to @match 1635 * 1390 * 1636 * Search for the inode specified by @hashval 1391 * Search for the inode specified by @hashval and @data in the inode 1637 * cache, where the helper function @match wi 1392 * cache, where the helper function @match will return 0 if the inode 1638 * does not match, 1 if the inode does match, 1393 * does not match, 1 if the inode does match, and -1 if the search 1639 * should be stopped. The @match function mu 1394 * should be stopped. The @match function must be responsible for 1640 * taking the i_lock spin_lock and checking i 1395 * taking the i_lock spin_lock and checking i_state for an inode being 1641 * freed or being initialized, and incrementi 1396 * freed or being initialized, and incrementing the reference count 1642 * before returning 1. It also must not slee 1397 * before returning 1. It also must not sleep, since it is called with 1643 * the inode_hash_lock spinlock held. 1398 * the inode_hash_lock spinlock held. 1644 * 1399 * 1645 * This is a even more generalized version of 1400 * This is a even more generalized version of ilookup5() when the 1646 * function must never block --- find_inode() 1401 * function must never block --- find_inode() can block in 1647 * __wait_on_freeing_inode() --- or when the 1402 * __wait_on_freeing_inode() --- or when the caller can not increment 1648 * the reference count because the resulting 1403 * the reference count because the resulting iput() might cause an 1649 * inode eviction. The tradeoff is that the 1404 * inode eviction. The tradeoff is that the @match funtion must be 1650 * very carefully implemented. 1405 * very carefully implemented. 1651 */ 1406 */ 1652 struct inode *find_inode_nowait(struct super_ 1407 struct inode *find_inode_nowait(struct super_block *sb, 1653 unsigned long 1408 unsigned long hashval, 1654 int (*match)( 1409 int (*match)(struct inode *, unsigned long, 1655 1410 void *), 1656 void *data) 1411 void *data) 1657 { 1412 { 1658 struct hlist_head *head = inode_hasht 1413 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1659 struct inode *inode, *ret_inode = NUL 1414 struct inode *inode, *ret_inode = NULL; 1660 int mval; 1415 int mval; 1661 1416 1662 spin_lock(&inode_hash_lock); 1417 spin_lock(&inode_hash_lock); 1663 hlist_for_each_entry(inode, head, i_h 1418 hlist_for_each_entry(inode, head, i_hash) { 1664 if (inode->i_sb != sb) 1419 if (inode->i_sb != sb) 1665 continue; 1420 continue; 1666 mval = match(inode, hashval, 1421 mval = match(inode, hashval, data); 1667 if (mval == 0) 1422 if (mval == 0) 1668 continue; 1423 continue; 1669 if (mval == 1) 1424 if (mval == 1) 1670 ret_inode = inode; 1425 ret_inode = inode; 1671 goto out; 1426 goto out; 1672 } 1427 } 1673 out: 1428 out: 1674 spin_unlock(&inode_hash_lock); 1429 spin_unlock(&inode_hash_lock); 1675 return ret_inode; 1430 return ret_inode; 1676 } 1431 } 1677 EXPORT_SYMBOL(find_inode_nowait); 1432 EXPORT_SYMBOL(find_inode_nowait); 1678 1433 1679 /** << 1680 * find_inode_rcu - find an inode in the inod << 1681 * @sb: Super block of file system to << 1682 * @hashval: Key to hash << 1683 * @test: Function to test match on an << 1684 * @data: Data for test function << 1685 * << 1686 * Search for the inode specified by @hashval << 1687 * where the helper function @test will retur << 1688 * and 1 if it does. The @test function must << 1689 * i_lock spin_lock and checking i_state for << 1690 * initialized. << 1691 * << 1692 * If successful, this will return the inode << 1693 * returned 1 and NULL otherwise. << 1694 * << 1695 * The @test function is not permitted to tak << 1696 * It is also not permitted to sleep. << 1697 * << 1698 * The caller must hold the RCU read lock. << 1699 */ << 1700 struct inode *find_inode_rcu(struct super_blo << 1701 int (*test)(stru << 1702 { << 1703 struct hlist_head *head = inode_hasht << 1704 struct inode *inode; << 1705 << 1706 RCU_LOCKDEP_WARN(!rcu_read_lock_held( << 1707 "suspicious find_ino << 1708 << 1709 hlist_for_each_entry_rcu(inode, head, << 1710 if (inode->i_sb == sb && << 1711 !(READ_ONCE(inode->i_stat << 1712 test(inode, data)) << 1713 return inode; << 1714 } << 1715 return NULL; << 1716 } << 1717 EXPORT_SYMBOL(find_inode_rcu); << 1718 << 1719 /** << 1720 * find_inode_by_ino_rcu - Find an inode in t << 1721 * @sb: Super block of file system to << 1722 * @ino: The inode number to match << 1723 * << 1724 * Search for the inode specified by @hashval << 1725 * where the helper function @test will retur << 1726 * and 1 if it does. The @test function must << 1727 * i_lock spin_lock and checking i_state for << 1728 * initialized. << 1729 * << 1730 * If successful, this will return the inode << 1731 * returned 1 and NULL otherwise. << 1732 * << 1733 * The @test function is not permitted to tak << 1734 * It is also not permitted to sleep. << 1735 * << 1736 * The caller must hold the RCU read lock. << 1737 */ << 1738 struct inode *find_inode_by_ino_rcu(struct su << 1739 unsigned << 1740 { << 1741 struct hlist_head *head = inode_hasht << 1742 struct inode *inode; << 1743 << 1744 RCU_LOCKDEP_WARN(!rcu_read_lock_held( << 1745 "suspicious find_ino << 1746 << 1747 hlist_for_each_entry_rcu(inode, head, << 1748 if (inode->i_ino == ino && << 1749 inode->i_sb == sb && << 1750 !(READ_ONCE(inode->i_stat << 1751 return inode; << 1752 } << 1753 return NULL; << 1754 } << 1755 EXPORT_SYMBOL(find_inode_by_ino_rcu); << 1756 << 1757 int insert_inode_locked(struct inode *inode) 1434 int insert_inode_locked(struct inode *inode) 1758 { 1435 { 1759 struct super_block *sb = inode->i_sb; 1436 struct super_block *sb = inode->i_sb; 1760 ino_t ino = inode->i_ino; 1437 ino_t ino = inode->i_ino; 1761 struct hlist_head *head = inode_hasht 1438 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1762 1439 1763 while (1) { 1440 while (1) { 1764 struct inode *old = NULL; 1441 struct inode *old = NULL; 1765 spin_lock(&inode_hash_lock); 1442 spin_lock(&inode_hash_lock); 1766 hlist_for_each_entry(old, hea 1443 hlist_for_each_entry(old, head, i_hash) { 1767 if (old->i_ino != ino 1444 if (old->i_ino != ino) 1768 continue; 1445 continue; 1769 if (old->i_sb != sb) 1446 if (old->i_sb != sb) 1770 continue; 1447 continue; 1771 spin_lock(&old->i_loc 1448 spin_lock(&old->i_lock); 1772 if (old->i_state & (I 1449 if (old->i_state & (I_FREEING|I_WILL_FREE)) { 1773 spin_unlock(& 1450 spin_unlock(&old->i_lock); 1774 continue; 1451 continue; 1775 } 1452 } 1776 break; 1453 break; 1777 } 1454 } 1778 if (likely(!old)) { 1455 if (likely(!old)) { 1779 spin_lock(&inode->i_l 1456 spin_lock(&inode->i_lock); 1780 inode->i_state |= I_N 1457 inode->i_state |= I_NEW | I_CREATING; 1781 hlist_add_head_rcu(&i !! 1458 hlist_add_head(&inode->i_hash, head); 1782 spin_unlock(&inode->i 1459 spin_unlock(&inode->i_lock); 1783 spin_unlock(&inode_ha 1460 spin_unlock(&inode_hash_lock); 1784 return 0; 1461 return 0; 1785 } 1462 } 1786 if (unlikely(old->i_state & I 1463 if (unlikely(old->i_state & I_CREATING)) { 1787 spin_unlock(&old->i_l 1464 spin_unlock(&old->i_lock); 1788 spin_unlock(&inode_ha 1465 spin_unlock(&inode_hash_lock); 1789 return -EBUSY; 1466 return -EBUSY; 1790 } 1467 } 1791 __iget(old); 1468 __iget(old); 1792 spin_unlock(&old->i_lock); 1469 spin_unlock(&old->i_lock); 1793 spin_unlock(&inode_hash_lock) 1470 spin_unlock(&inode_hash_lock); 1794 wait_on_inode(old); 1471 wait_on_inode(old); 1795 if (unlikely(!inode_unhashed( 1472 if (unlikely(!inode_unhashed(old))) { 1796 iput(old); 1473 iput(old); 1797 return -EBUSY; 1474 return -EBUSY; 1798 } 1475 } 1799 iput(old); 1476 iput(old); 1800 } 1477 } 1801 } 1478 } 1802 EXPORT_SYMBOL(insert_inode_locked); 1479 EXPORT_SYMBOL(insert_inode_locked); 1803 1480 1804 int insert_inode_locked4(struct inode *inode, 1481 int insert_inode_locked4(struct inode *inode, unsigned long hashval, 1805 int (*test)(struct inode *, v 1482 int (*test)(struct inode *, void *), void *data) 1806 { 1483 { 1807 struct inode *old; 1484 struct inode *old; 1808 1485 1809 inode->i_state |= I_CREATING; 1486 inode->i_state |= I_CREATING; 1810 old = inode_insert5(inode, hashval, t 1487 old = inode_insert5(inode, hashval, test, NULL, data); 1811 1488 1812 if (old != inode) { 1489 if (old != inode) { 1813 iput(old); 1490 iput(old); 1814 return -EBUSY; 1491 return -EBUSY; 1815 } 1492 } 1816 return 0; 1493 return 0; 1817 } 1494 } 1818 EXPORT_SYMBOL(insert_inode_locked4); 1495 EXPORT_SYMBOL(insert_inode_locked4); 1819 1496 1820 1497 1821 int generic_delete_inode(struct inode *inode) 1498 int generic_delete_inode(struct inode *inode) 1822 { 1499 { 1823 return 1; 1500 return 1; 1824 } 1501 } 1825 EXPORT_SYMBOL(generic_delete_inode); 1502 EXPORT_SYMBOL(generic_delete_inode); 1826 1503 1827 /* 1504 /* 1828 * Called when we're dropping the last refere 1505 * Called when we're dropping the last reference 1829 * to an inode. 1506 * to an inode. 1830 * 1507 * 1831 * Call the FS "drop_inode()" function, defau 1508 * Call the FS "drop_inode()" function, defaulting to 1832 * the legacy UNIX filesystem behaviour. If 1509 * the legacy UNIX filesystem behaviour. If it tells 1833 * us to evict inode, do so. Otherwise, reta 1510 * us to evict inode, do so. Otherwise, retain inode 1834 * in cache if fs is alive, sync and evict if 1511 * in cache if fs is alive, sync and evict if fs is 1835 * shutting down. 1512 * shutting down. 1836 */ 1513 */ 1837 static void iput_final(struct inode *inode) 1514 static void iput_final(struct inode *inode) 1838 { 1515 { 1839 struct super_block *sb = inode->i_sb; 1516 struct super_block *sb = inode->i_sb; 1840 const struct super_operations *op = i 1517 const struct super_operations *op = inode->i_sb->s_op; 1841 unsigned long state; << 1842 int drop; 1518 int drop; 1843 1519 1844 WARN_ON(inode->i_state & I_NEW); 1520 WARN_ON(inode->i_state & I_NEW); 1845 1521 1846 if (op->drop_inode) 1522 if (op->drop_inode) 1847 drop = op->drop_inode(inode); 1523 drop = op->drop_inode(inode); 1848 else 1524 else 1849 drop = generic_drop_inode(ino 1525 drop = generic_drop_inode(inode); 1850 1526 1851 if (!drop && !! 1527 if (!drop && (sb->s_flags & SB_ACTIVE)) { 1852 !(inode->i_state & I_DONTCACHE) & !! 1528 inode_add_lru(inode); 1853 (sb->s_flags & SB_ACTIVE)) { << 1854 __inode_add_lru(inode, true); << 1855 spin_unlock(&inode->i_lock); 1529 spin_unlock(&inode->i_lock); 1856 return; 1530 return; 1857 } 1531 } 1858 1532 1859 state = inode->i_state; << 1860 if (!drop) { 1533 if (!drop) { 1861 WRITE_ONCE(inode->i_state, st !! 1534 inode->i_state |= I_WILL_FREE; 1862 spin_unlock(&inode->i_lock); 1535 spin_unlock(&inode->i_lock); 1863 << 1864 write_inode_now(inode, 1); 1536 write_inode_now(inode, 1); 1865 << 1866 spin_lock(&inode->i_lock); 1537 spin_lock(&inode->i_lock); 1867 state = inode->i_state; !! 1538 WARN_ON(inode->i_state & I_NEW); 1868 WARN_ON(state & I_NEW); !! 1539 inode->i_state &= ~I_WILL_FREE; 1869 state &= ~I_WILL_FREE; << 1870 } 1540 } 1871 1541 1872 WRITE_ONCE(inode->i_state, state | I_ !! 1542 inode->i_state |= I_FREEING; 1873 if (!list_empty(&inode->i_lru)) 1543 if (!list_empty(&inode->i_lru)) 1874 inode_lru_list_del(inode); 1544 inode_lru_list_del(inode); 1875 spin_unlock(&inode->i_lock); 1545 spin_unlock(&inode->i_lock); 1876 1546 1877 evict(inode); 1547 evict(inode); 1878 } 1548 } 1879 1549 1880 /** 1550 /** 1881 * iput - put an inode 1551 * iput - put an inode 1882 * @inode: inode to put 1552 * @inode: inode to put 1883 * 1553 * 1884 * Puts an inode, dropping its usage cou 1554 * Puts an inode, dropping its usage count. If the inode use count hits 1885 * zero, the inode is then freed and may 1555 * zero, the inode is then freed and may also be destroyed. 1886 * 1556 * 1887 * Consequently, iput() can sleep. 1557 * Consequently, iput() can sleep. 1888 */ 1558 */ 1889 void iput(struct inode *inode) 1559 void iput(struct inode *inode) 1890 { 1560 { 1891 if (!inode) 1561 if (!inode) 1892 return; 1562 return; 1893 BUG_ON(inode->i_state & I_CLEAR); 1563 BUG_ON(inode->i_state & I_CLEAR); 1894 retry: 1564 retry: 1895 if (atomic_dec_and_lock(&inode->i_cou 1565 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) { 1896 if (inode->i_nlink && (inode- 1566 if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) { 1897 atomic_inc(&inode->i_ 1567 atomic_inc(&inode->i_count); 1898 spin_unlock(&inode->i 1568 spin_unlock(&inode->i_lock); 1899 trace_writeback_lazyt 1569 trace_writeback_lazytime_iput(inode); 1900 mark_inode_dirty_sync 1570 mark_inode_dirty_sync(inode); 1901 goto retry; 1571 goto retry; 1902 } 1572 } 1903 iput_final(inode); 1573 iput_final(inode); 1904 } 1574 } 1905 } 1575 } 1906 EXPORT_SYMBOL(iput); 1576 EXPORT_SYMBOL(iput); 1907 1577 1908 #ifdef CONFIG_BLOCK << 1909 /** 1578 /** 1910 * bmap - find a block number in a fi 1579 * bmap - find a block number in a file 1911 * @inode: inode owning the block numbe !! 1580 * @inode: inode of file 1912 * @block: pointer containing the block !! 1581 * @block: block to find 1913 * 1582 * 1914 * Replaces the value in ``*block`` with !! 1583 * Returns the block number on the device holding the inode that 1915 * corresponding to the requested block !! 1584 * is the disk block number for the block of the file requested. 1916 * That is, asked for block 4 of inode 1 !! 1585 * That is, asked for block 4 of inode 1 the function will return the 1917 * 4 in ``*block``, with disk block rela !! 1586 * disk block relative to the disk start that holds that block of the 1918 * block of the file. !! 1587 * file. 1919 * !! 1588 */ 1920 * Returns -EINVAL in case of error, 0 o !! 1589 sector_t bmap(struct inode *inode, sector_t block) 1921 * hole, returns 0 and ``*block`` is als !! 1590 { >> 1591 sector_t res = 0; >> 1592 if (inode->i_mapping->a_ops->bmap) >> 1593 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); >> 1594 return res; >> 1595 } >> 1596 EXPORT_SYMBOL(bmap); >> 1597 >> 1598 /* >> 1599 * Update times in overlayed inode from underlying real inode 1922 */ 1600 */ 1923 int bmap(struct inode *inode, sector_t *block !! 1601 static void update_ovl_inode_times(struct dentry *dentry, struct inode *inode, >> 1602 bool rcu) 1924 { 1603 { 1925 if (!inode->i_mapping->a_ops->bmap) !! 1604 struct dentry *upperdentry; 1926 return -EINVAL; << 1927 1605 1928 *block = inode->i_mapping->a_ops->bma !! 1606 /* 1929 return 0; !! 1607 * Nothing to do if in rcu or if non-overlayfs >> 1608 */ >> 1609 if (rcu || likely(!(dentry->d_flags & DCACHE_OP_REAL))) >> 1610 return; >> 1611 >> 1612 upperdentry = d_real(dentry, NULL, 0, D_REAL_UPPER); >> 1613 >> 1614 /* >> 1615 * If file is on lower then we can't update atime, so no worries about >> 1616 * stale mtime/ctime. >> 1617 */ >> 1618 if (upperdentry) { >> 1619 struct inode *realinode = d_inode(upperdentry); >> 1620 >> 1621 if ((!timespec64_equal(&inode->i_mtime, &realinode->i_mtime) || >> 1622 !timespec64_equal(&inode->i_ctime, &realinode->i_ctime))) { >> 1623 inode->i_mtime = realinode->i_mtime; >> 1624 inode->i_ctime = realinode->i_ctime; >> 1625 } >> 1626 } 1930 } 1627 } 1931 EXPORT_SYMBOL(bmap); << 1932 #endif << 1933 1628 1934 /* 1629 /* 1935 * With relative atime, only update atime if 1630 * With relative atime, only update atime if the previous atime is 1936 * earlier than or equal to either the ctime !! 1631 * earlier than either the ctime or mtime or if at least a day has 1937 * or if at least a day has passed since the !! 1632 * passed since the last atime update. 1938 */ 1633 */ 1939 static bool relatime_need_update(struct vfsmo !! 1634 static int relatime_need_update(const struct path *path, struct inode *inode, 1940 struct timespec6 !! 1635 struct timespec now, bool rcu) 1941 { 1636 { 1942 struct timespec64 atime, mtime, ctime << 1943 1637 1944 if (!(mnt->mnt_flags & MNT_RELATIME)) !! 1638 if (!(path->mnt->mnt_flags & MNT_RELATIME)) 1945 return true; !! 1639 return 1; >> 1640 >> 1641 update_ovl_inode_times(path->dentry, inode, rcu); 1946 /* 1642 /* 1947 * Is mtime younger than or equal to !! 1643 * Is mtime younger than atime? If yes, update atime: 1948 */ 1644 */ 1949 atime = inode_get_atime(inode); !! 1645 if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0) 1950 mtime = inode_get_mtime(inode); !! 1646 return 1; 1951 if (timespec64_compare(&mtime, &atime << 1952 return true; << 1953 /* 1647 /* 1954 * Is ctime younger than or equal to !! 1648 * Is ctime younger than atime? If yes, update atime: 1955 */ 1649 */ 1956 ctime = inode_get_ctime(inode); !! 1650 if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0) 1957 if (timespec64_compare(&ctime, &atime !! 1651 return 1; 1958 return true; << 1959 1652 1960 /* 1653 /* 1961 * Is the previous atime value older 1654 * Is the previous atime value older than a day? If yes, 1962 * update atime: 1655 * update atime: 1963 */ 1656 */ 1964 if ((long)(now.tv_sec - atime.tv_sec) !! 1657 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60) 1965 return true; !! 1658 return 1; 1966 /* 1659 /* 1967 * Good, we can skip the atime update 1660 * Good, we can skip the atime update: 1968 */ 1661 */ 1969 return false; !! 1662 return 0; 1970 } << 1971 << 1972 /** << 1973 * inode_update_timestamps - update the times << 1974 * @inode: inode to be updated << 1975 * @flags: S_* flags that needed to be update << 1976 * << 1977 * The update_time function is called when an << 1978 * updated for a read or write operation. Thi << 1979 * actual timestamps. It's up to the caller t << 1980 * dirty appropriately. << 1981 * << 1982 * In the case where any of S_MTIME, S_CTIME, << 1983 * attempt to update all three of them. S_ATI << 1984 * independently of the rest. << 1985 * << 1986 * Returns a set of S_* flags indicating whic << 1987 */ << 1988 int inode_update_timestamps(struct inode *ino << 1989 { << 1990 int updated = 0; << 1991 struct timespec64 now; << 1992 << 1993 if (flags & (S_MTIME|S_CTIME|S_VERSIO << 1994 struct timespec64 ctime = ino << 1995 struct timespec64 mtime = ino << 1996 << 1997 now = inode_set_ctime_current << 1998 if (!timespec64_equal(&now, & << 1999 updated |= S_CTIME; << 2000 if (!timespec64_equal(&now, & << 2001 inode_set_mtime_to_ts << 2002 updated |= S_MTIME; << 2003 } << 2004 if (IS_I_VERSION(inode) && in << 2005 updated |= S_VERSION; << 2006 } else { << 2007 now = current_time(inode); << 2008 } << 2009 << 2010 if (flags & S_ATIME) { << 2011 struct timespec64 atime = ino << 2012 << 2013 if (!timespec64_equal(&now, & << 2014 inode_set_atime_to_ts << 2015 updated |= S_ATIME; << 2016 } << 2017 } << 2018 return updated; << 2019 } 1663 } 2020 EXPORT_SYMBOL(inode_update_timestamps); << 2021 1664 2022 /** !! 1665 int generic_update_time(struct inode *inode, struct timespec64 *time, int flags) 2023 * generic_update_time - update the timestamp << 2024 * @inode: inode to be updated << 2025 * @flags: S_* flags that needed to be update << 2026 * << 2027 * The update_time function is called when an << 2028 * updated for a read or write operation. In << 2029 * or S_VERSION need to be updated we attempt << 2030 * updates can be handled done independently << 2031 * << 2032 * Returns a S_* mask indicating which fields << 2033 */ << 2034 int generic_update_time(struct inode *inode, << 2035 { 1666 { 2036 int updated = inode_update_timestamps !! 1667 int iflags = I_DIRTY_TIME; 2037 int dirty_flags = 0; !! 1668 bool dirty = false; 2038 1669 2039 if (updated & (S_ATIME|S_MTIME|S_CTIM !! 1670 if (flags & S_ATIME) 2040 dirty_flags = inode->i_sb->s_ !! 1671 inode->i_atime = *time; 2041 if (updated & S_VERSION) !! 1672 if (flags & S_VERSION) 2042 dirty_flags |= I_DIRTY_SYNC; !! 1673 dirty = inode_maybe_inc_iversion(inode, false); 2043 __mark_inode_dirty(inode, dirty_flags !! 1674 if (flags & S_CTIME) 2044 return updated; !! 1675 inode->i_ctime = *time; >> 1676 if (flags & S_MTIME) >> 1677 inode->i_mtime = *time; >> 1678 if ((flags & (S_ATIME | S_CTIME | S_MTIME)) && >> 1679 !(inode->i_sb->s_flags & SB_LAZYTIME)) >> 1680 dirty = true; >> 1681 >> 1682 if (dirty) >> 1683 iflags |= I_DIRTY_SYNC; >> 1684 __mark_inode_dirty(inode, iflags); >> 1685 return 0; 2045 } 1686 } 2046 EXPORT_SYMBOL(generic_update_time); 1687 EXPORT_SYMBOL(generic_update_time); 2047 1688 2048 /* 1689 /* 2049 * This does the actual work of updating an i 1690 * This does the actual work of updating an inodes time or version. Must have 2050 * had called mnt_want_write() before calling 1691 * had called mnt_want_write() before calling this. 2051 */ 1692 */ 2052 int inode_update_time(struct inode *inode, in !! 1693 static int update_time(struct inode *inode, struct timespec64 *time, int flags) 2053 { 1694 { 2054 if (inode->i_op->update_time) !! 1695 int (*update_time)(struct inode *, struct timespec64 *, int); 2055 return inode->i_op->update_ti !! 1696 2056 generic_update_time(inode, flags); !! 1697 update_time = inode->i_op->update_time ? inode->i_op->update_time : 2057 return 0; !! 1698 generic_update_time; >> 1699 >> 1700 return update_time(inode, time, flags); 2058 } 1701 } 2059 EXPORT_SYMBOL(inode_update_time); << 2060 1702 2061 /** 1703 /** 2062 * atime_needs_update - updat !! 1704 * touch_atime - update the access time 2063 * @path: the &struct path to update 1705 * @path: the &struct path to update 2064 * @inode: inode to update 1706 * @inode: inode to update 2065 * 1707 * 2066 * Update the accessed time on an inode 1708 * Update the accessed time on an inode and mark it for writeback. 2067 * This function automatically handles r 1709 * This function automatically handles read only file systems and media, 2068 * as well as the "noatime" flag and ino 1710 * as well as the "noatime" flag and inode specific "noatime" markers. 2069 */ 1711 */ 2070 bool atime_needs_update(const struct path *pa !! 1712 bool __atime_needs_update(const struct path *path, struct inode *inode, >> 1713 bool rcu) 2071 { 1714 { 2072 struct vfsmount *mnt = path->mnt; 1715 struct vfsmount *mnt = path->mnt; 2073 struct timespec64 now, atime; !! 1716 struct timespec64 now; 2074 1717 2075 if (inode->i_flags & S_NOATIME) 1718 if (inode->i_flags & S_NOATIME) 2076 return false; 1719 return false; 2077 1720 2078 /* Atime updates will likely cause i_ 1721 /* Atime updates will likely cause i_uid and i_gid to be written 2079 * back improprely if their true valu 1722 * back improprely if their true value is unknown to the vfs. 2080 */ 1723 */ 2081 if (HAS_UNMAPPED_ID(mnt_idmap(mnt), i !! 1724 if (HAS_UNMAPPED_ID(inode)) 2082 return false; 1725 return false; 2083 1726 2084 if (IS_NOATIME(inode)) 1727 if (IS_NOATIME(inode)) 2085 return false; 1728 return false; 2086 if ((inode->i_sb->s_flags & SB_NODIRA 1729 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode)) 2087 return false; 1730 return false; 2088 1731 2089 if (mnt->mnt_flags & MNT_NOATIME) 1732 if (mnt->mnt_flags & MNT_NOATIME) 2090 return false; 1733 return false; 2091 if ((mnt->mnt_flags & MNT_NODIRATIME) 1734 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) 2092 return false; 1735 return false; 2093 1736 2094 now = current_time(inode); 1737 now = current_time(inode); 2095 1738 2096 if (!relatime_need_update(mnt, inode, !! 1739 if (!relatime_need_update(path, inode, timespec64_to_timespec(now), rcu)) 2097 return false; 1740 return false; 2098 1741 2099 atime = inode_get_atime(inode); !! 1742 if (timespec64_equal(&inode->i_atime, &now)) 2100 if (timespec64_equal(&atime, &now)) << 2101 return false; 1743 return false; 2102 1744 2103 return true; 1745 return true; 2104 } 1746 } 2105 1747 2106 void touch_atime(const struct path *path) 1748 void touch_atime(const struct path *path) 2107 { 1749 { 2108 struct vfsmount *mnt = path->mnt; 1750 struct vfsmount *mnt = path->mnt; 2109 struct inode *inode = d_inode(path->d 1751 struct inode *inode = d_inode(path->dentry); >> 1752 struct timespec64 now; 2110 1753 2111 if (!atime_needs_update(path, inode)) !! 1754 if (!__atime_needs_update(path, inode, false)) 2112 return; 1755 return; 2113 1756 2114 if (!sb_start_write_trylock(inode->i_ 1757 if (!sb_start_write_trylock(inode->i_sb)) 2115 return; 1758 return; 2116 1759 2117 if (mnt_get_write_access(mnt) != 0) !! 1760 if (__mnt_want_write(mnt) != 0) 2118 goto skip_update; 1761 goto skip_update; 2119 /* 1762 /* 2120 * File systems can error out when up 1763 * File systems can error out when updating inodes if they need to 2121 * allocate new space to modify an in 1764 * allocate new space to modify an inode (such is the case for 2122 * Btrfs), but since we touch atime w 1765 * Btrfs), but since we touch atime while walking down the path we 2123 * really don't care if we failed to 1766 * really don't care if we failed to update the atime of the file, 2124 * so just ignore the return value. 1767 * so just ignore the return value. 2125 * We may also fail on filesystems th 1768 * We may also fail on filesystems that have the ability to make parts 2126 * of the fs read only, e.g. subvolum 1769 * of the fs read only, e.g. subvolumes in Btrfs. 2127 */ 1770 */ 2128 inode_update_time(inode, S_ATIME); !! 1771 now = current_time(inode); 2129 mnt_put_write_access(mnt); !! 1772 update_time(inode, &now, S_ATIME); >> 1773 __mnt_drop_write(mnt); 2130 skip_update: 1774 skip_update: 2131 sb_end_write(inode->i_sb); 1775 sb_end_write(inode->i_sb); 2132 } 1776 } 2133 EXPORT_SYMBOL(touch_atime); 1777 EXPORT_SYMBOL(touch_atime); 2134 1778 2135 /* 1779 /* >> 1780 * The logic we want is >> 1781 * >> 1782 * if suid or (sgid and xgrp) >> 1783 * remove privs >> 1784 */ >> 1785 int should_remove_suid(struct dentry *dentry) >> 1786 { >> 1787 umode_t mode = d_inode(dentry)->i_mode; >> 1788 int kill = 0; >> 1789 >> 1790 /* suid always must be killed */ >> 1791 if (unlikely(mode & S_ISUID)) >> 1792 kill = ATTR_KILL_SUID; >> 1793 >> 1794 /* >> 1795 * sgid without any exec bits is just a mandatory locking mark; leave >> 1796 * it alone. If some exec bits are set, it's a real sgid; kill it. >> 1797 */ >> 1798 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) >> 1799 kill |= ATTR_KILL_SGID; >> 1800 >> 1801 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) >> 1802 return kill; >> 1803 >> 1804 return 0; >> 1805 } >> 1806 EXPORT_SYMBOL(should_remove_suid); >> 1807 >> 1808 /* 2136 * Return mask of changes for notify_change() 1809 * Return mask of changes for notify_change() that need to be done as a 2137 * response to write or truncate. Return 0 if 1810 * response to write or truncate. Return 0 if nothing has to be changed. 2138 * Negative value on error (change should be 1811 * Negative value on error (change should be denied). 2139 */ 1812 */ 2140 int dentry_needs_remove_privs(struct mnt_idma !! 1813 int dentry_needs_remove_privs(struct dentry *dentry) 2141 struct dentry * << 2142 { 1814 { 2143 struct inode *inode = d_inode(dentry) 1815 struct inode *inode = d_inode(dentry); 2144 int mask = 0; 1816 int mask = 0; 2145 int ret; 1817 int ret; 2146 1818 2147 if (IS_NOSEC(inode)) 1819 if (IS_NOSEC(inode)) 2148 return 0; 1820 return 0; 2149 1821 2150 mask = setattr_should_drop_suidgid(id !! 1822 mask = should_remove_suid(dentry); 2151 ret = security_inode_need_killpriv(de 1823 ret = security_inode_need_killpriv(dentry); 2152 if (ret < 0) 1824 if (ret < 0) 2153 return ret; 1825 return ret; 2154 if (ret) 1826 if (ret) 2155 mask |= ATTR_KILL_PRIV; 1827 mask |= ATTR_KILL_PRIV; 2156 return mask; 1828 return mask; 2157 } 1829 } 2158 1830 2159 static int __remove_privs(struct mnt_idmap *i !! 1831 static int __remove_privs(struct dentry *dentry, int kill) 2160 struct dentry *dent << 2161 { 1832 { 2162 struct iattr newattrs; 1833 struct iattr newattrs; 2163 1834 2164 newattrs.ia_valid = ATTR_FORCE | kill 1835 newattrs.ia_valid = ATTR_FORCE | kill; 2165 /* 1836 /* 2166 * Note we call this on write, so not 1837 * Note we call this on write, so notify_change will not 2167 * encounter any conflicting delegati 1838 * encounter any conflicting delegations: 2168 */ 1839 */ 2169 return notify_change(idmap, dentry, & !! 1840 return notify_change(dentry, &newattrs, NULL); 2170 } 1841 } 2171 1842 2172 int file_remove_privs_flags(struct file *file !! 1843 /* >> 1844 * Remove special file priviledges (suid, capabilities) when file is written >> 1845 * to or truncated. >> 1846 */ >> 1847 int file_remove_privs(struct file *file) 2173 { 1848 { 2174 struct dentry *dentry = file_dentry(f 1849 struct dentry *dentry = file_dentry(file); 2175 struct inode *inode = file_inode(file 1850 struct inode *inode = file_inode(file); 2176 int error = 0; << 2177 int kill; 1851 int kill; >> 1852 int error = 0; 2178 1853 2179 if (IS_NOSEC(inode) || !S_ISREG(inode !! 1854 /* Fast path for nothing security related */ >> 1855 if (IS_NOSEC(inode)) 2180 return 0; 1856 return 0; 2181 1857 2182 kill = dentry_needs_remove_privs(file !! 1858 kill = dentry_needs_remove_privs(dentry); 2183 if (kill < 0) 1859 if (kill < 0) 2184 return kill; 1860 return kill; 2185 !! 1861 if (kill) 2186 if (kill) { !! 1862 error = __remove_privs(dentry, kill); 2187 if (flags & IOCB_NOWAIT) << 2188 return -EAGAIN; << 2189 << 2190 error = __remove_privs(file_m << 2191 } << 2192 << 2193 if (!error) 1863 if (!error) 2194 inode_has_no_xattr(inode); 1864 inode_has_no_xattr(inode); >> 1865 2195 return error; 1866 return error; 2196 } 1867 } 2197 EXPORT_SYMBOL_GPL(file_remove_privs_flags); !! 1868 EXPORT_SYMBOL(file_remove_privs); 2198 1869 2199 /** 1870 /** 2200 * file_remove_privs - remove special file pr !! 1871 * file_update_time - update mtime and ctime time 2201 * @file: file to remove privileges from !! 1872 * @file: file accessed 2202 * 1873 * 2203 * When file is modified by a write or trunca !! 1874 * Update the mtime and ctime members of an inode and mark the inode 2204 * file privileges are removed. !! 1875 * for writeback. Note that this function is meant exclusively for 2205 * !! 1876 * usage in the file write path of filesystems, and filesystems may 2206 * Return: 0 on success, negative errno on fa !! 1877 * choose to explicitly ignore update via this function with the >> 1878 * S_NOCMTIME inode flag, e.g. for network filesystem where these >> 1879 * timestamps are handled by the server. This can return an error for >> 1880 * file systems who need to allocate space in order to update an inode. 2207 */ 1881 */ 2208 int file_remove_privs(struct file *file) << 2209 { << 2210 return file_remove_privs_flags(file, << 2211 } << 2212 EXPORT_SYMBOL(file_remove_privs); << 2213 1882 2214 static int inode_needs_update_time(struct ino !! 1883 int file_update_time(struct file *file) 2215 { 1884 { >> 1885 struct inode *inode = file_inode(file); >> 1886 struct timespec64 now; 2216 int sync_it = 0; 1887 int sync_it = 0; 2217 struct timespec64 now = current_time( !! 1888 int ret; 2218 struct timespec64 ts; << 2219 1889 2220 /* First try to exhaust all avenues t 1890 /* First try to exhaust all avenues to not sync */ 2221 if (IS_NOCMTIME(inode)) 1891 if (IS_NOCMTIME(inode)) 2222 return 0; 1892 return 0; 2223 1893 2224 ts = inode_get_mtime(inode); !! 1894 now = current_time(inode); 2225 if (!timespec64_equal(&ts, &now)) !! 1895 if (!timespec64_equal(&inode->i_mtime, &now)) 2226 sync_it = S_MTIME; 1896 sync_it = S_MTIME; 2227 1897 2228 ts = inode_get_ctime(inode); !! 1898 if (!timespec64_equal(&inode->i_ctime, &now)) 2229 if (!timespec64_equal(&ts, &now)) << 2230 sync_it |= S_CTIME; 1899 sync_it |= S_CTIME; 2231 1900 2232 if (IS_I_VERSION(inode) && inode_iver 1901 if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode)) 2233 sync_it |= S_VERSION; 1902 sync_it |= S_VERSION; 2234 1903 2235 return sync_it; !! 1904 if (!sync_it) 2236 } !! 1905 return 0; 2237 1906 2238 static int __file_update_time(struct file *fi !! 1907 /* Finally allowed to write? Takes lock. */ 2239 { !! 1908 if (__mnt_want_write_file(file)) 2240 int ret = 0; !! 1909 return 0; 2241 struct inode *inode = file_inode(file << 2242 1910 2243 /* try to update time settings */ !! 1911 ret = update_time(inode, &now, sync_it); 2244 if (!mnt_get_write_access_file(file)) !! 1912 __mnt_drop_write_file(file); 2245 ret = inode_update_time(inode << 2246 mnt_put_write_access_file(fil << 2247 } << 2248 1913 2249 return ret; 1914 return ret; 2250 } 1915 } 2251 << 2252 /** << 2253 * file_update_time - update mtime and ctime << 2254 * @file: file accessed << 2255 * << 2256 * Update the mtime and ctime members of an i << 2257 * writeback. Note that this function is mean << 2258 * the file write path of filesystems, and fi << 2259 * explicitly ignore updates via this functio << 2260 * flag, e.g. for network filesystem where th << 2261 * by the server. This can return an error fo << 2262 * allocate space in order to update an inode << 2263 * << 2264 * Return: 0 on success, negative errno on fa << 2265 */ << 2266 int file_update_time(struct file *file) << 2267 { << 2268 int ret; << 2269 struct inode *inode = file_inode(file << 2270 << 2271 ret = inode_needs_update_time(inode); << 2272 if (ret <= 0) << 2273 return ret; << 2274 << 2275 return __file_update_time(file, ret); << 2276 } << 2277 EXPORT_SYMBOL(file_update_time); 1916 EXPORT_SYMBOL(file_update_time); 2278 1917 2279 /** << 2280 * file_modified_flags - handle mandated vfs << 2281 * @file: file that was modified << 2282 * @flags: kiocb flags << 2283 * << 2284 * When file has been modified ensure that sp << 2285 * file privileges are removed and time setti << 2286 * << 2287 * If IOCB_NOWAIT is set, special file privil << 2288 * time settings will not be updated. It will << 2289 * << 2290 * Context: Caller must hold the file's inode << 2291 * << 2292 * Return: 0 on success, negative errno on fa << 2293 */ << 2294 static int file_modified_flags(struct file *f << 2295 { << 2296 int ret; << 2297 struct inode *inode = file_inode(file << 2298 << 2299 /* << 2300 * Clear the security bits if the pro << 2301 * This keeps people from modifying s << 2302 */ << 2303 ret = file_remove_privs_flags(file, f << 2304 if (ret) << 2305 return ret; << 2306 << 2307 if (unlikely(file->f_mode & FMODE_NOC << 2308 return 0; << 2309 << 2310 ret = inode_needs_update_time(inode); << 2311 if (ret <= 0) << 2312 return ret; << 2313 if (flags & IOCB_NOWAIT) << 2314 return -EAGAIN; << 2315 << 2316 return __file_update_time(file, ret); << 2317 } << 2318 << 2319 /** << 2320 * file_modified - handle mandated vfs change << 2321 * @file: file that was modified << 2322 * << 2323 * When file has been modified ensure that sp << 2324 * file privileges are removed and time setti << 2325 * << 2326 * Context: Caller must hold the file's inode << 2327 * << 2328 * Return: 0 on success, negative errno on fa << 2329 */ << 2330 int file_modified(struct file *file) << 2331 { << 2332 return file_modified_flags(file, 0); << 2333 } << 2334 EXPORT_SYMBOL(file_modified); << 2335 << 2336 /** << 2337 * kiocb_modified - handle mandated vfs chang << 2338 * @iocb: iocb that was modified << 2339 * << 2340 * When file has been modified ensure that sp << 2341 * file privileges are removed and time setti << 2342 * << 2343 * Context: Caller must hold the file's inode << 2344 * << 2345 * Return: 0 on success, negative errno on fa << 2346 */ << 2347 int kiocb_modified(struct kiocb *iocb) << 2348 { << 2349 return file_modified_flags(iocb->ki_f << 2350 } << 2351 EXPORT_SYMBOL_GPL(kiocb_modified); << 2352 << 2353 int inode_needs_sync(struct inode *inode) 1918 int inode_needs_sync(struct inode *inode) 2354 { 1919 { 2355 if (IS_SYNC(inode)) 1920 if (IS_SYNC(inode)) 2356 return 1; 1921 return 1; 2357 if (S_ISDIR(inode->i_mode) && IS_DIRS 1922 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) 2358 return 1; 1923 return 1; 2359 return 0; 1924 return 0; 2360 } 1925 } 2361 EXPORT_SYMBOL(inode_needs_sync); 1926 EXPORT_SYMBOL(inode_needs_sync); 2362 1927 2363 /* 1928 /* 2364 * If we try to find an inode in the inode ha 1929 * If we try to find an inode in the inode hash while it is being 2365 * deleted, we have to wait until the filesys 1930 * deleted, we have to wait until the filesystem completes its 2366 * deletion before reporting that it isn't fo 1931 * deletion before reporting that it isn't found. This function waits 2367 * until the deletion _might_ have completed. 1932 * until the deletion _might_ have completed. Callers are responsible 2368 * to recheck inode state. 1933 * to recheck inode state. 2369 * 1934 * 2370 * It doesn't matter if I_NEW is not set init 1935 * It doesn't matter if I_NEW is not set initially, a call to 2371 * wake_up_bit(&inode->i_state, __I_NEW) afte 1936 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list 2372 * will DTRT. 1937 * will DTRT. 2373 */ 1938 */ 2374 static void __wait_on_freeing_inode(struct in !! 1939 static void __wait_on_freeing_inode(struct inode *inode) 2375 { 1940 { 2376 struct wait_bit_queue_entry wqe; !! 1941 wait_queue_head_t *wq; 2377 struct wait_queue_head *wq_head; !! 1942 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW); 2378 !! 1943 wq = bit_waitqueue(&inode->i_state, __I_NEW); 2379 /* !! 1944 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2380 * Handle racing against evict(), see << 2381 */ << 2382 if (unlikely(inode_unhashed(inode))) << 2383 WARN_ON(is_inode_hash_locked) << 2384 spin_unlock(&inode->i_lock); << 2385 return; << 2386 } << 2387 << 2388 wq_head = inode_bit_waitqueue(&wqe, i << 2389 prepare_to_wait_event(wq_head, &wqe.w << 2390 spin_unlock(&inode->i_lock); 1945 spin_unlock(&inode->i_lock); 2391 rcu_read_unlock(); !! 1946 spin_unlock(&inode_hash_lock); 2392 if (is_inode_hash_locked) << 2393 spin_unlock(&inode_hash_lock) << 2394 schedule(); 1947 schedule(); 2395 finish_wait(wq_head, &wqe.wq_entry); !! 1948 finish_wait(wq, &wait.wq_entry); 2396 if (is_inode_hash_locked) !! 1949 spin_lock(&inode_hash_lock); 2397 spin_lock(&inode_hash_lock); << 2398 rcu_read_lock(); << 2399 } 1950 } 2400 1951 2401 static __initdata unsigned long ihash_entries 1952 static __initdata unsigned long ihash_entries; 2402 static int __init set_ihash_entries(char *str 1953 static int __init set_ihash_entries(char *str) 2403 { 1954 { 2404 if (!str) 1955 if (!str) 2405 return 0; 1956 return 0; 2406 ihash_entries = simple_strtoul(str, & 1957 ihash_entries = simple_strtoul(str, &str, 0); 2407 return 1; 1958 return 1; 2408 } 1959 } 2409 __setup("ihash_entries=", set_ihash_entries); 1960 __setup("ihash_entries=", set_ihash_entries); 2410 1961 2411 /* 1962 /* 2412 * Initialize the waitqueues and inode hash t 1963 * Initialize the waitqueues and inode hash table. 2413 */ 1964 */ 2414 void __init inode_init_early(void) 1965 void __init inode_init_early(void) 2415 { 1966 { 2416 /* If hashes are distributed across N 1967 /* If hashes are distributed across NUMA nodes, defer 2417 * hash allocation until vmalloc spac 1968 * hash allocation until vmalloc space is available. 2418 */ 1969 */ 2419 if (hashdist) 1970 if (hashdist) 2420 return; 1971 return; 2421 1972 2422 inode_hashtable = 1973 inode_hashtable = 2423 alloc_large_system_hash("Inod 1974 alloc_large_system_hash("Inode-cache", 2424 sizeo 1975 sizeof(struct hlist_head), 2425 ihash 1976 ihash_entries, 2426 14, 1977 14, 2427 HASH_ 1978 HASH_EARLY | HASH_ZERO, 2428 &i_ha 1979 &i_hash_shift, 2429 &i_ha 1980 &i_hash_mask, 2430 0, 1981 0, 2431 0); 1982 0); 2432 } 1983 } 2433 1984 2434 void __init inode_init(void) 1985 void __init inode_init(void) 2435 { 1986 { 2436 /* inode slab cache */ 1987 /* inode slab cache */ 2437 inode_cachep = kmem_cache_create("ino 1988 inode_cachep = kmem_cache_create("inode_cache", 2438 size 1989 sizeof(struct inode), 2439 0, 1990 0, 2440 (SLA 1991 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| 2441 SLAB !! 1992 SLAB_MEM_SPREAD|SLAB_ACCOUNT), 2442 init 1993 init_once); 2443 1994 2444 /* Hash may have been set up in inode 1995 /* Hash may have been set up in inode_init_early */ 2445 if (!hashdist) 1996 if (!hashdist) 2446 return; 1997 return; 2447 1998 2448 inode_hashtable = 1999 inode_hashtable = 2449 alloc_large_system_hash("Inod 2000 alloc_large_system_hash("Inode-cache", 2450 sizeo 2001 sizeof(struct hlist_head), 2451 ihash 2002 ihash_entries, 2452 14, 2003 14, 2453 HASH_ 2004 HASH_ZERO, 2454 &i_ha 2005 &i_hash_shift, 2455 &i_ha 2006 &i_hash_mask, 2456 0, 2007 0, 2457 0); 2008 0); 2458 } 2009 } 2459 2010 2460 void init_special_inode(struct inode *inode, 2011 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) 2461 { 2012 { 2462 inode->i_mode = mode; 2013 inode->i_mode = mode; 2463 if (S_ISCHR(mode)) { 2014 if (S_ISCHR(mode)) { 2464 inode->i_fop = &def_chr_fops; 2015 inode->i_fop = &def_chr_fops; 2465 inode->i_rdev = rdev; 2016 inode->i_rdev = rdev; 2466 } else if (S_ISBLK(mode)) { 2017 } else if (S_ISBLK(mode)) { 2467 if (IS_ENABLED(CONFIG_BLOCK)) !! 2018 inode->i_fop = &def_blk_fops; 2468 inode->i_fop = &def_b << 2469 inode->i_rdev = rdev; 2019 inode->i_rdev = rdev; 2470 } else if (S_ISFIFO(mode)) 2020 } else if (S_ISFIFO(mode)) 2471 inode->i_fop = &pipefifo_fops 2021 inode->i_fop = &pipefifo_fops; 2472 else if (S_ISSOCK(mode)) 2022 else if (S_ISSOCK(mode)) 2473 ; /* leave it no_open_f 2023 ; /* leave it no_open_fops */ 2474 else 2024 else 2475 printk(KERN_DEBUG "init_speci 2025 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for" 2476 " inode %s: 2026 " inode %s:%lu\n", mode, inode->i_sb->s_id, 2477 inode->i_in 2027 inode->i_ino); 2478 } 2028 } 2479 EXPORT_SYMBOL(init_special_inode); 2029 EXPORT_SYMBOL(init_special_inode); 2480 2030 2481 /** 2031 /** 2482 * inode_init_owner - Init uid,gid,mode for n 2032 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards 2483 * @idmap: idmap of the mount the inode was c << 2484 * @inode: New inode 2033 * @inode: New inode 2485 * @dir: Directory inode 2034 * @dir: Directory inode 2486 * @mode: mode of the new inode 2035 * @mode: mode of the new inode 2487 * << 2488 * If the inode has been created through an i << 2489 * the vfsmount must be passed through @idmap << 2490 * care to map the inode according to @idmap << 2491 * and initializing i_uid and i_gid. On non-i << 2492 * checking is to be performed on the raw ino << 2493 */ 2036 */ 2494 void inode_init_owner(struct mnt_idmap *idmap !! 2037 void inode_init_owner(struct inode *inode, const struct inode *dir, 2495 const struct inode *dir !! 2038 umode_t mode) 2496 { 2039 { 2497 inode_fsuid_set(inode, idmap); !! 2040 inode->i_uid = current_fsuid(); 2498 if (dir && dir->i_mode & S_ISGID) { 2041 if (dir && dir->i_mode & S_ISGID) { 2499 inode->i_gid = dir->i_gid; 2042 inode->i_gid = dir->i_gid; 2500 2043 2501 /* Directories are special, a 2044 /* Directories are special, and always inherit S_ISGID */ 2502 if (S_ISDIR(mode)) 2045 if (S_ISDIR(mode)) 2503 mode |= S_ISGID; 2046 mode |= S_ISGID; >> 2047 else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) && >> 2048 !in_group_p(inode->i_gid) && >> 2049 !capable_wrt_inode_uidgid(dir, CAP_FSETID)) >> 2050 mode &= ~S_ISGID; 2504 } else 2051 } else 2505 inode_fsgid_set(inode, idmap) !! 2052 inode->i_gid = current_fsgid(); 2506 inode->i_mode = mode; 2053 inode->i_mode = mode; 2507 } 2054 } 2508 EXPORT_SYMBOL(inode_init_owner); 2055 EXPORT_SYMBOL(inode_init_owner); 2509 2056 2510 /** 2057 /** 2511 * inode_owner_or_capable - check current tas 2058 * inode_owner_or_capable - check current task permissions to inode 2512 * @idmap: idmap of the mount the inode was f << 2513 * @inode: inode being checked 2059 * @inode: inode being checked 2514 * 2060 * 2515 * Return true if current either has CAP_FOWN 2061 * Return true if current either has CAP_FOWNER in a namespace with the 2516 * inode owner uid mapped, or owns the file. 2062 * inode owner uid mapped, or owns the file. 2517 * << 2518 * If the inode has been found through an idm << 2519 * the vfsmount must be passed through @idmap << 2520 * care to map the inode according to @idmap << 2521 * On non-idmapped mounts or if permission ch << 2522 * raw inode simply pass @nop_mnt_idmap. << 2523 */ 2063 */ 2524 bool inode_owner_or_capable(struct mnt_idmap !! 2064 bool inode_owner_or_capable(const struct inode *inode) 2525 const struct inod << 2526 { 2065 { 2527 vfsuid_t vfsuid; << 2528 struct user_namespace *ns; 2066 struct user_namespace *ns; 2529 2067 2530 vfsuid = i_uid_into_vfsuid(idmap, ino !! 2068 if (uid_eq(current_fsuid(), inode->i_uid)) 2531 if (vfsuid_eq_kuid(vfsuid, current_fs << 2532 return true; 2069 return true; 2533 2070 2534 ns = current_user_ns(); 2071 ns = current_user_ns(); 2535 if (vfsuid_has_mapping(ns, vfsuid) && !! 2072 if (kuid_has_mapping(ns, inode->i_uid) && ns_capable(ns, CAP_FOWNER)) 2536 return true; 2073 return true; 2537 return false; 2074 return false; 2538 } 2075 } 2539 EXPORT_SYMBOL(inode_owner_or_capable); 2076 EXPORT_SYMBOL(inode_owner_or_capable); 2540 2077 2541 /* 2078 /* 2542 * Direct i/o helper functions 2079 * Direct i/o helper functions 2543 */ 2080 */ 2544 bool inode_dio_finished(const struct inode *i !! 2081 static void __inode_dio_wait(struct inode *inode) 2545 { 2082 { 2546 return atomic_read(&inode->i_dio_coun !! 2083 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP); >> 2084 DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP); >> 2085 >> 2086 do { >> 2087 prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE); >> 2088 if (atomic_read(&inode->i_dio_count)) >> 2089 schedule(); >> 2090 } while (atomic_read(&inode->i_dio_count)); >> 2091 finish_wait(wq, &q.wq_entry); 2547 } 2092 } 2548 EXPORT_SYMBOL(inode_dio_finished); << 2549 2093 2550 /** 2094 /** 2551 * inode_dio_wait - wait for outstanding DIO 2095 * inode_dio_wait - wait for outstanding DIO requests to finish 2552 * @inode: inode to wait for 2096 * @inode: inode to wait for 2553 * 2097 * 2554 * Waits for all pending direct I/O requests 2098 * Waits for all pending direct I/O requests to finish so that we can 2555 * proceed with a truncate or equivalent oper 2099 * proceed with a truncate or equivalent operation. 2556 * 2100 * 2557 * Must be called under a lock that serialize 2101 * Must be called under a lock that serializes taking new references 2558 * to i_dio_count, usually by inode->i_mutex. 2102 * to i_dio_count, usually by inode->i_mutex. 2559 */ 2103 */ 2560 void inode_dio_wait(struct inode *inode) 2104 void inode_dio_wait(struct inode *inode) 2561 { 2105 { 2562 wait_var_event(&inode->i_dio_count, i !! 2106 if (atomic_read(&inode->i_dio_count)) >> 2107 __inode_dio_wait(inode); 2563 } 2108 } 2564 EXPORT_SYMBOL(inode_dio_wait); 2109 EXPORT_SYMBOL(inode_dio_wait); 2565 2110 2566 void inode_dio_wait_interruptible(struct inod << 2567 { << 2568 wait_var_event_interruptible(&inode-> << 2569 inode_di << 2570 } << 2571 EXPORT_SYMBOL(inode_dio_wait_interruptible); << 2572 << 2573 /* 2111 /* 2574 * inode_set_flags - atomically set some inod 2112 * inode_set_flags - atomically set some inode flags 2575 * 2113 * 2576 * Note: the caller should be holding i_mutex 2114 * Note: the caller should be holding i_mutex, or else be sure that 2577 * they have exclusive access to the inode st 2115 * they have exclusive access to the inode structure (i.e., while the 2578 * inode is being instantiated). The reason 2116 * inode is being instantiated). The reason for the cmpxchg() loop 2579 * --- which wouldn't be necessary if all cod 2117 * --- which wouldn't be necessary if all code paths which modify 2580 * i_flags actually followed this rule, is th 2118 * i_flags actually followed this rule, is that there is at least one 2581 * code path which doesn't today so we use cm 2119 * code path which doesn't today so we use cmpxchg() out of an abundance 2582 * of caution. 2120 * of caution. 2583 * 2121 * 2584 * In the long run, i_mutex is overkill, and 2122 * In the long run, i_mutex is overkill, and we should probably look 2585 * at using the i_lock spinlock to protect i_ 2123 * at using the i_lock spinlock to protect i_flags, and then make sure 2586 * it is so documented in include/linux/fs.h 2124 * it is so documented in include/linux/fs.h and that all code follows 2587 * the locking convention!! 2125 * the locking convention!! 2588 */ 2126 */ 2589 void inode_set_flags(struct inode *inode, uns 2127 void inode_set_flags(struct inode *inode, unsigned int flags, 2590 unsigned int mask) 2128 unsigned int mask) 2591 { 2129 { >> 2130 unsigned int old_flags, new_flags; >> 2131 2592 WARN_ON_ONCE(flags & ~mask); 2132 WARN_ON_ONCE(flags & ~mask); 2593 set_mask_bits(&inode->i_flags, mask, !! 2133 do { >> 2134 old_flags = READ_ONCE(inode->i_flags); >> 2135 new_flags = (old_flags & ~mask) | flags; >> 2136 } while (unlikely(cmpxchg(&inode->i_flags, old_flags, >> 2137 new_flags) != old_flags)); 2594 } 2138 } 2595 EXPORT_SYMBOL(inode_set_flags); 2139 EXPORT_SYMBOL(inode_set_flags); 2596 2140 2597 void inode_nohighmem(struct inode *inode) 2141 void inode_nohighmem(struct inode *inode) 2598 { 2142 { 2599 mapping_set_gfp_mask(inode->i_mapping 2143 mapping_set_gfp_mask(inode->i_mapping, GFP_USER); 2600 } 2144 } 2601 EXPORT_SYMBOL(inode_nohighmem); 2145 EXPORT_SYMBOL(inode_nohighmem); 2602 2146 2603 /** 2147 /** 2604 * timestamp_truncate - Truncate timespec to !! 2148 * timespec64_trunc - Truncate timespec64 to a granularity 2605 * @t: Timespec !! 2149 * @t: Timespec64 2606 * @inode: inode being updated !! 2150 * @gran: Granularity in ns. 2607 * 2151 * 2608 * Truncate a timespec to the granularity sup !! 2152 * Truncate a timespec64 to a granularity. Always rounds down. gran must 2609 * containing the inode. Always rounds down. << 2610 * not be 0 nor greater than a second (NSEC_P 2153 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). 2611 */ 2154 */ 2612 struct timespec64 timestamp_truncate(struct t !! 2155 struct timespec64 timespec64_trunc(struct timespec64 t, unsigned gran) 2613 { 2156 { 2614 struct super_block *sb = inode->i_sb; << 2615 unsigned int gran = sb->s_time_gran; << 2616 << 2617 t.tv_sec = clamp(t.tv_sec, sb->s_time << 2618 if (unlikely(t.tv_sec == sb->s_time_m << 2619 t.tv_nsec = 0; << 2620 << 2621 /* Avoid division in the common cases 2157 /* Avoid division in the common cases 1 ns and 1 s. */ 2622 if (gran == 1) !! 2158 if (gran == 1) { 2623 ; /* nothing */ !! 2159 /* nothing */ 2624 else if (gran == NSEC_PER_SEC) !! 2160 } else if (gran == NSEC_PER_SEC) { 2625 t.tv_nsec = 0; 2161 t.tv_nsec = 0; 2626 else if (gran > 1 && gran < NSEC_PER_ !! 2162 } else if (gran > 1 && gran < NSEC_PER_SEC) { 2627 t.tv_nsec -= t.tv_nsec % gran 2163 t.tv_nsec -= t.tv_nsec % gran; 2628 else !! 2164 } else { 2629 WARN(1, "invalid file time gr !! 2165 WARN(1, "illegal file time granularity: %u", gran); >> 2166 } 2630 return t; 2167 return t; 2631 } 2168 } 2632 EXPORT_SYMBOL(timestamp_truncate); !! 2169 EXPORT_SYMBOL(timespec64_trunc); 2633 2170 2634 /** 2171 /** 2635 * current_time - Return FS time 2172 * current_time - Return FS time 2636 * @inode: inode. 2173 * @inode: inode. 2637 * 2174 * 2638 * Return the current time truncated to the t 2175 * Return the current time truncated to the time granularity supported by 2639 * the fs. 2176 * the fs. 2640 * 2177 * 2641 * Note that inode and inode->sb cannot be NU 2178 * Note that inode and inode->sb cannot be NULL. 2642 * Otherwise, the function warns and returns 2179 * Otherwise, the function warns and returns time without truncation. 2643 */ 2180 */ 2644 struct timespec64 current_time(struct inode * 2181 struct timespec64 current_time(struct inode *inode) 2645 { 2182 { 2646 struct timespec64 now; !! 2183 struct timespec64 now = current_kernel_time64(); 2647 << 2648 ktime_get_coarse_real_ts64(&now); << 2649 return timestamp_truncate(now, inode) << 2650 } << 2651 EXPORT_SYMBOL(current_time); << 2652 << 2653 /** << 2654 * inode_set_ctime_current - set the ctime to << 2655 * @inode: inode << 2656 * << 2657 * Set the inode->i_ctime to the current valu << 2658 * the current value that was assigned to i_c << 2659 */ << 2660 struct timespec64 inode_set_ctime_current(str << 2661 { << 2662 struct timespec64 now = current_time( << 2663 2184 2664 inode_set_ctime_to_ts(inode, now); !! 2185 if (unlikely(!inode->i_sb)) { 2665 return now; !! 2186 WARN(1, "current_time() called with uninitialized super_block in the inode"); 2666 } !! 2187 return now; 2667 EXPORT_SYMBOL(inode_set_ctime_current); !! 2188 } 2668 << 2669 /** << 2670 * in_group_or_capable - check whether caller << 2671 * @idmap: idmap of the mount @inode was << 2672 * @inode: inode to check << 2673 * @vfsgid: the new/current vfsgid of @in << 2674 * << 2675 * Check wether @vfsgid is in the caller's gr << 2676 * privileged with CAP_FSETID over @inode. Th << 2677 * whether the setgid bit can be kept or must << 2678 * << 2679 * Return: true if the caller is sufficiently << 2680 */ << 2681 bool in_group_or_capable(struct mnt_idmap *id << 2682 const struct inode * << 2683 { << 2684 if (vfsgid_in_group_p(vfsgid)) << 2685 return true; << 2686 if (capable_wrt_inode_uidgid(idmap, i << 2687 return true; << 2688 return false; << 2689 } << 2690 EXPORT_SYMBOL(in_group_or_capable); << 2691 2189 2692 /** !! 2190 return timespec64_trunc(now, inode->i_sb->s_time_gran); 2693 * mode_strip_sgid - handle the sgid bit for << 2694 * @idmap: idmap of the mount the inode was c << 2695 * @dir: parent directory inode << 2696 * @mode: mode of the file to be created in @ << 2697 * << 2698 * If the @mode of the new file has both the << 2699 * raised and @dir has the S_ISGID bit raised << 2700 * either in the group of the parent director << 2701 * in their user namespace and are privileged << 2702 * In all other cases, strip the S_ISGID bit << 2703 * << 2704 * Return: the new mode to use for the file << 2705 */ << 2706 umode_t mode_strip_sgid(struct mnt_idmap *idm << 2707 const struct inode *d << 2708 { << 2709 if ((mode & (S_ISGID | S_IXGRP)) != ( << 2710 return mode; << 2711 if (S_ISDIR(mode) || !dir || !(dir->i << 2712 return mode; << 2713 if (in_group_or_capable(idmap, dir, i << 2714 return mode; << 2715 return mode & ~S_ISGID; << 2716 } 2191 } 2717 EXPORT_SYMBOL(mode_strip_sgid); !! 2192 EXPORT_SYMBOL(current_time); 2718 2193
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.